Created
November 25, 2012 19:20
-
-
Save jonathanyee/4144857 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
import sys | |
if __name__ == '__main__': | |
# check command line argument for 25 letters | |
if len(sys.argv[1]) == 25: | |
listOfLetters = list(sys.argv[1]) | |
print listOfLetters | |
# better than readlines as this doesnt include \n | |
dictionary = open('/usr/share/dict/words', 'r').read().splitlines() | |
# split up dictionary words into 2d array by character | |
dictionaryArray = [] | |
for word in dictionary: | |
# only add words with at least 2 letters | |
if len(word) > 1: | |
dictionaryArray.append(list(word)) | |
possiblewords = [] | |
#need to check for duplicates | |
for wordArray in dictionaryArray: | |
dummystack = wordArray | |
for character in wordArray: | |
if character in listOfLetters: | |
dummystack.remove(character) | |
if len(dummystack) == 0: | |
possiblewords.append(''.join(wordArray)) | |
else: | |
break | |
# sort answers by length in descending order | |
possiblewords.sort(key=len, reverse=True) | |
for word in possiblewords[:10]: | |
print word | |
else : | |
print "Enter 25 letters without spaces" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment