Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Digits Doublets by imloafer
from collections import deque
def checkio(numbers):
start, end = numbers[0], numbers[-1]
path = deque([[x] for x in numbers[1:] if len([(a, b) for a, b in zip(str(start), str(x)) if a == b]) == 2])
res = []
while path:
line = path.popleft()
number = line[-1]
if number == end:
res.append([start]+ line)
else:
for next_number in (x for x in numbers[1:] if len([(a, b) for a, b in zip(str(number), str(x)) if a == b]) == 2\
and x not in line):
path.append(line + [next_number])
return min(res, key=len)
#These "asserts" using only for self-checking and not necessary for auto-testing
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"
Oct. 24, 2022
Comments: