Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Digits Doublets solution in Uncategorized category for Digits Doublets by przemyslaw.daniel
def a2ndb1d(a, b): # are 2 numbers different by 1 digit
s = sum([int(x!=y) for x, y in zip(str(a), str(b))])
return s==1 and len(str(a))==len(str(b))
def checkio(numbers):
best=[0]*(len(numbers)+1)
stack=[([numbers[0]], numbers[1:])]
while len(stack)>0:
path, choice = stack.pop()
if path[-1]==numbers[-1]:
best=min(path[:], best, key=len)
continue
for i in choice:
if not a2ndb1d(path[-1], i): continue
stack.append((path+[i], [x for x in choice if x != i]))
return None if sum(best)==0 else best
if __name__ == '__main__':
assert checkio([123, 991, 323, 321, 329, 121, 921, 125, 999]) == [123, 121, 921, 991, 999], "First"
assert checkio([111, 222, 333, 444, 555, 666, 121, 727, 127, 777]) == [111, 121, 127, 727, 777], "Second"
assert checkio([456, 455, 454, 356, 656, 654]) == [456, 454, 654], "Third, [456, 656, 654] is correct too"
May 6, 2016
Comments: