Skip to content

Instantly share code, notes, and snippets.

@yabberyabber
Created February 5, 2018 21:56
Show Gist options
  • Save yabberyabber/a35c70a923ab33e79f45b8bc424421a8 to your computer and use it in GitHub Desktop.
Save yabberyabber/a35c70a923ab33e79f45b8bc424421a8 to your computer and use it in GitHub Desktop.
import enchant
d = enchant.Dict("en_US")
num_mappings = {
1: [],
2: ['a', 'b', 'c'],
3: ['d', 'e', 'f'],
4: ['g', 'h', 'i'],
5: ['j', 'k', 'l'],
6: ['m', 'n', 'p'],
7: ['p', 'q', 'r', 's'],
8: ['t', 'u', 'v'],
9: ['w', 'x', 'y', 'z'],
0: [],
}
def enumerate_possible_strings(prev, nums):
if nums:
for mapping in num_mappings[nums[0]]:
yield from enumerate_possible_strings(prev + mapping, nums[1:])
if not num_mappings[nums[0]]:
yield from enumerate_possible_strings(prev, nums[1:])
for i in range(len(prev)):
yield prev[i:]
def main(number):
seen = set()
for mapping in enumerate_possible_strings("", number):
if mapping not in seen and d.check(mapping):
print(mapping)
seen.add(mapping)
if __name__ == '__main__':
main([4, 2, 5, 4, 0, 1, 2, 4, 6, 7])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment