Skip to content

Instantly share code, notes, and snippets.

@amundo
Created February 10, 2010 16:38
Show Gist options
  • Save amundo/300516 to your computer and use it in GitHub Desktop.
Save amundo/300516 to your computer and use it in GitHub Desktop.
find all the words that can be built using the letters in a list, as in Scrabble™.
#!/usr/bin/env python
# coding: utf-8
"""
scrabblewords.py - find all the words that can be built using the letters
in a list, as in Scrabble™.
"""
import codecs
words = codecs.open('/usr/share/dict/words',mode='rU', encoding='utf-8').readlines()
words = [word.strip() for word in words]
def has_all_letters(this,that):
"""if all the letters of this word are also in that word, return true"""
return set(this).issubset(that)
def scrabblewords(tiles, dictionary=words):
for word in dictionary:
if has_all_letters( word, tiles) and \
all([(word.count(letter) <= tiles.count(letter)) for letter in word]):
print word
if __name__ == "__main__":
import sys
tiles = sys.argv[1]
scrabblewords(tiles, words)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment