From 3bca7ea33f330bf1e2a150562c99e654a0144d6d Mon Sep 17 00:00:00 2001 From: Colin Date: Wed, 13 Aug 2025 21:20:21 -0400 Subject: [PATCH] Update e09b4_even_odd_sums.py Refactored to use slicing and sum() for even/odd indices, making the code shorter, clearer, and matching the problem statement. --- ch03-lists-tuples/e09b4_even_odd_sums.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/ch03-lists-tuples/e09b4_even_odd_sums.py b/ch03-lists-tuples/e09b4_even_odd_sums.py index 3afc336..fd5d941 100755 --- a/ch03-lists-tuples/e09b4_even_odd_sums.py +++ b/ch03-lists-tuples/e09b4_even_odd_sums.py @@ -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])]