Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Anagrams By Stacks by freixodachamorra
def checkio(phrase):
def move(stack1, stack2, buffer, mov):
index1, index2 = [int(i) for i in mov]
local_stack1, local_stack2, local_buffer = stack1[:], stack2[:], buffer
origin = [local_buffer, local_stack1, local_stack2][index1]
destination = [local_buffer, local_stack1, local_stack2][index2]
if mov[0] == '0':
temp = local_buffer
local_buffer = ''
else:
temp = origin.pop()
if mov[1] == '0':
local_buffer = temp
else:
destination.append(temp)
return local_stack1, local_stack2, local_buffer
def movs(stack1, stack2, buffer):
origin = []
if stack1:
origin.append('1')
if stack2:
origin.append('2')
if buffer != '':
origin.append('0')
destiny = ['1','2']
if buffer == '':
destiny.append('0')
output = []
for i in origin:
for j in destiny:
if i !=j:
output.append(i+j)
return output
make_status = lambda s1,s2,b : "".join(s1) + "#" + "".join(s2) + "#" + b
origin, target = phrase.split('-')
queu = []
visited = []
stack1 = list(origin)
stack2 = []
buffer = ''
for i in movs(stack1, stack2, buffer):
stack1 = list(origin)
stack2 = []
buffer = ''
stack1, stack2, buffer = move(stack1, stack2, buffer, i)
queu.append([stack1, stack2, buffer, i])
visited.append(make_status(stack1, stack2, buffer))
while True:
queu2 = []
for param in queu:
stack1, stack2, buffer, st = param
if "".join(stack2) == target:
return st
for mov in movs(stack1, stack2, buffer):
stack1_c, stack2_c, buffer_c, st_c = stack1, stack2, buffer, st
stack1_c, stack2_c, buffer_c = move(stack1_c, stack2_c, buffer_c, mov)
status = make_status(stack1_c, stack2_c, buffer_c)
if status not in visited:
visited.append(status)
st_c += ',' + mov
queu2.append([stack1_c, stack2_c, buffer_c, st_c])
queu = queu2
assert check_solution(checkio, "rice-cire", 5), "rice-cire"
assert check_solution(checkio, "tort-trot", 4), "tort-trot"
assert check_solution(checkio, "hello-holle", 14), "hello-holle"
assert check_solution(checkio, "anagram-mragana", 8), "anagram-mragana"
assert check_solution(checkio, "mirror-mirorr", 25), "mirror-mirorr"
July 29, 2018
Comments: