fork download
  1. from functools import reduce
  2. import sys
  3.  
  4. def parse_int_list(numbers_str, acc=None):
  5.  
  6. if acc is None:
  7. acc = []
  8.  
  9. # empty string case
  10. numbers_str = numbers_str.strip()
  11. if not numbers_str:
  12. return acc
  13.  
  14. # Finding first number
  15. space_index = numbers_str.find(' ')
  16. if space_index == -1:
  17. # returning Last number
  18. return acc + [int(numbers_str)]
  19.  
  20. # recursive calling
  21. current = int(numbers_str[:space_index])
  22. return parse_int_list(numbers_str[space_index:], acc + [current])
  23.  
  24. def read_input():
  25.  
  26. return sys.stdin.readline().strip()
  27.  
  28. def process_test_case():
  29.  
  30. count = int(read_input())
  31. numbers = parse_int_list(read_input())
  32.  
  33. # Filtering positive numbers and using reduce for calculating sum
  34. return reduce(
  35. lambda acc, x: acc + x * x,
  36. filter(lambda x: x > 0, numbers),
  37. 0
  38. )
  39.  
  40. def process_all_cases(remaining, results):
  41.  
  42. if remaining == 0:
  43. # Printing all results without blank spaces
  44. print(*results, sep='\n')
  45. return
  46.  
  47. # Processing current test case and iterating rest
  48. return process_all_cases(
  49. remaining - 1,
  50. results + [process_test_case()]
  51. )
  52.  
  53. def main():
  54. # Reading number of test cases
  55. num_cases = int(read_input())
  56. process_all_cases(num_cases, [])
  57.  
  58. if __name__ == "__main__":
  59. main()
Success #stdin #stdout 0.05s 9800KB
stdin
2
4
3 -1 1 14
5
9 6 -53 32 16
stdout
206
1397