Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for The Einstein Problem-Lite by UFO665
# migrated from python 2.7
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"]
dctStruct = {"number": NUMBERS, "color": COLORS, "beverage": BEVERAGES,
"cigarettes": CIGARETTES, "nationality": NATIONALITY, "pet": PETS}
def findStruct(word):
for key, val in dctStruct.items():
if word in val:
return key
def answer(relations, question):
lstRelations = list(relations)
dct = {key: [None] * 5 for key in QUESTIONS}
dct["number"] = NUMBERS
iRel = 0
while lstRelations:
relA, relB = lstRelations[iRel % len(lstRelations)].split("-")
structA, structB = findStruct(relA), findStruct(relB)
if relA in dct[structA]:
index = dct[structA].index(relA)
dct[structB][index] = relB
lstRelations.remove("{}-{}".format(relA, relB))
elif relB in dct[structB]:
index = dct[structB].index(relB)
dct[structA][index] = relA
lstRelations.remove("{}-{}".format(relA, relB))
else:
iRel += 1
for key, val in dct.items():
if val.count(None) == 1:
index = val.index(None)
dct[key][index] = set(dctStruct[key]).difference(set(val)).pop()
relA, relB = question.split("-")
struct = findStruct(relA)
return dct[relB][dct[struct].index(relA)]
Jan. 31, 2016