Created
April 5, 2017 15:18
-
-
Save PM2Ring/dfc59f314fb18a0816c1bb7c796e9126 to your computer and use it in GitHub Desktop.
Alphagram Maker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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