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: # # Date Last Modified:
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:
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.
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.
import time start = time.time() ... # computation I'd like to time end = time.time() print("This computation took %2.3f seconds" % (end - start))
> 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. Made 611180 comparisons. 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. Made 12978244 comparisons. 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. Made 14756720 comparisons. 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. Made 38868873 comparisons. Enter a scrambled word (or EXIT): valley Found 720 permutations; 360.0 unique permutations Found word: valley Solving this jumble took 0.02574 seconds Checked 1 permutations. Made 59663 comparisons. Enter a scrambled word (or EXIT): exit Thanks for playing! Goodbye.