Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
An orderly queue of one solution in Clear category for Anagrams By Stacks by smilicic
from collections import deque
def checkio(data):
def act(state, action):
i, o = int(action[0]), int(action[1])
if state[i] == "" or (action[1] == '0' and state[0] != ""):
return state
s0, s1, s2 = state
buff = state[i][-1] if o == 0 else "" if i==0 else s0
first = s1 + state[i][-1] if o==1 else s1[:-1] if i==1 else s1
second = s2 + state[i][-1] if o==2 else s2[:-1] if i==2 else s2
return (buff, first, second)
start, goal = data.split("-")
current_state = ("",start,"")
visited_states, state_queue = {current_state: None}, deque([current_state])
while current_state[2] != goal:
for action in "12", "10", "01", "02", "20", "21":
new_state = act(current_state, action)
if new_state not in visited_states:
visited_states[new_state] = action
state_queue.append(new_state)
current_state = state_queue.popleft()
action, path = visited_states[current_state], []
while action:
path = [action] + path
current_state = act(current_state, action[::-1])
action = visited_states[current_state]
return ",".join(path)
Oct. 2, 2016
Comments: