Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Digits Doublets by Chocolater
def checkio(numbers):
chains = [[numbers.pop(0)]]
for i in range(len(numbers)):
new_chains = []
# Taking each of the chains...
for chain in chains:
# Each of the awailable numbers...
for number in numbers:
# And checking if the number fits at the end of chain.
if ((number not in chain and
[str(number)[a] == str(chain[-1])[a]
for a in range(3)].count(True) == 2)):
# If it does, then new chainis created.
new_chains += [chain + [number]]
# And if it's the last number, then the chain is ready.
if number == numbers[-1]:
return chain + [number]
chains = new_chains[:]
#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"
April 10, 2018