Due Tuesday 21-Sep, at 10:00pm
hw4.py
file includes test functions to help you test on your own before
you submit to Gradescope. When you run your file, problems will be tested in order. If
you wish to temporarily bypass specific tests (say, because you have not yet completed
some functions), you can comment out individual test function calls at the bottom
of your file in main()
. However, be sure to uncomment and test everything together
before you submit! Ask a CA if you need help with this.Do not use sets, dictionaries, try/except, classes, or recursion this week. The autograder (or a manual CA review later) will reject your submission entirely if you do.
Like in the previous assignment, 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 starts this week, so please use good style from now on!
You will notice that the skeleton file only includes testcases for some of the functions you are writing. You should write testcases for the others. (You can find some nice ways to test in the write-up below, but you will need to translate those to actual testcases.)
vowelCount(s)
, that takes a string s, and returns the number of vowels in s, ignoring case, so "A" and "a" are both vowels. The vowels are "a", "e", "i", "o", and "u". So, for example:
rotateStringLeft(s, n)
that takes a string s and a
possibly-negative integer n. If n is non-negative, the function
returns the string s rotated n places to the left.
If n is negative, the function
returns the string s rotated |n| places to the right. So, for example:
isRotation(s, t)
that takes two possibly-empty strings and returns True if one is a rotation of the other. Note that a string
is not considered a rotation of itself.nondestructiveRotateList(a, n)
which
takes a list L
and an integer n
, and nondestructively modifies the list so
that each element is shifted to the right by n
indices (including
wraparound). The function should then return this new list. For example:
nondestructiveRotateList([1,2,3,4], 1) -> [4,1,2,3] nondestructiveRotateList([4,3,2,6,5], 2) -> [6, 5, 4, 3, 2] nondestructiveRotateList([1,2,3], 0) -> [1,2,3] nondestructiveRotateList([1, 2, 3], -1) -> [2, 3, 1]
L
, so after
the call, that exact list is rotated n
indices to the right
with wraparound, and a new list is not created. As usual for
destructive functions, this function returns None
.
Also: you may not call the nondestructive version here, and in
fact, you may not even create a new list (or tuple or other similar data
structure). You must modify the original list in place (list methods are
your friends!).
text
your task is to find the most frequently
occuring substring of a given length. In the event of a tie between two substrings, follow alphabetic order. Consider the following example in which the length is three (n
= 3) and the text is
just baababacb
. The most frequent substring would then
be aba
because this is the substring with size 3 that appears most
often in the whole text (it appears twice) while the other six different
substrings appear only once (baa ; aab ; bab ; bac ; acb)
. You can
assume length >= 0
. Here are more examples:
getEvalSteps(expr)
, that takes a string containing a simple arithmetic expression, and returns a
multi-line string containing the step-by-step (one operator at a time) evaluation of that expression. For example, this
call:
getEvalSteps("2+3*4-8**3%3")
produces this result (which is a single multi-line string):
2+3*4-8**3%3 = 2+3*4-512%3 = 2+12-512%3 = 2+12-2 = 14-2 = 12Here are some considerations and hints: