Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Mind Switcher by kurosawa4434
def mind_switcher(journal):
# robot state get
robots = {}
for pair in journal:
r1, r2 = pair
robots[r1], robots[r2] = robots.get(r2, r2), robots.get(r1, r1)
# switched Robot get
switched_links = []
for body, mind in robots.items():
if body != mind:
switched_links.append((body, mind))
# chain search
chains = []
for sw in switched_links:
r1, r2 = sw
for i, ch in enumerate(chains):
if r1 in ch or r2 in ch:
if r1 not in ch :
chains[i].append(r1)
if r2 not in ch :
chains[i].append(r2)
break
else:
chains.append([r1, r2])
# chain connect
ok = False
while not ok:
ok = True
for i, ch1 in enumerate(chains):
for j, ch2 in enumerate(chains[i+1:], start=i+1):
if set(ch1) & set(ch2):
chains[i] = list(set(ch1 + ch2))
chains.pop(j)
ok = False
break
if not ok:
break
# result make
result = []
for ch in chains:
for i in range(len(ch) - 1):
if i == 0:
next_robot = ch[0]
nikola_memo = ch[0]
result.append( {'nikola', next_robot})
next_robot = robots[next_robot]
result.append({'sophia', next_robot})
result.append({'nikola', next_robot})
result.append({'sophia', nikola_memo})
if len(chains) % 2:
result.append({'nikola', 'sophia'})
return result
July 21, 2016
Comments: