#!/usr/bin/python import string def phonify(s): replace = { 'a': '2', 'b': '22', 'c': '222', 'd': '3', 'e': '33', 'f': '333', 'g': '4', 'h': '44', 'i': '444', 'j': '5', 'k': '55', 'l': '555', 'm': '6', 'n': '66', 'o': '666', 'p': '7', 'q': '77', 'r': '777', 's': '7777', 't': '8', 'u': '88', 'v': '888', 'w': '9', 'x': '99', 'y': '999', 'z': '9999', } filtered = filter(lambda x: x in string.letters, list(s)) translated = map(lambda x: replace[x.lower()], filtered) return '-'.join(translated) def annoyed(s): ''' out = '' previous = '' potential_pause = False for ch in s: if ch in string.digits: if potential_pause and ch == previous: out += '--' potential_pause = False previous = ch out += ch elif ch == '-': potential_pause = True else: assert False, ch return out ''' for x in range(2, 10): for y in range(2, 10): if x != y: s = s.replace('{}-{}'.format(x, y), '{}{}'.format(x, y)) return s.replace('-', '--') def main(wordlist_path): f = open(wordlist_path) lines = [line[:-1] for line in f] f.close() stage = [(annoyed(phonify(line)), line) for line in lines] del lines outlist = [(len(phone), phone, text) for phone, text in stage] del stage outlist.sort(key=lambda x: x[0], reverse=True) maxlen = outlist[0][0] for length, phone, text in outlist: print('{}{} {}'.format(phone, ' '*(maxlen-length), text)) if __name__ == '__main__': main('/usr/share/dict/words')