Due Tuesday 5-Nov, at 9:00pm
hw9
hw9.py
in that folderhw9.py
and add the functions and some testcases as requiredhw9.py
to Gradescope.
For this hw, you may submit up to 15 times, but only your last submission counts.While you may submit to Gradescope as often as you like for this assignment, some questions are not autograded, so you will be responsible for testing your code and making sure it meets the problem requirements.
Like in the previous assignments, we will be grading your code based on whether it follows the 15-112 style guide. We may deduct up to 10 points from your overall grade for style errors. We highly recommend that you try to write clean code with good style all along, rather than fixing your style issues at the end. Good style helps you code faster and with fewer bugs. It is totally worth it. In any case, style grading already started, so please use good style from now on!
for
loops or while
on the problems marked recursive.maxEven(L)
you cannot call evensOnly(L)
and then take the max of that list, since that gratuitously creates an extraneous list (and, to be technical, uses O(N) space instead of O(1) space as the problem requires). Instead, directly compute maxEven(L)
with recursion. In fact, that’s the main point of asking you to solve both evensOnly(L)
and maxEven(L)
: to encourage you to think about the structural differences in these problems and how that affects your recursive logic.evenCount(L)
which given a
possibly-empty list L
of integers, returns the number of even integers in L
.
So:
evenCount([5,8,23,42])
returns
2
.
evenSum(L)
which given a possibly-empty list L
of integers, returns the sum of
the even integers in L
. Do not create a new list. You cannot use
the builtin function sum
of lists. Return 0 if the list has no even
integers in it. So,
evenSum([5,8,23,42])
returns
50
.
evensOnly(L)
which given a possibly-empty list L
of integers, returns a new list containing
only the even integers in L
in the same order they appear in L
. So,
evensOnly([5,8,23,42])
returns
[8, 42]
.
maxEven(L)
which given a possibly-empty list L
of integers, returns the largest even
integer in L
, or None
if L
does not contain any even integers.
So, maxEven([5,8,23,42])
returns
42
.
onlyEvenDigits([43, 23265, 17, 58344])
returns
[4, 226, 0, 844]
.
Also the function returns the empty list if the original list is empty.
Remember to not use strings.
You may not use loops/iteration in this problem.
True
if the
string is palindrome, False
otherwise.
For example,
Write the recursive function digitCountMapInRange(lo, hi)
that takes integers lo
and hi
, and returns a
map of the counts of all the digits that occur in the numbers between
lo
and hi
inclusively.
For example, for digitCountMapInRange(9, 12)
, we consider
the numbers in the range from 9
to 12
. That
is: 9, 10, 11, 12
. These numbers include one 0
,
four 1
's, one 2
, and one 9
, so:
assert(digitCountMapInRange(9, 12) == { 0:1, 1:4, 2:1, 9:1 })
.
Be sure to handle negative numbers and 0
correctly.
For example, for digitCountMapInRange(-1, 3)
, we consider
the numbers in the range from -1
to 3
. That
is: -1, 0, 1, 2, 3
. These numbers include one 0
,
two 1
's, one 2
, and one 3
, so:
assert(digitCountMapInRange(-1, 3) == { 0:1, 1:2, 2:1, 3:1 })