Skip to content

Instantly share code, notes, and snippets.

@iolloyd
Created November 25, 2012 17:44
Show Gist options
  • Save iolloyd/4144512 to your computer and use it in GitHub Desktop.
Save iolloyd/4144512 to your computer and use it in GitHub Desktop.
ios letterpress solver
"""
To use just type ... python letterPressSolver.py <25 letter string of grid> <favourite letters>
"""
class Solver():
def __init__(self, grid):
wordList = open('/usr/share/dict/words').readlines()
self.wordList = [x.lower().strip() for x in wordList]
self.grid = list(grid)
self.grid.sort()
def solve(self):
return filter(lambda w: self.canBeMade(w), self.wordList)
def solveBetter(self, letters):
wordList = self.solve()
return filter(lambda w, letters=letters: self.canBeMade(letters, list(w)), wordList)
def canBeMade(self, word, words=[]):
word.strip()
wordLetters = list(word)
wordLetters.sort()
if words == []: words = self.grid[:]
return self.canBeMade_(wordLetters[:], words)
def canBeMade_(self, small, big):
if small == [] : return True
if big == [] : return False
if small[0] < big[0] : return False
if small[0] > big[0] : return self.canBeMade_(small, big[1:])
if small[0] == big[0]: return self.canBeMade_(small[1:], big[1:])
return self.canBeMade_(small, big[1:])
if __name__ == "__main__":
import sys
grid = sys.argv[1]
betterLetters = sys.argv[2]
solver = Solver(grid)
better = solver.solveBetter(betterLetters)
for w in better: print w
@iolloyd
Copy link
Author

iolloyd commented Nov 25, 2012

Please be aware that solver.solve() is also available

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