Due Tuesday 27-Jan, at 9:00pm
hw3.py file includes some 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 lists, list indexing, sets, dictionaries, recursion, or anything we have not yet covered in class or the notes. 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:
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.rotateStringLeft may be helpful here.
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:
'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:
longestSubpalindrome(s) from CS Academy.
You must solve the problem directly on the website, doing all of your testing there. Do not write the solution in Thonny (or a different IDE) and copy/paste it into the website.
patternedMessage(message, pattern) that takes two strings, a message and a pattern, and returns a string produced by replacing the non-whitespace characters in the pattern with the non-whitespace characters in the message (where any leading or trailing newlines in the pattern are first removed). As a first example:
| 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
|
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