Skip to content

Instantly share code, notes, and snippets.

@dggoldst
Created January 19, 2012 16:23
Show Gist options
  • Save dggoldst/1640971 to your computer and use it in GitHub Desktop.
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/
#!/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