Created
January 19, 2012 16:23
-
-
Save dggoldst/1640971 to your computer and use it in GitHub Desktop.
Given a long number, generate mnemonics using the digit-sound system, per http://www.decisionsciencenews.com/2012/01/17/some-code-to-help-you-remember-numbers/
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/python | |
# -*- coding: utf-8 -*- | |
import re | |
from cStringIO import StringIO | |
from sys import argv | |
import random | |
mstr = str(argv[1]) | |
adict = {} | |
wl = open('wordNum.txt') | |
line = wl.readline() | |
while line: | |
m = re.match(r"(\w+)\t(\w+)\n", line) | |
if m is not None: | |
word = m.group(1) | |
number = m.group(2) | |
adict[number] = word + ' ' \ | |
+ adict.get(number, '') | |
line = wl.readline() | |
def rec(x, y): | |
if len(x) == 0: | |
return | |
if x in adict: | |
print x | |
print adict[x] | |
elif y == 1: | |
p1 = x[0:len(x) / 2] | |
p2 = x[len(x) / 2:len(x)] | |
rec(p1, y) | |
rec(p2, y) | |
else: | |
p1 = x[0:len(x) / 2 + 1] | |
p2 = x[len(x) / 2 + 1:len(x)] | |
rec(p1, y) | |
rec(p2, y) | |
def rec3(x): | |
if len(x) == 0: | |
return | |
if x in adict: | |
print x | |
print adict[x] | |
else: | |
maxlen = 0 | |
winner = '' | |
for a in adict.keys(): | |
if a in x: | |
if len(a) > maxlen: # goes with the first | |
winner = a | |
maxlen = len(a) | |
if maxlen > 0: | |
(part1, part2) = x.split(winner, 1) | |
rec3(part1) | |
rec3(winner) | |
rec3(part2) | |
else: | |
p1 = x[0:len(x) / 2 + 1] | |
p2 = x[len(x) / 2 + 1:len(x)] | |
rec3(p1) | |
rec3(p2) | |
print '-----------------------' | |
rec(mstr, 1) | |
print '-----------------------' | |
rec(mstr, 2) | |
print '-----------------------' | |
rec3(mstr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment