Skip to content

Instantly share code, notes, and snippets.

@PM2Ring
Created April 5, 2017 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PM2Ring/dfc59f314fb18a0816c1bb7c796e9126 to your computer and use it in GitHub Desktop.
Save PM2Ring/dfc59f314fb18a0816c1bb7c796e9126 to your computer and use it in GitHub Desktop.
Alphagram Maker
#!/usr/bin/env python3
''' Transform words into 1-based indices of their letters in alphabetical order.
Eg 'APPLE' gets these indices: [(1, 'A'), (2, 'E'), (3, 'L'), (4, 'P'), (5, 'P')]
So we encode 'APPLE' as (1, 4, 5, 3, 2)
See http://stackoverflow.com/questions/43233004/regex-expression-to-find-words-where-you-know-the-alphabetical-order-of-the-lett
Written by PM 2Ring 2017.04.06
'''
import readline
def word_index(word):
d = {}
for i, c in enumerate(sorted(word), 1):
d.setdefault(c, []).append(i)
return tuple(d[u].pop(0) for u in word)
patterns = {}
# Sowpods wordlist from http://www.3zsoftware.com/download/
fname = 'scrabble_wordlist_sowpods.txt'
with open(fname) as f:
for word in f:
w = word.strip()
patterns.setdefault(word_index(w), []).append(w)
def print_words(pat):
print(pat)
words = patterns.get(pat, [])
print(' '.join(words))
# Test
#pat = (1, 4, 5, 3, 2)
#print_words(pat)
while True:
s = input('word: ')
pat = word_index(s)
print_words(pat)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment