Skip to content

Instantly share code, notes, and snippets.

@aphahn
Created December 24, 2010 22:47
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 aphahn/754549 to your computer and use it in GitHub Desktop.
Save aphahn/754549 to your computer and use it in GitHub Desktop.
A fun little tool that, given suffixes and a wordlist, prints words that end in those suffixes
#!/usr/bin/env python
import sys
def to_set(collection):
return "{" + ",".join(collection) + "}"
suffixes = {}
for suffix in sys.argv[1:-1]:
suffixes[suffix] = set()
filename = sys.argv[-1]
with open(filename, 'r') as wordlist:
for line in wordlist:
word = line.strip()
for suffix, set in suffixes.iteritems():
if word.endswith(suffix):
prefix = word[:-len(suffix)]
if prefix:
set.add(prefix)
prefixes = None
for set in suffixes.itervalues():
if prefixes is None:
prefixes = set
else:
prefixes &= set
print to_set(prefixes) + to_set(suffixes.keys())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment