Skip to content

Instantly share code, notes, and snippets.

@acspike
Created November 21, 2012 14:20
Show Gist options
  • Save acspike/4125063 to your computer and use it in GitHub Desktop.
Save acspike/4125063 to your computer and use it in GitHub Desktop.
cheating at letterpress
class word(object):
def __init__(self,w):
self.w = w
self.key = "%02d%s" % (len(w),w)
self.set = self._makeset(w)
def __str__(self):
return self.w
def __cmp__(self,other):
return cmp(other.key,self.key)
def __repr__(self):
return self.w
def _makeset(self, w):
w = list(w)
w.sort()
last = ''
c = 0
enum = []
for x in w:
if x == last:
c += 1
else:
last = x
c = 1
enum.append(x+str(c))
return set(enum)
def sub(self, other):
return self.set <= other.set
def sup(self, other):
return self.set >= other.set
wordlist = [word(x) for x in open('azwords.txt','r').read().split()]
class puzzle(object):
def __init__(self, p):
self.p = word(p)
self.words = [x for x in wordlist if self.p.sup(x)]
self.words.sort()
def has(self, w):
w = word(w)
return [x for x in self.words if x.sup(w)]
def top(self, n=10):
return self.words[:n]
if __name__ == '__main__':
p = puzzle('tphakmbpthwwnchznaredzvbi')
print p.top()
print p.has('zz')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment