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 TovarischZhukov
# 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"]
all_data={"number":NUMBERS, "nationality":NATIONALITY, "color":COLORS, "pet":PETS,"beverage":BEVERAGES,"cigarettes":CIGARETTES}
def add(data,key,val):
if key not in data:
data[key]={}
vtype=None
for el in all_data:
if val in all_data[el]:
if val in data:
data[key].update(data[val])
data[key][el]=val
if key in all_data[el]:
vtype=el
data[key]["type"]=vtype
def check(data, val):
key=None
for el in all_data:
if val in all_data[el]:
key=el
break
data[val]={"type":key}
for el in list(data.keys()):
if el not in all_data[key]:
if key not in data[el]:
data[el][key]=val
data[val][data[el]["type"]]=el
def getResult(data,val,typeVal):
if typeVal in data[val]:
return data[val][typeVal]
for el in data[val]:
if el!="type":
r=getResult(data,data[val][el],typeVal)
if r: return r
def answer(relations, question):
q=question.split("-")
tmp_data=[val for el in all_data for val in all_data[el]]
data={}
for el in relations:
val=el.split("-")
add(data,val[0],val[1])
if val[1] in tmp_data:
tmp_data.remove(val[1])
add(data,val[1],val[0])
if val[0] in tmp_data:
tmp_data.remove(val[0])
for el in tmp_data:
check(data,el)
return getResult(data,q[0],q[1])
Feb. 2, 2016