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 Moff
def move(state, action):
stacks = [list(s) for s in state]
from_s = int(action[0])
to_s = int(action[1])
if not stacks[from_s]:
return False
if to_s == 0 and stacks[to_s]:
return False
stacks[to_s].append(stacks[from_s].pop())
return tuple(''.join(s) for s in stacks)
def possible_moves(state):
return [act for act in ("12", "10", "01", "02", "20", "21") if move(state, act)]
def checkio(words):
"""bfs"""
w1, w2 = words.split('-')
x = ('', w1, '')
path_to = {x: []}
queue = [x]
while queue:
x = queue.pop()
if x[2] == w2:
return ','.join(path_to[x])
for act in possible_moves(x):
z = move(x, act)
if z not in path_to:
path_to[z] = path_to[x] + [act]
queue.insert(0, z)
Sept. 22, 2015