Due Tuesday 22-Mar, at 10:00pm
hw9
hw9.py
in that folderhw9.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.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
.
You can't use the builtin function max
.
onlyEvenDigits(L)
, that takes a list L
of non-negative integers (you may assume that), and returns a new list of the
same numbers only without their odd digits
(if that leaves no digits, then replace the number with 0). So:
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.
onlyEvens(n)
that takes an integer n
and returns the same integer but with all the odd digits removed. So onlyEvens(1234)
returns 24.
alternatingSum(L)
that takes a possibly-empty list of numbers, L
, and returns the alternating sum of the list,
where every other value is subtracted rather than added. For example: alternatingSum([1,2,3,4,5]) returns 1-2+3-4+5 (that is, 3).
If lst is empty, return 0.
d
, and 2 smaller lines of length d
divided by √2 originating from each end point. The end points of each of
the smaller lines are also the origin of a recursively downsized fractal h-tree
with length equal to half of the length of the h-tree at the previous level. The
width of the lines should gradually decrease as well (make reasonable choices).
Your fractal h-tree will be generated by a function drawFractalHTree(canvas,
xc, yc, d, color, level)
which you will write. Your function will take as
parameter a canvas to draw on, the (xc, yc) coordinates of the center of the
h-tree, the length d of the horizontal line, a color, and an integer level
representing the maximum depth of recursion of your fractal h-tree.
The following
picture shows a fractal h-tree with a maximum recursion depth of 2 with color blue.