Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Anagrams By Stacks by htamas
from collections import deque
def step(a, b, c):
if a:
yield (a[:-1], b, c + a[-1]), '12'
if c:
yield (a + c[-1], b, c[:-1]), '21'
if b:
yield (a + b, '', c), '01'
yield (a, '', c + b), '02'
else:
if a:
yield (a[:-1], a[-1], c), '10'
if b:
yield (a, c[-1], c[:-1]), '20'
def checkio(data):
[orig, target] = data.split('-')
orig = (orig, '', '')
target = ('', '', target)
reached = {orig}
queue = deque([(orig, '')])
while queue:
prev, seq = queue.popleft()
for state, move in step(*prev):
if state not in reached:
if state == target:
return seq + move
reached.add(state)
queue.append((state, seq + move + ','))
Oct. 22, 2013
Comments: