Created
April 8, 2011 02:40
-
-
Save kevinburke/909183 to your computer and use it in GitHub Desktop.
Words With Friends helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
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 | |
d = { 'a':1, 'b':3, 'c':4, 'd':2, 'e':1, 'f':4, 'g':4, 'h':4, 'i':1, | |
'j':10, 'k':5, 'l':1, 'm':4, 'n':2, 'o':1, 'p':4, 'q':10, 'r':1, | |
's':1, 't':1, 'u':1, 'v':5, 'w':4, 'x':8, 'y':4, 'z':10} | |
def scrabble_score(a_word): | |
'''return the scrabble score of a word''' | |
return sum([d[i] for i in str.lower(a_word)]) | |
def main(): | |
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(ScrabbleWordScore(word, scrabble_score(word))) | |
for i in sorted(valid): | |
print i | |
print count | |
class ScrabbleWordScore: | |
def __init__(self, word, score): | |
self.word = word | |
self.score = score | |
def __cmp__(self, other): | |
return cmp(other.score, self.score) | |
def __str__ (self): | |
return "".join([self.word, " ", str(self.score)]) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment