Due Tuesday 28-Jan, at 9: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, dictionaries, try/except, classes, 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.)
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.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.
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.
mostFrequentLetters(s)
, that takes a string
s
, and ignoring case (so "A" and "a" are treated the same), returns
a lowercase string containing the letters of s in most frequently used order.
(In the event of a tie between two letters, follow alphabetic order.)
So:
mostFrequentLetters("We Attack at Dawn")returns "atwcdekn". Note that digits, punctuation, and whitespace are not letters! Also note that seeing as we have not yet covered lists, sets, maps, or efficiency, you are not expected to write the most efficient solution. (And you should not use lists, sets, or maps in your solution.) Finally, if s does not contain any alphabetic characters, the result should be the empty string ("").
longestCommonSubstring(s1, s2)
, that takes two possibly-empty strings and returns the longest string that occurs in both strings (and returns the empty string if either string is empty). For example:
longestCommonSubstring("abcdef", "abqrcdest") returns "cde" longestCommonSubstring("abcdef", "ghi") returns "" (the empty string)If there are two or more longest common substrings, return the lexicographically smaller one (ie, just use "<" to compare the strings). So, for example:
longestCommonSubstring("abcABC", "zzabZZAB") returns "AB" and not "ab"
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: