Skip to content

Instantly share code, notes, and snippets.

@danomoseley
Created February 25, 2021 22:12
Show Gist options
  • Save danomoseley/8456643435dcd64dbda70c5abec33f66 to your computer and use it in GitHub Desktop.
Save danomoseley/8456643435dcd64dbda70c5abec33f66 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
def recursive_t9(numbers, letter_map):
if len(str(numbers)) == 1:
return letter_map[int(numbers)]
elif len(str(numbers)) == 0:
return []
other_options = recursive_t9(str(numbers)[1:], letter_map)
words = []
for option in letter_map[int(str(numbers)[0])]:
for other_option in other_options:
words.append(option+other_option)
return words
def t9_predictor(numbers, valid_words):
letter_map = [[],[],['a','b','c'],['d','e','f'],['g','h','i'],['j','k','l'],['m','n','o'],['p','q','r','s'],['t','u','v'],['w','x','y','z']]
possible_words = recursive_t9(numbers, letter_map)
return list(set(possible_words) & set(valid_words))
if __name__ == "__main__":
with open('/usr/share/dict/words') as f:
valid_words = f.read().splitlines()
while True:
numbers = input("Enter numbers: ")
print(t9_predictor(numbers, valid_words))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment