### CS313E Assignment 6: Jumble Step 1 (10 points)

Due: by Monday, October 29, 2012 by 11:59pm

Your program listing should have the following information.

```#  Files: Permutations.py, Wordlist.py, Solver.py
#
#  Description:
#
#  Student's Name:
#
#  Student's UT EID:
#
#  Course Name: CS 313E
#
#  Date Created:
#
```

• You must submit your .py files.
• Your .py file should have the header with the proper documentation.
• You should be submitting your .py file through the web based turnin program. We will not accept files e-mailed to us.
• Your code must compile before submission.

### The Assignment

You can do this assignment with one other student. Only submit one version, but be sure to indicate on your submission what students participated.

This is the first of a series of assignments aimed at solving the Jumbles that you see in the paper or online: Jumble website. The idea is this: given a series of scrambled words, unscramble them. There's also another component to use some letters to solve a riddle; we're not going to be dealing with that.

The basic algorithm is as follows: given a scrambled word (e.g., ACTMH), you generate permutations of the word and check your wordlist until you either find the word (MATCH), or you exhaust the list of permutations, in which case you report failure. For this first program in this series, you'll be doing part of this. In particular, implement the following steps:

Implement a Wordlist abstract data type with the following interface:
• __init__( ): create an empty Wordlist.

• __len__( ): number of words in the Wordlist.

• isEmpty(): is the Wordlist empty?

• addWord( word, f ): add a word to the Wordlist, if it satisfies filter function f.

• addWordsFromFile( filename, f ): given an external file containing words, add to the Wordlist any that satisfy f. Assume that the file contains one word per line, and that the words do not repeat.

• removeWord( word ): remove the word from the Wordlist.

• findWord( word ): return a pair ( Boolean, integer ) indicating whether or not the word is in the wordlist and how many comparison you performed to determine.
The filter function f the name of a Boolean-valued function on words, e.g., only returns True if the word has length 5 or 6.

For this first version, simply store the words in a Python list and implement findWord via a linear search of the list. You will have to implement it with a loop to count the number of comparisons; that is, don't just do something like word in self._words. When you create a new Wordlist, print out how many words were stored and how long it took.

#### Write a Top-Level Driver

Your main program should create an empty Wordlist and populate it from the words in this file: Unordered word list, using for the filter one that admits words of 5 or 6 letters. Print out the number or words in the wordlist and how long it took to generate the Wordlist.

Following that initial setup, your main program should enter a loop. In each iteration prompt the user for an input string, generate permutations of the string, and test whether any permutation is in the Wordlist. If so, return that as your answer along with statistics of the search (how many comparisons you made and how long it took). If no permutation is in the Wordlist, report failure and the statistics of the search. Loop until the user enters "exit." User input should not be case sensitive. Sample output is below.

Some code to generate permutations of a string is here: Permutation code. Here allPerms( string ) will give you all of the permutations of string and howManyPerms( string ) returns a pair: (total number of permutations, unique permutations).

This file Jumbles contains some recent jumbles from the Austin American Statesman. You can use these as sample input to test your program.

#### Computing Timing Information

To time some Python code, first import the time module. The method time.time() will give you the current time. If you record that in a variable at the start and end of the computation, the difference of the two will be the elapsed time in seconds. For example,
```import time

start = time.time()

...  # computation I'd like to time

end = time.time()
print("This computation took %2.3f seconds" % (end - start))
```

### Sample run of the Program

Don't worry if your comparison counts don't match these. Also, I didn't filter the wordlist, so yours will have many fewer words.

```
> python Solver.py
Using flat unsorted wordlist.
The Wordlist contains  113810  words.
Building the Wordlist took 0.472 seconds

Enter a scrambled word (or EXIT):  torll
Found  120 permutations;  60.0 unique permutations
Found word: troll
Solving this jumble took 0.16808 seconds
Checked  6  permutations.

Enter a scrambled word (or EXIT):  denrt
Found  120 permutations;  120.0 unique permutations
Found word: trend
Solving this jumble took 3.39772 seconds
Checked  115  permutations.

Enter a scrambled word (or EXIT):  gewhit
Found  720 permutations;  720.0 unique permutations
Found word: weight
Solving this jumble took 4.10399 seconds
Checked  130  permutations.

Enter a scrambled word (or EXIT):  yalelv
Found  720 permutations;  360.0 unique permutations
Found word: valley
Solving this jumble took 10.38837 seconds
Checked  342  permutations.