Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
The Einstein Problem-Lite solution in Uncategorized category for The Einstein Problem-Lite by capback250
# migrated from python 2.7
from functools import reduce
DEFAULTS = {'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']}
RESULT = {'COLORS':[None] * 5,
'PETS':[None] * 5,
'BEVERAGES':[None] * 5,
'CIGARETTES':[None] * 5,
'NATIONALITY':[None] * 5,
'NUMBER':[1,2,3,4,5]}
QUESTIONS = {"number":"NUMBER",
"color":'COLORS',
"nationality":'NATIONALITY',
"beverage":'BEVERAGES',
"cigarettes":'CIGARETTES',
"pet":'PETS'}
#It's look like code-porno :(
def answer(connections, question):
for conn in connections:
c1, c2 = conn.split('-')
if any([x in DEFAULTS['NUMBERS'] for x in [c1, c2]]):
if c1 in DEFAULTS['NUMBERS']:
keyfind, index = c2, int(c1) -1
else:
keyfind, index = c1, int(c2) -1
for category, arr in list(DEFAULTS.items()):
if keyfind in arr:
RESULT[category][index] = keyfind
while None in reduce(list.__add__, list(RESULT.values())):
for name, tempArr in list(RESULT.items()):
for tempVal in tempArr:
if tempVal is not None:
for _ in comparator(connections, tempVal):
yielded = _.pop()
if not yielded.isdigit():
_index = tempArr.index(tempVal)
for _name, _val in list(DEFAULTS.items()):
if yielded in _val:
RESULT[_name][_index] = yielded
fillchecker()
for name, value in list(RESULT.items()):
if question.split('-')[0] in value:
_index = value.index(question.split('-')[0])
return str(RESULT[QUESTIONS[question.split('-')[1]]][_index])
def comparator(inputVals, value):
for pair in inputVals:
if value in pair.split('-'):
yield set(pair.split('-')) - set([value])
def fillchecker():
for name, value in list(RESULT.items()):
if value.count(None) == 1:
indexToChange = value.index(None)
RESULT[name][indexToChange] = (set(DEFAULTS[name]) - set(RESULT[name])).pop()
Feb. 1, 2016