Created
December 7, 2011 05:47
-
-
Save zahlman/1441624 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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