Due Tuesday 31-Oct, 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.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
.
alternatingSum(L, n)
that takes a possibly-empty list of numbers, L, and returns the alternating sum of the list,
where every nth value is subtracted rather than added.
n will be always greater than 1.
For example: alternatingSum([5,2,3,4,1,6], 2)
returns 5-2+3-4+1-6 (that is, -3).
alternatingSum([5,2,3,4,1,6], 3)
returns 5+2-3+4+1-6 (that is, 3).
If L is empty, return 0.
upper()
for strings is forbidden if the
string has more than one character. You may use O(N) string builtin functions as
long as the string consists of one character. This means, you can use
s.upper()
only if len(s) == 1
. If you do, the
autograder or a manual check later will reject your solution.
r
, and 8 rays of length 2*r
originating from the center of
the circle and radially equally spaced. The external tip of each ray is also the
origin of a recursively downsized fractal sun with radius equal to 1/4 of the
radius of the sun at the previous level. Also, the suns originating at the tip
of the rays will have different colors, i.e., the color of a sun is a function
of the recursion level of the fractal sun itself. You can invent the coloring
function you prefer, just make sure that your sun will look good no matter what
the maximum level of recursion is going to be. Your fractal sun will be
generated by a recursive function drawFractalSun(xc, yc, r, 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 sun, the radius r of the sun's circle, and an
integer level representing the maximum depth of recursion of your fractal sun.
Note: In this task, you can use iteration to draw the rays.
The following picture shows a fractal sun with a maximum recursion depth of 1, 2, and 4, with colors alternating from red, green, and blue.
cmu_graphics
.
Do not call runApp in the submitted code. Otherwise, the autograder might fail.