Do not use sets, dictionaries, try/except, classes, or recursion this week.
The autograder will reject your submission entirely if you do.
-
vowelCount(s) [10 pts]
Write the function 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:
assert(vowelCount("Abc def!!! a? yzyzyz!") == 3) # two a's and one e
-
rotateStringLeft(s, n) [10 pts]
Note: To receive credit, do not use loops on this problem.
Write the function 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:
assert(rotateStringLeft('abcd', 1) == 'bcda')
assert(rotateStringLeft('abcd', -1) == 'dabc')
-
isRotation(s, t) [10 pts]
Write the function 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.
Hint: rotateStringLeft may be helpful here.
-
averageWithPolicy(scores) [20 pts]
Consider the following excerpt from a course syllabus:
In order to reward improvement, I will replace one quiz score that is immediately followed by two higher scores. So, if you have a quiz that goes very badly, but your next two quizzes are each better than that bad quiz, I will replace that low quiz with the higher of the two scores immediately following it. If you have multiple "bad" quizzes that meet the criteria, I will replace the one that maximizes your point gain.
Write the non-destructive function averageWithPolicy(scores) which takes as an argument a list of scores and returns the average of those scores after applying this policy.
Consider the following examples:
assert(averageWithPolicy([42, 20, 40, 35, 50, 65]) == 47.0)
assert(averageWithPolicy([25, 30, 20, 45, 40, 60, 70, 80, 90, 100]) == 59.0)
The first example is 47
because the quiz to be replaced is the 35
, and it will be replaced by a 65
. That means we are finding the average of [42, 20, 40, 65, 50, 65]
The second example is 59
because the quiz to be replaced is the 40
, and it will be replaced by a 70
.
That means we are finding the average of [25, 30, 20, 45, 70, 60, 70, 80, 90, 100]
Hint: You don’t actually need to find and replace anything in the list. Instead, you just need to find the largest difference among all candidates that meet the "lower before two higher" criteria. I recommend you create a helper function to do that.
-
nondestructiveRemoveRepeats(L) [10 pts]
Important Note: to receive any credit for this problem,
you may not simply create a copy of L and then call the
destructiveRemoveRepeats function below. Instead, you must
create the resulting list from scratch here. Also, note
that the autograder has no way to check this, so our CA's will
check this manually after the hw deadline.
Write the function nondestructiveRemoveRepeats(L), which
takes a list L and nondestructively returns a new list in
which any repeating elements in L are removed. For example:
assert(nondestructiveRemoveRepeats([1, 3, 5, 3, 3, 2, 1, 7, 5]) ==
[1, 3, 5, 2, 7])
Also:
L = [1, 3, 5, 3, 3, 2, 1, 7, 5]
assert(nondestructiveRemoveRepeats(L) == [1, 3, 5, 2, 7])
assert(L == [1, 3, 5, 3, 3, 2, 1, 7, 5]) # nondestructive!
Note that the values in the resulting list occur in the order they
appear in the original list, but each occurs only once in the result.
Also, since this is a nondestructive function, it returns the
resulting list.
-
destructiveRemoveRepeats(L) [10 pts]
Important Note: this is the analog of the previous
important note. Here, to receive any credit for this problem,
you may not simply call nondestructiveRemoveRepeats(L)
and then somehow remove all the elements in L and then
append the elements from that call. Instead, you must
destructively modify the list as you go. Also, again, note
that the autograder has no way to check this, so our TA's will
check this manually after the hw deadline.
Write the function destructiveRemoveRepeats(L), which implements the
same function destructively. Thus, this function should directly modify
the provided list to not have any repeating elements. Since this
is a destructive function, it should not return any value at all
(so, implicitly, it should return None).
For example:
L = [1, 3, 5, 3, 3, 2, 1, 7, 5]
assert(destructiveRemoveRepeats(L) == None)
assert(L == [1, 3, 5, 2, 7]) # destructive!
-
bestScrabbleScore(dictionary, letterScores, hand) [30 pts]
Background: In a Scrabble-like game, players each have a hand, which is a list of lowercase letters. There is also a dictionary, which is a list of legal words (all in lowercase letters). And there is a list of letterScores, which is length 26, where letterScores[i] contains the point value for the ith character in the alphabet (so letterScores[0] contains the point value for 'a'). Players can use some or all of the tiles in their hand and arrange them in any order to form words. The point value for a word is 0 if it is not in the dictionary, otherwise it is the sum of the point values of each letter in the word, according to the letterScores list (pretty much as it works in actual Scrabble).
In case you are interested, here is a list of the actual letterScores for Scrabble:
letterScores = [
# a, b, c, d, e, f, g, h, i, j, k, l, m
1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3,
# n, o, p, q, r, s, t, u, v, w, x, y, z
1, 1, 3,10, 1, 1, 1, 1, 4, 4, 8, 4,10
]
Note that your function must work for any list of letterScores as is provided by the caller.
With this in mind, write the function bestScrabbleScore(dictionary, letterScores, hand) that takes 3 lists -- dictionary (a list of lowercase words), letterScores (a list of 26 integers), and hand (a list of lowercase characters) -- and returns a tuple of the highest-scoring word in the dictionary that can be formed by some arrangement of some subset of letters in the hand, followed by its score. In the case of a tie, the first element of the tuple should instead be a list of all such words in the order they appear in the dictionary. If no such words exist, return None.
Note: you should definitely write helper functions for this problem! In fact, try to think of at least two helper functions you could use before writing any code at all.
Another note: there is no fixed dictionary here. Each time we call the function, we may provide a different dictionary! It may contain 100 words or perhaps 100,000 words.