Due: by Wednesday, November 28, 2012 by 11:59pm. Notice that I will be out of town from 11/27 to 11/29, so I'd suggest doing this one early, if you think you might need help.
Your program listing should have the following information.
# Files: Wordlist.py, HashSolver.py, README # # Description: # # Student's Name: # # Student's UT EID: # # Course Name: CS 313E # # Date Created: # # Date Last Modified:
This is the third in 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.
In the earlier versions, you needed to try all permutations of string to see if any were in the Wordlist. You can avoid permutations entirely by realizing the following: a solution is any word in the Wordlist that has exactly the same letters as the input (including duplications).
That means that you could solve the problem by examining each word in the Wordlist to see if it contains exactly the same letters. That would be a relatively slow linear search, but you'd only have to do it once for each jumble.
However, you can do even better with a bit of pre-processing of the Wordlist. Assuming you have a hash function that is invariant under permutation (i.e., all permutations of a string hash to the same value), you can store your Wordlist in a hash table. Then your input string should hash to the same bucket as the solution word. You just need to find it in there. To do that, you search the list of words in the bucket for one that has the same letters. If you find one, you're done. If not, there is no solution to the Jumble.
The HashedWordList class should override the following methods: __init__, addWord, If you didn't implement addWordsFromFile by repeatedly calling addWord, you may have to override addWordsFromFile also. You'll also be adding two new methods: findPerm, which replaces findWord, and loadFactor, which computes a pair containing the number of empty buckets and the average length of a (non-empty) bucket in your hash table. Unlike findWord, findPerm is looking for a string in the hash bucket with the same letters, not an exact match.
The interface for HashedWordList should be exactly the same as the interface for WordList, except that you're adding findPerm and not using findWord. Otherwise, the user of the class should not see any difference.
After created a hash table version of your Wordlist, accept strings from the user as before. When a user inputs a string S, you'll check to see if a permutation of S is in the Wordlist using the method findPerm. findPerm should perform the following steps:
To check that two strings have exactly the same letters (including duplicates), you can test the following:
sorted( str1 ) == sorted( str2 )The function sorted returns a list of characters in sorted order. There may be quicker ways to do this, but you shouldn't have many comparisons anyway.
As before, compute and print the statistics of the search (how many comparisons you made and how long it took). For this case, comparisons means the number of words you check in the bucket to which you hashed. Loop until the user enters "exit." User input should not be case sensitive. The output should be very nearly identical to that from Assignment 6 and Assignment 8.
felix:~/cs313e/python/newjumble> python HashSolver.py Using hash table wordlist. Creating wordlist The Wordlist contains 22633 words. There are 1668 empty buckets Non-empty buckets have an average length of 2.71411440221 Building the Wordlist took 0.656 seconds Enter a scrambled word (or EXIT): yodoz Found word: doozy Solving this jumble took 0.00010 seconds Made 1 comparisons. Enter a scrambled word (or EXIT): ulpem Found word: plume Solving this jumble took 0.00012 seconds Made 4 comparisons. Enter a scrambled word (or EXIT): tribte Found word: bitter Solving this jumble took 0.00010 seconds Made 2 comparisons. Enter a scrambled word (or EXIT): sluvia Found word: visual Solving this jumble took 0.00011 seconds Made 1 comparisons. Enter a scrambled word (or EXIT): exit Thanks for playing! Goodbye. felix:~/cs313e/python/newjumble>