Due Tuesday 12-Sep, at 10:00pm
hw3.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 convert strings to lists, use lists or recursion this week. The autograder (or a manual CA review later) will reject your submission entirely if you do.
Starting with this 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.)
consonantCount(s)
, that takes a string s
, and returns the number of consonants in s
, ignoring case, so "B" and "b" are both consonants. The consonants are all of the English letters
except "a", "e", "i", "o", and "u". So, for example:
input()
to read a string from the user and then use
the function float()
to convert that string to a float number. We
also learned that if the user enters an invalid number, our program will crash.
Now we don't like programs that crash. So, we would like to check if a string
can be a float before we attempt to convert it to a float. This way, if the
string is not float, we can print an error message and exit gracefully instead
of crashing the program. If would be nice to have a function called isFloat that
takes an input of string and returns back True if the input is a valid float and
False if it is not. We can use this function in our programs as given below:
isFloat
function that checks the
string passed to it for validity of being a float number. If the input is a
valid float number, it should return True, otherwise, it should return False.
We will be calling the function as shown in the above example, so make sure the
return values, function name, and input parameters are appropriate. You should
NOT use a try/except structure for this function.
'ABC'
and we use the shifts 3,4,5. Then we shift 'A' by 3 to get 'D', we shift 'B' by 4 to get 'F', and we shift 'C' by 5 to get 'H'. So the encoded message is 'DFH'.
Next, instead of listing the shifts as numbers, we can encode the shifts themselves in a string which we will call the key. Here, 'A' is 0, 'B' is 1, and so on. So the shifts 3,4,5 will be represented by the key 'DEF'.
Finally, what happens if our key is shorter than our message? In that case, we will repeat the key until it is as long or longer than the message. For example, if we have the message 'FGHIJ'
and the key 'AB'
, we first repeat the key to get 'ABABAB'
. Now encode the message as just described with this key.
A Vigenere Cipher works in this way. It takes a message and a key, repeats the key until it is at least as long as the message, and then uses the key to find the shift for each corresponding letter in the message.
With this in mind, write the function applyVigenereCipher(msg, key)
which returns the encoded message that results from perfoming a Vigenere Cipher
on msg
with the given key.
Some notes:
topScorer(data)
that takes a multi-line string encoding
scores as csv data for some kind of competition with players receiving scores,
so each line has comma-separated values. The first
value on each line is the name of the player (which you can assume
has no integers in it), and each value after that
is an individual score (which you can assume is a non-negative integer).
You should add all the scores for that player,
and then return the player with the highest total score. If there is a tie,
return all the tied players in a comma-separated string with the names
in the same order they appeared in the original data. If nobody wins
(there is no data), return None
(not the string "None"). So, for example:
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:
call | result |
patternedMessage("Go Pirates!!!", """ *************** ****** ****** *************** """) |
GoPirates!!!GoP irates !!!GoP irates!!!GoPira |
call | result |
patternedMessage("Three Diamonds!",""" * * * *** *** *** ***** ***** ***** *** *** *** * * * """) |
T h r eeD iam ond s!Thr eeDia monds !Th ree Dia m o n |
patternedMessage("Go Steelers!", """ oooo$$$$$$$$$$$$oooo oo$$$$$$$$$$$$$$$$$$$$$$$$o oo$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o o$ $$ o$ o $ oo o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o $$ $$ $$o$ oo $ $ '$ o$$$$$$$$$ $$$$$$$$$$$$$ $$$$$$$$$o $$$o$$o$ '$$$$$$o$ o$$$$$$$$$ $$$$$$$$$$$ $$$$$$$$$$o $$$$$$$$ $$$$$$$ $$$$$$$$$$$ $$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$ $$$$$$$$$$$$$$ '$$$ '$$$'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ '$$$ $$$ o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ '$$$o o$$' $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$o $$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$' '$$$$$$ooooo$$$$o o$$$oooo$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ o$$$$$$$$$$$$$$$$$ $$$$$$$$'$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$' '''' $$$$ '$$$$$$$$$$$$$$$$$$$$$$$$$$$$' o$$$ '$$$o '$$$$$$$$$$$$$$$$$$'$$' $$$ $$$o '$$'$$$$$$' o$$$ $$$$o o$$$' '$$$$o o$$$$$$o'$$$$o o$$$$ '$$$$$oo '$$$$o$$$$$o o$$$$' '$$$$$oooo '$$$o$$$$$$$$$' '$$$$$$$oo $$$$$$$$$$ '$$$$$$$$$$$ $$$$$$$$$$$$ $$$$$$$$$$' '$$$' """)
GoSteelers!GoSteeler s!GoSteelers!GoSteelers!GoS teelers!GoSteelers!GoSteelers!GoS te el er s ! Go Steelers!GoSteelers!GoSteelers!GoSteel er s! GoSt ee l e rs !GoSteeler s!GoSteelers! GoSteelers !GoSteel ers!GoSte elers!GoSt eelers!GoSt eelers!GoSt eelers!G oSteele rs!GoSteele rs!GoSteele rs!GoSteelers!GoSteeler s!GoSteelers!GoSteelers !GoSteelers!G oSteelers!GoSt eele rs!GoSteelers!GoSteelers!GoSteelers!GoSteelers!GoSteel ers! GoS teelers!GoSteelers!GoSteelers!GoSteelers!GoSteelers !GoSt eele rs!GoSteelers!GoSteelers!GoSteelers!GoSteelers!GoSt eele rs! GoSteelers!GoSteelers!GoSteelers!GoSteelers!Go Steelers!GoSteele rs!GoSteelers !GoSteelers!GoSteelers!GoSteelers!GoS teelers!GoSteelers !GoSteelers!G oSteelers!GoSteelers!GoSteelers!Go Steel ers! GoSt eelers!GoSteelers!GoSteelers!G oSte elers !GoSteelers!GoSteelers! GoS teel ers!GoSteel ers! GoSte elers !GoSte elers!GoSteele rs!Go Steelers !GoSteelers! GoStee lers!GoSte elers!GoSteeler s!GoSteele rs!GoSteel ers!GoSteele rs!GoSteeler s!GoSteeler s!GoS
bonusGetEvalSteps(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:
bonusGetEvalSteps("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:
bonusEncode112(s)
and bonusDecode112(s)
that can bonusEncode and decode strings using it.
Here's the one (an only) example we can give you: