- 3

Please can someone solve this problem using python, javascript or java ?

Write a function that takes an array of positive integers. The function should calculate the sum of all even and odd integers and return an array containing the sums. The first index in the returned array should hold the sum of the even integers and the second index should hold the sum of the odd integers.

6th Sep 2020, 12:42 AM
Sani Ibrahim Mahmud
Sani Ibrahim Mahmud - avatar
6 ответов
+ 10
def sum_even_odd(arr): evensum = sum(n for n in arr if not n % 2) oddsum = sum(n for n in arr if n % 2) return evensum, oddsum arr=[1, 2, 3, 4, 5, 6] print(sum_even_odd(arr)) # output (12,9)
6th Sep 2020, 5:22 AM
David Ashton
David Ashton - avatar
+ 3
6th Sep 2020, 1:26 AM
Steven M
Steven M - avatar
+ 3
hi Sani Ibrahim Mahmud it would have been cool if you try this yourself and then if you get stuck you can share that here on Q&A to get help that way you will have good knowledge of the problem
6th Sep 2020, 10:02 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 2
Have you tried to code this solution? Can you share you attempt?
6th Sep 2020, 12:43 AM
Steven M
Steven M - avatar
+ 1
def EvenOddSum(a, n):     even = 0     odd = 0     for i in range(n):         if i % 2 == 0:             even += a[i]         else:             odd += a[i] arr = [1, 2, 3, 4, 5, 6] n = len(arr) EvenOddSum(arr, n)
6th Sep 2020, 12:47 AM
Sani Ibrahim Mahmud
Sani Ibrahim Mahmud - avatar
+ 1
Where is your return? Don't you want to return even and odd?
6th Sep 2020, 1:00 AM
Steven M
Steven M - avatar