CSc 120: Sum the Odd Numbers (Recursive)
Expected Behavior
Write a recursive function sum_odds(arglist), where
arglist is a list of integers, that returns the sum of all
of the odd elements of arglist.
Programming Requirements
Solve this problem using recursion. You are allowed to use only
the following programming constructs:
-
if statements;
-
return statements;
-
assignment;
-
recursive function calls (but not to helper functions);
-
comparison operations (==, !=, etc.);
-
arithmetic operations (+, -, etc.);
-
list indexing and slicing;
-
list and string concatenation.
Solutions that go outside these constructs, e.g., by
using for/while loops or list comprehensions,
will not get credit.
Examples
-
sum_odds([1,3,5,7,6])
return value: 16
-
sum_odds([2,4,6])
return value: 0
-
sum_odds([])
return value: 0
-
sum_odds([1,2,3,4,5,6,7,8,9])
return value: 25