Last active
April 25, 2016 17:06
-
-
Save mumbleskates/b2836d6e07834af25f25e0e229efa2ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
from itertools import product | |
adjacents = ('08', '124', '1253', '236', '1457', '24568', '3569', '478', '57890', '689') | |
adjacents = {str(i): s for i, s in enumerate(adjacents)} | |
def get_pins_itertools(observed): | |
for pins in product(*(adjacents[ch] for ch in observed)): | |
yield "".join(pins) | |
def get_pins_recursive(observed): | |
# split off the first character | |
first, rest = observed[:1], observed[1:] | |
if not first: | |
return | |
if not rest: | |
# we are on the last digit | |
for ch in adjacents[first]: | |
yield ch | |
for key in adjacents[first]: # loop over our first digit | |
for tail in get_pins_recursive(rest): # add on the results for the rest | |
yield key + tail |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment