Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
MindSwitcher solution in Clear category for Mind Switcher by bunnychai
def mind_switcher(journal):
# array form
robots = {}
for r1, r2 in journal:
robots[r1], robots[r2] = robots.get(r2, r2), robots.get(r1, r1)
# disjoint cycles
cycles, visit = [], set()
for r1, r2 in robots.items():
if r1 not in visit:
cycle, c = [], r1
while c not in visit:
visit.add(c)
cycle.append(c)
c = robots[c]
if len(cycle) > 1:
cycles.append(cycle)
# invert each cycle
x, y = 'nikola', 'sophia'
inverse = []
for cycle in cycles:
for c in cycle[:-1]:
inverse.append({x, c})
k = cycle[-1]
inverse.extend([{y, k}, {x, k}, {y, cycle[0]}])
if len(cycles) % 2:
inverse.append({x, y})
return inverse
July 23, 2014
Comments: