Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Mind Switcher by han97
# migrated from python 2.7
from functools import reduce
def mind_switcher(journal):
names = list(reduce(set.union, journal))
d = {s: i for i, s in enumerate(names)}
journal = [(d[j.pop()], d[j.pop()]) for j in journal]
def cycles():
from sympy.combinatorics.permutations import Cycle, Perm
res = Cycle()
for t in reversed(journal):
res = res(*t)
return Perm(res).cyclic_form
swap = False
res = []
x, y = "nikola", "sophia"
for cycle in cycles():
swap = not swap
res.append({x, names[cycle[0]]})
for i in range(1, len(cycle)):
res.append({y, names[cycle[i]]})
res.extend([{x, names[cycle[1]]}, {y, names[cycle[0]]}])
if swap:
res.append({x, y})
return res
Aug. 12, 2016
Comments: