Answer to Question #157393 in Python for Andu

Question #157393

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]


1
Expert's answer
2021-01-24T17:31:43-0500
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))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS