'''
The observed PIN,https://www.codewars.com/kata/5263c6999e0f40dee200059d/train/python
'''
def get_pins(observed):
adjacent = {
'0' : '08', # 0
'1' : '124', # 1
'2' : '1235', # 2
'3' : '236', # 3
'4' : '1457', # 4
'5' : '24568', # 5
'6' : '3569', # 6
'7' : '478', # 7
'8' : '57890', # 8
'9' : '689' # 9
}
result = ['']
for ch in observed:
result = [x+y for x in result for y in adjacent[ch]]
return result
# other's solution learning
from itertools import product
ADJACENTS = ('08', '124', '2135', '326', '4157', '52468', '6359', '748', '85790', '968')
def get_pins_other(observed):
# *运算符用于解包一个可迭代独享, 在函数调用中使用*运算符时, 会将可迭代对象中的元素作为单独的参数传递给函数
# product()函数用于求多个可迭代对象的笛卡尔积,笛卡尔积是一个集合,其中每个元素是一个元组,
# 元组的第一个元素取自第一个输入的可迭代对象,第二个元素取自第二个输入的可迭代对象,以此类推
# 例如: product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# join()方法用于将序列中的元素以指定的字符连接生成一个新的字符串
return [''.join(p) for p in product( *(ADJACENTS[int(d)] for d in observed) )]