491,913 views
6 votes
6 votes
Write a function addingAllTheWeirdStuff which adds the sum of all the odd numbers in array2 to each element under 10 in array1. Similarly, addingAllTheWeirdStuff should also add the sum of all the even numbers in array2 to those elements over 10 in array1.

User Sigman
by
2.6k points

1 Answer

12 votes
12 votes

Answer:

The function is as follows:

def addingAllTheWeirdStuff(array1,array2):

sumOdd = 0

for i in array2:

if i % 2 == 1:

sumOdd+=i

for i in array1:

if i < 10:

sumOdd+=i

print("Sum:",sumOdd)

sumEven = 0

for i in array1:

if i % 2 == 0:

sumEven+=i

for i in array2:

if i > 10:

sumEven+=i

print("Sum:",sumEven)

Step-by-step explanation:

This declares the function

def addingAllTheWeirdStuff(array1,array2):

This initializes the sum of odd numbers in array 2 to 0

sumOdd = 0

This iterates through array 2

for i in array2:

This adds up all odd numbers in it

if i % 2 == 1:

sumOdd+=i

This iterates through array 1

for i in array1:

This adds up all elements less than 10 to sumOdd

if i < 10:

sumOdd+=i

This prints the calculated sum

print("Sum:",sumOdd)

This initializes the sum of even numbers in array 1 to 0

sumEven = 0

This iterates through array 1

for i in array1:

This adds up all even numbers in it

if i % 2 == 0:

sumEven+=i

This iterates through array 2

for i in array2:

This adds up all elements greater than 10 to sumEven

if i > 10:

sumEven+=i

This prints the calculated sum

print("Sum:",sumEven)

User Vishnus
by
2.3k points