Skip to content

Instantly share code, notes, and snippets.

@kevinburke
Created February 10, 2011 04:09
Show Gist options
  • Save kevinburke/819931 to your computer and use it in GitHub Desktop.
Save kevinburke/819931 to your computer and use it in GitHub Desktop.
import copy
import pdb
import re
file_loc = '/usr/share/dict/words'
words = {}
with open(file_loc) as f:
word = f.readline()
while word != '':
words[word[:-1]] = True
word = f.readline()
def test_for_blanks(a_list):
for i in a_list:
if i != '.':
return False
return True
the_word = ''
while True:
print "What's the word?"
the_input = raw_input()
nums = ['4','5','6','7','8']
nums.reverse()
if re.search('^[0-9]+$', the_input):
nums = the_input.split()
elif the_input == "no":
break
else:
the_word = the_input
#pretty hacky
word_chars = list(the_word)
word_chars.sort(reverse=True)
blanks = 0
for i in word_chars:
if i == '.':
blanks += 1
for num in nums:
valid = []
count = 0
for word in words.iterkeys():
count += 1
if len(word) == int(num):
chars = list(word)
new_word = copy.copy(word_chars)
errors = 0
for (i, letter) in enumerate(chars):
try:
new_word.remove(letter)
except ValueError:
errors += 1
if errors <= blanks:
valid.append(word)
valid.sort()
for i in valid:
print i
print count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment