CS110-Higher-Order-Functions

Requirement

In the problems from the last request, we did a bit of rudimentary Scrabble scoring. Your ultimate task here is to write a function that takes as input a rack - a list of letters - and returns the highest scoring word that can be made with those letters. This is the key ingredient in a computerized Scrabble game!

In this problem, you may use recursion as well as the built-in higher-order functions map, filter, and reduce. In fact, if you write a function that needs to process a long list (e.g. a dictionary) with more than a few hundred items in it, you are much better off letting map, filter, or reduce cruise through those lists since they have been optimized to work very fast.

There will be a few places here where using anonymous functions (lambda) is probably the cleanest and nicest way to do business. Use it when it’s the cleanest way to proceed.

One of the objectives of this problem is to have you think about designing a more complicated program that involves several functions. You have complete autonomy in deciding what functions to write in order to reach the ultimate goal (see more on the two required functions below). Try to think carefully about which functions you will need and try to implement those functions so that they are as simple and clean as possible. Part of your score on this problem will be based on the elegance of your overall design and individual functions.

Our solution has fewer than 9 functions, two of which we modified from our solution to request 1. The remaining 7 (or so) functions range from one to four lines of code per function. While you are not required to have the same number of functions and you may have a few slightly longer functions, this is intended to indicate that there is not much code that needs to be written here!

Be sure to have a comment at the top of the file with your name(s), the filename, and the date. Any function that is even a bit complicated should at least have a short comment explaining to the reader how this function works. Also, include a docstring for every function that you write.

All of your work should go into a single file named scrabble.py. Any tests and their results should go into this file, commented out of course. Make sure your file loads without producing any output. We will test your program by loading this file and testing your functions. Be sure they are properly named.

  1. Firstly, rewrite your definition of letterScore(letter, scoreList> so it makes use of the built-in function filter.
  2. Secondly, rewrite your definition of wordScore(word, scoreList) so that it makes use of the built-in map and reduce.
  3. As an exercise in writing list comprehensions, define the following variables, using expressions made up of list comprehensions:
    words – all of the words in the list Dictionary.
    twoLetterWords – all of the words in the list Dictionary having exactly two letters.
    lengths – a list of only the lengths of all words in Dictionary.
  4. Now the main problem.
    A bit later in this problem, we’ll give you a fairly large dictionary of English words to use. For now, we recommend that you use the following tiny dictionary during the course of testing and development. Include this line near the top of your file. Now, it’s a global variable that can be used by any of your functions in that file. Similarly, you should include the scrabble letter score list from request 1.

Don’t try to change the values of these global variables! As we’ll see soon, that can make Python angry. However, there’s no need to change the contents of the Dictionary or the scrabble letter scores during execution of the program.

The details

Ultimately, there are two functions that we will be testing.

scoreList(Rack) takes as input a Rack which is a list of lower-case letters and returns a list of all of the words in the global Dictionary that can be made from those letters and 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 the tiny Dictionary above:

1
2
3
4
>>> scoreList(["a", "s", "m", "t", "p"])
[['a', 1], ['am', 4], ['at', 2], ['spam', 8]]
>>> scoreList(["a", "s", "m", "o", "f", "o"])
[['a', 1], ['am', 4], ['foo', 6]]

The order in which the words are presented is not important.

bestWord(Rack) takes as input a Rack as above and returns a list with two elements: the highest possible scoring word from that Rack followed by its score. If there are ties, they can be broken arbitrarily. Here is an example, again using the tiny Dictionary above:

1
2
>>> bestWord(["a", "s", "m", "t", "p"])
['spam', 8]

Aside from these two functions, all of the other helper functions are up to you! Some of those helper functions may be functions that you wrote in request 1 or slight variants of those functions. However, 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 appropriate - they are powerful and they are optimized to be very fast.

Test each function carefully with small test inputs before you proceed to write the next function. This will save you a lot of time and aggravation!

A reminder about in: 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:

1
2
3
4
if S[0] in Rack:
# if True you will end up here
else:
# if False you will end up here

That in feature is very handy. Now, recursion will let you do the rest of the work without much effort on your part!
Although we don’t expect that you will end up doing huge recursive calls, you should know that Python can get snippy when there are a lot of recursive calls. By default, Python generally complains when there are 1000 or more recursive calls of the same function. To convince Python to be friendlier, you can use the following at the top of your file.

1
import sys

sys.setrecursionlimit(10000) # Allows up to 10000 recursive calls; the maximum permitted ranges from system to system

Trying it with a bigger dictionary

Finally, if you want to test out a big dictionary (it’s more fun than the little one above), you can download the file. It contains a large list of words (a bit over 4000 words). The list is still called Dictionary. After you have downloaded the file and placed it in the same directory as your Python file, you should do the following at the top of your Python file:

1
from dict import *

Now, you can refer to Dictionary as a global variable. This is much nicer than cutting-and-pasting that very large list into your Python file. Here are some examples using that Dictionary.

1
2
>>> scoreList(['w', 'y', 'l', 'e', 'l', 'o'])
[['leo', 3], ['low', 6], ['lowly', 11], ['ow', 5], ['owe', 6], ['owl', 6], ['we', 5], ['well', 7], ['woe', 6], ['yell', 7], ['yo', 5]]

Notice that “yellow” is not in this dictionary. We “cropped” off words of length 6 or more to keep the dictionary from getting too large. Finally, here is an example of bestWord in action:

1
2
3
4
5
6
>>> bestWord(['w', 'y', 'l', 'e', 'l', 'o'])
['lowly', 11]
>>> bestWord(["s", "p", "a", "m", "y"])
['may', 8]
>>> bestWord(["s", "p", "a", "m", "y", "z"])
['zap', 14]

And an even bigger dictionary!

Want to try this with an even bigger dictionary? We’ve got one! The bad news is that it causes IDLE to crash on some computers. However, if you are running Python without IDLE (e.g. from the Unix shell) then this dictionary should work just fine!

1
from bigdict import *

The dictionary is still called Dictionary and it is available as a global variable. Here is an example of it in use:

1
2
>>> scoreList(['a', 'b', 'v', 'x', 'y', 'y', 'z', 'z', 'z'])
[['ab', 4], ['aby', 8], ['ax', 9], ['ay', 5], ['ba', 4], ['bay', 8], ['by