Skip to content

Instantly share code, notes, and snippets.

@federicobond
Last active October 13, 2015 13:48
Show Gist options
  • Save federicobond/4205392 to your computer and use it in GitHub Desktop.
Save federicobond/4205392 to your computer and use it in GitHub Desktop.
Small script to give you the longest words you can play in Letterpress for iOS. Used it for demonstration purposes. Cheating spoils the fun.
from collections import Counter
class Letterpress(object):
DICTIONARY = "Words/Words/en.txt"
def __init__(self, available):
"""Inialize letterpress object"""
self.words = self.build_words()
self.available = Counter(available.lower())
def build_words(self):
"""Build list of words"""
with open(self.DICTIONARY) as f:
words = f.read().splitlines()
filtered_words = [word for word in words if self.valid_word(word)]
return filtered_words
def valid_word(self, word):
"""Discard short words and proper nouns"""
return len(word) > 3 and word[0].islower()
def can_form(self, word):
"""Define words that can be formed with the available characters"""
word = Counter(word)
return all(word[k] <= self.available[k] for k in word)
def solve(self, count=200):
"""Search all possible words that can be formed and sort them by length"""
possible_words = [word for word in self.words if self.can_form(word)]
possible_words.sort(key=len, reverse=True)
return possible_words[:count]
def solve_filtered(self, letters, count=200):
"""Search for words with the given characters"""
return [word for word in self.solve() if
all(c in word for c in list(letters.lower()))]
if __name__ == "__main__":
from sys import argv
letterpress = Letterpress(argv[1])
print "Solution for board: " + argv[1].upper()
if len(argv) > 2:
print letterpress.solve_filtered(argv[2])
else:
print letterpress.solve()
# Run it with python letterpress.py ABCDEABCDEABCDE
# Optionally provide a second parameter to filter words containing specific letters
@federicobond
Copy link
Author

The official Letterpress dictionary can now be found at https://github.com/atebits/Words

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