Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Einstein Problem-Lite by Sim0000
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"]
CATEGORY = ["NUMBERS", "COLORS", "NATIONALITY", "BEVERAGES", "CIGARETTES", "PETS"]
def answer(relations, question):
# find category name from item
def find_category(item):
for category in CATEGORY:
if item in eval(category): return category
raise Exception('no such a item')
# search placement by DFS
def search(relations):
if not relations: return True # found solution
item = relations[0].split('-')
find = lambda i:table[category[i]].index(item[i]) if item[i] in table[category[i]] else -1
category = list(map(find_category, item))
index = list(map(find, range(2)))
# both item[0] and item[1] are registered.
if index[0] == index[1] >= 0:
if search(relations[1:]): return True
# item[0] is registered, item[1] is not registered.
elif index[0] >= 0 and index[1] == -1 and table[category[1]][index[0]] == None:
table[category[1]][index[0]] = item[1]
if search(relations[1:]): return True
table[category[1]][index[0]] = None
# item[0] is not registered, item[1] is registered.
elif index[1] >= 0 and index[0] == -1 and table[category[0]][index[1]] == None:
table[category[0]][index[1]] = item[0]
if search(relations[1:]): return True
table[category[0]][index[1]] = None
# both item[0] and item[1] are not registered.
elif index[0] == index[1] == -1:
for i in range(5):
if table[category[0]][i] == table[category[1]][i] == None:
table[category[0]][i] = item[0]
table[category[1]][i] = item[1]
if search(relations[1:]): return True
table[category[0]][i] = table[category[1]][i] = None
return False
# initialize
table = {category: [None] * 5 for category in CATEGORY}
# search placement
if not search(relations):
raise Exception('not found')
# In the case of only one place is not decided in each category
for category in CATEGORY:
if table[category].count(None) == 1:
index = table[category].index(None)
item = (set(eval(category)) - set(table[category])).pop()
table[category][index] = item
# answer question
item, query = question.split('-')
category = CATEGORY[QUESTIONS.index(query)]
index = table[find_category(item)].index(item)
return table[category][index]
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?
Jan. 27, 2015
Comments: