- 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.
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)
+ 3
Sani Ibrahim Mahmud are you looking for something like this?
https://code.sololearn.com/cvNXSH32XsGC/?ref=app
+ 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
+ 2
Have you tried to code this solution? Can you share you attempt?
+ 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)
+ 1
Where is your return? Don't you want to return even and odd?