Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Mind Switcher by pohmelie
def mind_switcher(journal):
m = {}
for a, b in journal:
m[a], m[b] = m.get(b, b), m.get(a, a)
m = dict(filter(lambda kv: len(set(kv)) != 1, m.items()))
rm = dict(map(lambda kv: kv[::-1], m.items()))
actions = []
count = 0
while m:
robot, brain = m.popitem()
actions += [{robot, "nikola"}, {rm[robot], "sophia"}, {robot, "sophia"}]
while brain in m:
actions.append({brain, "nikola"})
brain = m.pop(brain)
count += 1
if count & 1:
actions.append({"nikola", "sophia"})
return actions
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
def check_solution(func, data):
robots = {"nikola": "nikola", "sophia": "sophia"}
switched = []
for pair in data:
switched.append(set(pair))
r1, r2 = pair
robots[r1], robots[r2] = robots.get(r2, r2), robots.get(r1, r1)
result = func(data)
if not isinstance(result, (list, tuple)) or not all(isinstance(p, set) for p in result):
print("The result should be a list/tuple of sets.")
return False
for pair in result:
if len(pair) != 2:
print(1, "Each pair should contain exactly two names.")
return False
r1, r2 = pair
if not isinstance(r1, str) or not isinstance(r2, str):
print("Names must be strings.")
return False
if r1 not in robots.keys():
print("I don't know '{}'.".format(r1))
return False
if r2 not in robots.keys():
print("I don't know '{}'.".format(r2))
return False
if set(pair) in switched:
print("'{}' and '{}' already were switched.".format(r1, r2))
return False
switched.append(set(pair))
robots[r1], robots[r2] = robots[r2], robots[r1]
for body, mind in robots.items():
if body != mind:
print("'{}' has '{}' mind.".format(body, mind))
return False
return True
assert check_solution(mind_switcher, ({"scout", "super"},))
assert check_solution(mind_switcher, ({'hater', 'scout'}, {'planer', 'hater'}))
assert check_solution(mind_switcher, ({'scout', 'driller'}, {'scout', 'lister'},
{'hater', 'digger'}, {'planer', 'lister'}, {'super', 'melter'}))
July 23, 2014
Comments: