Question
scrabbleScores = [ [a, 1], [b, 3], [c, 3], [d, 2], [e, 1], [f, 4], [g, 2], [h, 4], [i, 1], [j, 8], [k, 5],
scrabbleScores = [ ["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1], ["f", 4], ["g", 2], ["h", 4], ["i", 1], ["j", 8], ["k", 5], ["l", 1], ["m", 3], ["n", 1], ["o", 1], ["p", 3], ["q", 10], ["r", 1], ["s", 1], ["t", 1], ["u", 1], ["v", 4], ["w", 4], ["x", 8], ["y", 4], ["z", 10] ]
nanoDictionary = ["a", "am", "at", "apple", "bat", "bar", "babble","can", "foo", "spam", "spammy", "zzyzva"]
Ultimately, there are two functions that we will be testing.
scoreList(rack, dictionary) has two arguments: a rack, which is a list of lower-case letters, and dictionary, which is a list of legal words.
The scoreList function returns a list of all of the words in the Dictionary argument that can be made from those letters, together with the score for each one. Specifically, this function returns a list of lists, each of which contains a string that can be made from the Rack and its Scrabble score. Here are some examples using nanoDictionaryabove:
In [1]: scoreList(["a", "s", "m", "t", "p"], nanoDictionary) Out[1]: [['a', 1], ['am', 4], ['at', 2], ['spam', 8]] In [2]: scoreList(["a", "s", "m", "o", "f", "o"], nanoDictionary) Out[2]: [['a', 1], ['am', 4], ['foo', 6]]
The order in which the words are presented is not important.
bestWord(rack, dictionary) accepts a Rack and Dictionary as above and returns a list with two elements: the highest-scoring possible word from that Rack, followed by its score. If there are ties, they can be broken arbitrarily. Here is an example, again using nanoDictionary above:
In [1]: bestWord(["a", "s", "m", "t", "p"], nanoDictionary) Out[1]: ['spam', 8]
Aside from these two functions, use the following functions to implement them:
You might find it useful to use a strategy in which you write a function that determines whether or not a given string can be made from the given Rack list. Then, another function can use that function to determine the list of all strings in the dictionary that can be made from the given Rack. Finally, another function might score those words.
Remember to use map, reduce, or filter where appropriatethey are powerful and they are optimized to be very fast.
Test each function carefully with small test arguments before you proceed to write the next function. This will save you a lot of time and aggravation!
A reminder about Python's in operator:
Imagine that you are writing a function that attempts to determine if a given string S can be made from the letters in the Rack list. You might be tempted to scramble ("permute," to use a technical term) the Rack in every possible way as part of this process, but that is more work than necessary. Instead, you can test if the first symbol in your string, S[0], appears in the rack with the statement:
if S[0] in Rack: # if True you will end up here else: # if False you will end up here
This in operator is very handy. From here, recursion will let you do the rest of the work without much effort on your part!
def letterScore(letter, scorelist): Returns the corresponding score for the parameter letter, according to the list parameter scorelist" if scorelist [j: elif lettrscorelist[e][e]: return letterScore(letter, scorelist[1:]) return 0 return scorelist[e][1] def wordscore(s, scorelist): Returns the score of parameter S according to the list parameter scorelist. If there is a part of the string that does not appear in the scorelist, program will return the combined score of the letters in scorelist" return elif letterScore(S[e], scorelist): returne return letterscore(s[e], scorelist)+wordscore(s[1:], scorelist)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started