from itertools import permutations from pprint import pprint COLORS = ['blue', 'green', 'red', 'white', 'yellow'] PETS = ['cat', 'bird', 'dog', 'fish', 'horse'] BEVERAGES = ['beer', 'coffee', 'milk', 'tea', 'water'] CIGARETTES = ['Dunhill', 'Rothmans', 'Pall Mall', 'Winfield', 'Marlboro'] NATIONALITY = ['Norwegian', 'Brit', 'Dane', 'German', 'Swede'] NUMBERS = ['1', '2', '3', '4', '5'] QUESTIONS = ["number", "color", "nationality", "beverage", "cigarettes", "pet"] def is_predicate(citizens: list, predicate: str): return any([(set(predicate.split("-"))).issubset(el.values()) for el in citizens]) def z(predicate: str): def carry(citizens: list): return is_predicate(citizens, predicate) return carry def answer(relations, question): my_list = ( ({"color": c[0], "pet": pe[0], "beverage": b[0], "cigarettes": ci[0], "nationality": n[0], "number": NUMBERS[0]}, {"color": c[1], "pet": pe[1], "beverage": b[1], "cigarettes": ci[1], "nationality": n[1], "number": NUMBERS[1]}, {"color": c[2], "pet": pe[2], "beverage": b[2], "cigarettes": ci[2], "nationality": n[2], "number": NUMBERS[2]}, {"color": c[3], "pet": pe[3], "beverage": b[3], "cigarettes": ci[3], "nationality": n[3], "number": NUMBERS[3]}, {"color": c[4], "pet": pe[4], "beverage": b[4], "cigarettes": ci[4], "nationality": n[4], "number": NUMBERS[4]} ) for c in permutations(COLORS) for pe in permutations(PETS) for b in permutations(BEVERAGES) for ci in permutations(CIGARETTES) for n in permutations(NATIONALITY) ) for p in relations: f = z(p) my_list = filter(f, my_list) return next(my_list) if __name__ == '__main__': pprint(answer(('Marlboro-blue', 'Norwegian-Dunhill', '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')) #asserts # 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?### pass