Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
BFS solution in Clear category for Digits Doublets by U.V
from collections import deque
def checkio(numbers):
s = [str(i) for i in numbers]
start, finish,path = s[0], s[~0], []
# neibh of node
nb = {i: sorted(j for j in s if sum( (a==b) for a, b in zip(i, j) ) == 2) for i in s}
# BFS
used = {start}
q = deque([(None, start, )])
while q:
frm, nod = q.popleft()
path.append([nod, frm])
if nod == finish:
break
used.update({nod} )
for n in nb[nod]:
if n not in used:
q.extend({(nod, n)})
# Backtrack
pp = [int(finish)]
while finish != start:
for t, f in path:
if t == finish:
break
finish = f
pp.insert(0, int(finish))
return pp
Jan. 25, 2023
Comments: