patternpythonMinor
Jumble solver algorithm
Viewed 0 times
solverjumblealgorithm
Problem
I need to write code for the given problem:
I'm provided with a word and I need to find all the possible combination of it that matches with a given list of words in a file.
Here's my code. Can I make it much better? I'm sure it can be done. Please offer suggestions.
```
dict = {}
file = open("/usr/share/dict/words", "r")
for word in file: #Iterate through every word in the dictionary
word = word.strip().lower() #Strip newlines and send to lowercase
sorted_word = ''.join(sorted(word)) #Alphabetically sort the word
if sorted_word in dict: #Check if sorted_word is already a key
if word not in dict[sorted_word]: #Make sure word is not already in the list under the key sorted_word
dict[sorted_word].append(word) #Add to list under the key sorted_word
else: #If not in dictionary
dict[sorted_word] = [word] #Create new list with one entry
while(True): #Loop until quit is typed
jumble = raw_input("Enter a jumble to decode or 'quit': ") #Get input
jumble = jumble.lower() #Send jumble to lower
if(jumble == "quit"): #Quit if quit is typed
break
jumble = ''.join(sorted(jumble)) #Sort jumble alphabetical
if jumble in dict: #If sorted jumble exists in dictionary
results = dict[jumble] #Get list of words that match jumble
for result in results: #Loop through list printing results
print result, #Trailing , desig
I'm provided with a word and I need to find all the possible combination of it that matches with a given list of words in a file.
Here's my code. Can I make it much better? I'm sure it can be done. Please offer suggestions.
```
dict = {}
file = open("/usr/share/dict/words", "r")
for word in file: #Iterate through every word in the dictionary
word = word.strip().lower() #Strip newlines and send to lowercase
sorted_word = ''.join(sorted(word)) #Alphabetically sort the word
if sorted_word in dict: #Check if sorted_word is already a key
if word not in dict[sorted_word]: #Make sure word is not already in the list under the key sorted_word
dict[sorted_word].append(word) #Add to list under the key sorted_word
else: #If not in dictionary
dict[sorted_word] = [word] #Create new list with one entry
while(True): #Loop until quit is typed
jumble = raw_input("Enter a jumble to decode or 'quit': ") #Get input
jumble = jumble.lower() #Send jumble to lower
if(jumble == "quit"): #Quit if quit is typed
break
jumble = ''.join(sorted(jumble)) #Sort jumble alphabetical
if jumble in dict: #If sorted jumble exists in dictionary
results = dict[jumble] #Get list of words that match jumble
for result in results: #Loop through list printing results
print result, #Trailing , desig
Solution
Hum, first of all, there are way too many comments. People simply won't read them, you should only keep the meaningful ones. For example, you could remove:
Anybody who knows how Python dictionaries work will know what this lign is doing, the code is explicit by itself. Instead of always repeating what your
Second point, avoid to name a variable
Also, you open your file once, but never close it. You should close it once you have filled your
if sorted_word in dict: #Check if sorted_word is already a keyAnybody who knows how Python dictionaries work will know what this lign is doing, the code is explicit by itself. Instead of always repeating what your
dict is doing, you should just explain once what it does when you declare it; the code will tell the rest. You should only explain once at the beginning how your algorithm work and let your code tell the rest. Honestly, you could remove almost all of your comments. Python is designed to be readable and it is.Second point, avoid to name a variable
dict: it is already the name of a built-in type. It's bad pratice and can be confusing for people who will try to read your code.Also, you open your file once, but never close it. You should close it once you have filled your
dict. You could use the with open('...', 'r') as f: construct to have your file closed automatically at the end of the with block, therefore, you won't even have to remember to close it. Also, file, like dict is also the name of a built-in type in Python, find another name :)Code Snippets
if sorted_word in dict: #Check if sorted_word is already a keyContext
StackExchange Code Review Q#37034, answer score: 6
Revisions (0)
No revisions yet.