fork download
  1. from itertools import permutations
  2. from pprint import pprint
  3.  
  4. COLORS = ['blue', 'green', 'red', 'white', 'yellow']
  5. PETS = ['cat', 'bird', 'dog', 'fish', 'horse']
  6. BEVERAGES = ['beer', 'coffee', 'milk', 'tea', 'water']
  7. CIGARETTES = ['Dunhill', 'Rothmans', 'Pall Mall', 'Winfield', 'Marlboro']
  8. NATIONALITY = ['Norwegian', 'Brit', 'Dane', 'German', 'Swede']
  9. NUMBERS = ['1', '2', '3', '4', '5']
  10. QUESTIONS = ["number", "color", "nationality", "beverage", "cigarettes", "pet"]
  11.  
  12.  
  13. def is_predicate(citizens: list, predicate: str):
  14. return any([(set(predicate.split("-"))).issubset(el.values()) for el in citizens])
  15.  
  16.  
  17. def z(predicate: str):
  18. def carry(citizens: list):
  19. return is_predicate(citizens, predicate)
  20. return carry
  21.  
  22.  
  23. def answer(relations, question):
  24. my_list = (
  25. ({"color": c[0], "pet": pe[0], "beverage": b[0],
  26. "cigarettes": ci[0], "nationality": n[0], "number": NUMBERS[0]},
  27. {"color": c[1], "pet": pe[1], "beverage": b[1],
  28. "cigarettes": ci[1], "nationality": n[1], "number": NUMBERS[1]},
  29. {"color": c[2], "pet": pe[2], "beverage": b[2],
  30. "cigarettes": ci[2], "nationality": n[2], "number": NUMBERS[2]},
  31. {"color": c[3], "pet": pe[3], "beverage": b[3],
  32. "cigarettes": ci[3], "nationality": n[3], "number": NUMBERS[3]},
  33. {"color": c[4], "pet": pe[4], "beverage": b[4],
  34. "cigarettes": ci[4], "nationality": n[4], "number": NUMBERS[4]}
  35. )
  36. for c in permutations(COLORS)
  37. for pe in permutations(PETS)
  38. for b in permutations(BEVERAGES)
  39. for ci in permutations(CIGARETTES)
  40. for n in permutations(NATIONALITY)
  41. )
  42. for p in relations:
  43. f = z(p)
  44. my_list = filter(f, my_list)
  45. return next(my_list)
  46.  
  47. if __name__ == '__main__':
  48. pprint(answer(('Marlboro-blue', 'Norwegian-Dunhill', 'Brit-3',
  49. 'German-coffee', 'beer-white', 'cat-water',
  50. 'horse-2', 'milk-3', '4-Rothmans',
  51. 'dog-Swede', 'Norwegian-1', 'horse-Marlboro',
  52. 'bird-Brit', '4-green', 'Winfield-beer',
  53. 'Dane-blue', '5-dog', 'blue-horse',
  54. 'yellow-cat', 'Winfield-Swede', 'tea-Marlboro'),
  55. 'fish-color'))
  56. #asserts
  57. # assert answer(('Norwegian-Dunhill', 'Marlboro-blue', 'Brit-3',
  58. # 'German-coffee', 'beer-white', 'cat-water',
  59. # 'horse-2', 'milk-3', '4-Rothmans',
  60. # 'dog-Swede', 'Norwegian-1', 'horse-Marlboro',
  61. # 'bird-Brit', '4-green', 'Winfield-beer',
  62. # 'Dane-blue', '5-dog', 'blue-horse',
  63. # 'yellow-cat', 'Winfield-Swede', 'tea-Marlboro'),
  64. # 'fish-color') == 'green' # What is the color of the house where the Fish lives?
  65. # assert answer(('Norwegian-Dunhill', 'Marlboro-blue', 'Brit-3',
  66. # 'German-coffee', 'beer-white', 'cat-water',
  67. # 'horse-2', 'milk-3', '4-Rothmans',
  68. # 'dog-Swede', 'Norwegian-1', 'horse-Marlboro',
  69. # 'bird-Brit', '4-green', 'Winfield-beer',
  70. # 'Dane-blue', '5-dog', 'blue-horse',
  71. # 'yellow-cat', 'Winfield-Swede', 'tea-Marlboro'),
  72. # 'tea-number') == '2' # What is the number of the house where tea is favorite beverage?
  73. # assert answer(('Norwegian-Dunhill', 'Marlboro-blue', 'Brit-3',
  74. # 'German-coffee', 'beer-white', 'cat-water',
  75. # 'horse-2', 'milk-3', '4-Rothmans',
  76. # 'dog-Swede', 'Norwegian-1', 'horse-Marlboro',
  77. # 'bird-Brit', '4-green', 'Winfield-beer',
  78. # 'Dane-blue', '5-dog', 'blue-horse',
  79. # 'yellow-cat', 'Winfield-Swede', 'tea-Marlboro'),
  80. # 'Norwegian-beverage') == 'water'
  81. # What is the favorite beverage of the Norwegian man?###
  82. pass
Runtime error #stdin #stdout #stderr 0.03s 42064KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Warning: cannot find your CPU L2 cache size in /proc/cpuinfo
Traceback (most recent call last):
  File "app_main.py", line 75, in run_toplevel
  File "prog.py", line 13
    def is_predicate(citizens: list, predicate: str):
                            ^
SyntaxError: invalid syntax