Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Short solution in Clear category for The Einstein Problem-Lite by Vasily__Chibilyaev
COLORS = ['blue', 'green', 'red', 'white', 'yellow']
PETS = ['cat', 'bird', 'dog', 'fish', 'horse']
BEVERAGES = ['beer', 'coffee', 'milk', 'tea', 'water']
CIGARETTES = ['Rothmans', 'Dunhill', 'Pall Mall', 'Winfield', 'Marlboro']
NATIONALITY = ['Brit', 'Dane', 'German', 'Norwegian', 'Swede']
NUMBERS = ['1', '2', '3', '4', '5']
QUESTIONS = ["number", "color", "nationality", "beverage", "cigarettes", "pet"]
def answer(relations, question):
from itertools import product
def satisfied(rel, p):
features=rel.split('-')
return (features[0] in p)==(features[1] in p)
owner_char, asked_char=question.split('-')
for p in product(NUMBERS, COLORS, NATIONALITY, BEVERAGES, CIGARETTES, PETS):
if all(satisfied(rel, p) for rel in relations) and owner_char in p:
return p[QUESTIONS.index(asked_char)]
if __name__ == '__main__':
assert answer(('Norwegian-Dunhill', 'Marlboro-blue', 'Brit-3',
'German-coffee', 'beer-white', 'cat-water',
'horse-2', 'milk-3', '4-Rothmans',
'dog-Swede', 'Norwegian-1', 'horse-Marlboro',
'bird-Brit', '4-green', 'Winfield-beer',
'Dane-blue', '5-dog', 'blue-horse',
'yellow-cat', 'Winfield-Swede', 'tea-Marlboro'),
'fish-color') == 'green' # What is the color of the house where the Fish lives?
assert answer(('Norwegian-Dunhill', 'Marlboro-blue', 'Brit-3',
'German-coffee', 'beer-white', 'cat-water',
'horse-2', 'milk-3', '4-Rothmans',
'dog-Swede', 'Norwegian-1', 'horse-Marlboro',
'bird-Brit', '4-green', 'Winfield-beer',
'Dane-blue', '5-dog', 'blue-horse',
'yellow-cat', 'Winfield-Swede', 'tea-Marlboro'),
'tea-number') == '2' # What is the number of the house where tea is favorite beverage?
assert answer(('Norwegian-Dunhill', 'Marlboro-blue', 'Brit-3',
'German-coffee', 'beer-white', 'cat-water',
'horse-2', 'milk-3', '4-Rothmans',
'dog-Swede', 'Norwegian-1', 'horse-Marlboro',
'bird-Brit', '4-green', 'Winfield-beer',
'Dane-blue', '5-dog', 'blue-horse',
'yellow-cat', 'Winfield-Swede', 'tea-Marlboro'),
'Norwegian-beverage') == 'water' # What is the favorite beverage of the Norwegian man?
Sept. 29, 2017