Sum of square

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.3 kB
2
Indexable
class SquareSumCalculator:
    def __init__(self):
        self.test_case_results = []

    def square_sum(self, numbers, n, total):
        if n == 0:
            return total
        last_num = numbers.pop()
        return self.square_sum(numbers, n - 1, total + (last_num ** 2) * (last_num > 0))

    def calculate_and_store_results(self, test_cases):
        if test_cases > 0:
            try:
                x = int(input())
                y_values = list(map(int, input().split()))
                test_case_re = self.square_sum(y_values, x, 0)
                self.test_case_results.append(test_case_re)
                self.calculate_and_store_results(test_cases - 1)
            except ValueError as e:
                print(f"Error: {e}")

    def print_results(self, index=0):
        if index < len(self.test_case_results):
            print(self.test_case_results[index])
            self.print_results(index + 1)

def main():
    try:
        test_cases = int(input())
        calculator = SquareSumCalculator()
        calculator.calculate_and_store_results(test_cases)
        calculator.print_results()
    except ValueError as e:
        print(f"Error: {e}")
    except KeyboardInterrupt:
        print("\nOperation aborted by user.")


if __name__ == "__main__":
    main()