Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Digits Doublets by Amachua
distance = lambda a, b: sum([a[i]!=b[i] for i in range(len(a))])
def checkio(numbers):
# Start with the first number.
paths = [[numbers.pop(0)]]
# BFS algorithm.
while paths:
# Get the new position.
current = paths.pop(0)
for i in range(len(numbers) - 1, -1, -1):
# For every remaining number check if the distance between the current one and another one is 1. (i.e. 1 letter different).
if distance(str(numbers[i]), str(current[-1])) != 1: continue
# If the distance is one and it's the last number, return the path to the final word.
if numbers[i] == numbers[-1]: return current+[numbers[-1]]
# If not add this word to the possible paths.
paths.append(current+[numbers.pop(i)])
Feb. 5, 2014
Comments: