Skip to content

Instantly share code, notes, and snippets.

@mumbleskates
Last active April 25, 2016 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mumbleskates/b2836d6e07834af25f25e0e229efa2ed to your computer and use it in GitHub Desktop.
Save mumbleskates/b2836d6e07834af25f25e0e229efa2ed to your computer and use it in GitHub Desktop.
# 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