1. Write a python function called calculateSum that takes two integer arrays of the same length and calculates the sum array (by adding elements in the same index).
Sample
Arguments: [2, 5, 6, 5]. 7,3, 3, 2]
Function wll return: (9, 8, 9, 7]
import os
def calculateSum(a, b):
try:
if len(a) != len(b):
print("Arrays have different sizes!")
os._exit(1)
else:
return [int(a[i]) + int(b[i]) for i in range(len(a))]
except:
print("Wrong input. Array contains non-integer value.")
os._exit(2)
if __name__ == '__main__':
print("Numbers are to be separated by spaces.")
a = input("Enter numbers in the first array:").split(" ")
b = input("Enter numbers in the second array:").split(" ")
print(calculateSum(a, b))
Comments
Leave a comment