Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions ch03-lists-tuples/e09b4_even_odd_sums.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@
"""Solution to chapter 3, exercise 9, beyond 4: even_odd_sums"""


def even_odd_sums(numbers):
"""Takes a list of numbers, and returns a two-element
list containing the sum of the even elements and the
sum of the odd elements.
"""
evens = []
odds = []
# Write a function that takes a list or tuple of numbers. Return a two-element list,
# containing (respectively) the sum of the even-indexed numbers and the sum of
# the odd-indexed numbers. So calling the function as even_odd_sums([10, 20,
# 30, 40, 50, 60]), you’ll get back [90, 120].

for one_number in numbers:
if one_number % 2:
odds.append(one_number)
else:
evens.append(one_number)

return [sum(evens), sum(odds)]
def even_odd_sums(any_type):
return [sum(any_type[::2]),sum(any_type[1::2])]