CSc 120: Count the Odd Elements (Recursion)
Expected Behavior
Write a recursive function count_odds(arglist), where
arglist is a list of integers, that returns the number of elements
in arglist that are odd.
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
-
count_odds([1,3,5,7,6])
return value: 4
-
count_odds([2,4,6])
return value: 0
-
count_odds([])
return value: 0
-
count_odds([1,2,3,4,5,6,7,8,9])
return value: 5