Skip to content

Instantly share code, notes, and snippets.

@martin-wintz
Last active December 15, 2015 08:09
Show Gist options
  • Save martin-wintz/5228765 to your computer and use it in GitHub Desktop.
Save martin-wintz/5228765 to your computer and use it in GitHub Desktop.
Little word puzzle solver: save as python script and run with -h for usage
import re
import argparse
def getSortedLetterList(wordList):
# Calculate the frequency with which characters appears in the dictionary
letterCount = {}
totalCount = 0
for word in wordList:
word = word.lower()
word = re.sub('\W', '', word)
for letter in word:
totalCount += 1
if letter in letterCount.keys():
letterCount[letter] += 1
else:
letterCount[letter] = 1
letterFrequency = {}
for letter in letterCount:
letterFrequency[letter] = letterCount[letter] / float(totalCount)
for letter in sorted(letterCount, key=letterFrequency.get):
print letter + ": " + str(letterFrequency[letter])
return sorted(letterCount, key=letterFrequency.get)
def findUncommonWords(allowedLetterRank = 15, filename = '2of4brif.txt'):
wordList = open(filename).read().splitlines()
letterList = getSortedLetterList(wordList)
# Look for words including the top "allowedLetterRank" uncommon letters
regex = re.compile('^[' + ''.join(letterList[0:allowedLetterRank]) + ']+$')
for word in wordList:
if regex.match(word):
print word
def main():
parser = argparse.ArgumentParser(description="Find words with only uncommon letters." +
" Specify the range of letters to use (starting from the least common letter)" +
" using -r. Lists the frequency rankings of the letters first then the words found.")
parser.add_argument("-r", "--rank", type=int, default=15,
help="Find words within this range of uncommon letters")
parser.add_argument("-f", "--file", default="2of4brif.txt",
help="Filename of dictionary (must be in the same directory)")
args = vars(parser.parse_args())
rank = args["rank"]
filename = args["file"]
findUncommonWords(rank, filename)
if __name__ == '__main__':
main()
@martin-wintz
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment