Skip to content

Instantly share code, notes, and snippets.

@sixthgear
Created December 7, 2011 03:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sixthgear/1441352 to your computer and use it in GitHub Desktop.
Save sixthgear/1441352 to your computer and use it in GitHub Desktop.
def check_word(word, letters):
"""
Check if a specific word can be created from the set of letters.
"""
for w in word:
pos = letters.find(w)
if pos == -1:
return False
letters = letters[:pos] + letters[pos+1:]
return True
def find_words(file, letters):
"""
For a given dictinary file, find the longest words that can be created from
the given set of letters.
"""
words = []
results = []
longest = 0
for word in open(file):
word = word.strip()
if len(word) < longest:
continue
if check_word(word, letters):
if len(word) > longest:
longest = len(word)
results = [word]
else:
results.append(word)
return results
if __name__ == '__main__':
import sys
if len(sys.argv) < 3:
print('Usage: scrabble.py dictionary [letter... ]')
sys.exit()
for r in find_words(sys.argv[1], str.join('', sys.argv[2:])):
print(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment