Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for The Einstein Problem-Lite by gyahun_dash
from collections import Counter
from itertools import chain
FEATURES = {
'color': {'blue', 'green', 'red', 'white', 'yellow'},
'pet': {'cat', 'bird', 'dog', 'fish', 'horse'},
'beverage': {'beer', 'coffee', 'milk', 'tea', 'water'},
'cigarettes': {'Rothmans', 'Dunhill', 'Pall Mall', 'Winfield', 'Marlboro'},
'nationality': {'Brit', 'Dane', 'German', 'Norwegian', 'Swede'},
'number': {'1', '2', '3', '4', '5'}}
CATEGORIES = {feat: cat for cat in FEATURES for feat in FEATURES[cat]}
def answer(relations, question):
cue, quest = question.split('-')
groups = [{feat} for feat in chain(*FEATURES.values())]
for rel in relations:
src, dst = [next(g for g in groups if f in g) for f in rel.split('-')]
if src != dst: dst.update(groups.pop(groups.index(src)))
own = next(g for g in groups if cue in g)
mapcat = lambda feats: map(CATEGORIES.get, feats)
while quest not in mapcat(own):
groups = [g for g in groups if set(mapcat(g)).isdisjoint(mapcat(own))]
counts = Counter(mapcat(chain(*groups)))
unicats = {c for c in counts if counts[c] == 1}
own |= {f for g in groups for f in g if unicats & set(mapcat(g))}
return next(feat for feat in own if feat in FEATURES[quest])
May 4, 2015