Python Program
This Python program uses hashing, dictionaries, and loops to search through an English word database file that finds matches for the user's inputted scrambled word. It also returns the computing time it took to find the match and how many comparisons between similar words/hashes it took to arrive at the match.
import os.path
import time
ILLEGALCHAR = "Word contains illegal characters. Try again"
INVALIDVALUE = "Word must be 5 or 6 characters long. Try again"
CANTSOLVE = "Sorry. I can't solve this jumble! Try again."
PRIMES = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113 ]
# Hash function
def hash(word,divisor = 10007):
# Creates the initial product variable
product = 1
# Gets the index of each letter in word and gets the corresponding value in PRIMES, then multiplies them
for i in word.lower():
index = ord(i)-ord("a")
product *= PRIMES[index]
return product%divisor
def createDicts(filename):
# Opens the wordlist text file
infile = open(filename, "r")
# Creating dictionaries variables for 5 and 6 letter words
dict5 = {}
dict6 = {}
all_words = []
for line in infile:
all_words.append(line.strip())
for line in all_words:
if len(line) == 5:
if hash(line) in dict5.keys():
dict5[hash(line)].append(line)
else:
dict5[hash(line)] = [line]
elif len(line) == 6:
if hash(line) in dict6.keys():
dict6[hash(line)].append(line)
else:
dict6[hash(line)] = [line]
else:
pass
return dict5,dict6
def main():
dict5,dict6 = createDicts("wordslist.txt")
print("Welcome to Jumble!\n\nTo play, please input a 5-letter or\n6-letter scrambled word and the program\nwill return an unscrambled word.")
while True:
jumble_input = input("\nEnter a scrambled word (or EXIT): ").strip()
if jumble_input.upper() == "EXIT" or jumble_input.upper() == "HALT" or jumble_input.upper() == "STOP":
print("Thanks for playing! Goodbye.")
return
elif not jumble_input.isalpha():
print(ILLEGALCHAR)
continue
elif len(jumble_input) == 5:
start = time.time()
if hash(jumble_input) in dict5.keys():
match_found = 0
for word in dict5[hash(jumble_input)]:
if sorted(word) == sorted(jumble_input):
end = time.time()
comparisons = len(dict5[hash(jumble_input)])
match_found += 1
print(f"Found word: {word}")
print("Solving this jumble took %2.5f seconds" % (end - start))
print(f"Made {comparisons} comparisons.")
break
else:
pass
if match_found == 0:
end = time.time()
comparisons = len(dict5[hash(jumble_input)])
print(CANTSOLVE)
print("Solving this jumble took %2.5f seconds" % (end - start))
print(f"Made {comparisons} comparisons.")
continue
else:
continue
else:
end = time.time()
print(CANTSOLVE)
print("Solving this jumble took %2.5f seconds" % (end - start))
print(f"Made 0 comparisons.")
continue
elif len(jumble_input) == 6:
start = time.time()
if hash(jumble_input) in dict6.keys():
match_found = 0
for word in dict6[hash(jumble_input)]:
if sorted(word) == sorted(jumble_input):
end = time.time()
comparisons = len(dict6[hash(jumble_input)])
match_found += 1
print(f"Found word: {word}")
print("Solving this jumble took %2.5f seconds" % (end - start))
print(f"Made {comparisons} comparisons.")
break
else:
pass
if match_found == 0:
end = time.time()
comparisons = len(dict6[hash(jumble_input)])
print(CANTSOLVE)
print("Solving this jumble took %2.5f seconds" % (end - start))
print(f"Made {comparisons} comparisons.")
continue
else:
continue
else:
end = time.time()
print(CANTSOLVE)
print("Solving this jumble took %2.5f seconds" % (end - start))
print(f"Made 0 comparisons.")
continue
else:
print(INVALIDVALUE)
continue
main()
Press Run to start or restart the program.
Use text inputs to interact with the game.