""" Dictionary module for the keypad entry problem. Author: Rodney Topor Date: November 2007 """ import string # mapping from letters to digits alpha_to_digit = string.maketrans(string.lowercase, "22233344455566677778889999") # dictionary mapping digit sequences to lists of words digits_to_words = {} def add_words(words): """Add each word in the list words to the dictionary.""" for word in words: add_word(word) def add_word(word): """Add a word to the dictionary.""" # compute digit sequence corresponding to word digits = word.translate(alpha_to_digit) # add word to the list of words for this digit sequence if (digits_to_words.has_key(digits)): digits_to_words[digits].append(word) else: digits_to_words[digits] = [word] def get_words(digits): """Return the list of words associated with a digit sequence.""" if (digits_to_words.has_key(digits)): return digits_to_words[digits] else: return ["*" * len(digits)]