Skip to content

Instantly share code, notes, and snippets.

@zahlman
Created December 7, 2011 05:47
Show Gist options
  • Select an option

  • Save zahlman/1441624 to your computer and use it in GitHub Desktop.

Select an option

Save zahlman/1441624 to your computer and use it in GitHub Desktop.
from collections import Counter
from sys import argv
letters = Counter(argv[2:])
letter_count = len(argv) - 2
with file(argv[1]) as f: words = [word.strip() for word in f] # one word per line
longest = max(len(word) for word in words)
for i in reversed(xrange(min(longest, letter_count) + 1)):
possible_words = [
word
for (histogram, word) in (
(Counter(word), word) for word in words if len(word) == i
)
if all(histogram[letter] <= letters[letter] for letter in histogram)
]
if possible_words:
print possible_words
break
else:
print "No words can be made with those letters."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment