Finished
1def drinks_water():
2 pass
1import itertools
32
3def drinks_water():
4 """Returns the nationality of the person who drinks water."""
5 solution = solve_puzzle()
6 for house in solution:
7 if house['drink'] == 'water':
8 return house['nationality']
9 return None
410
511def owns_zebra():
6 pass
12 """Returns the nationality of the person who owns the zebra."""
13 solution = solve_puzzle()
14 for house in solution:
15 if house['pet'] == 'zebra':
16 return house['nationality']
17 return None
18
19def solve_puzzle():
20 """
21 Solves the Zebra Puzzle using constraint satisfaction.
22 Returns a list of 5 dictionaries, each representing a house with its attributes.
23 """
24 # Define the possible values for each category
25 nationalities = ['Englishman', 'Spaniard', 'Ukrainian', 'Norwegian', 'Japanese']
26 colors = ['red', 'green', 'ivory', 'yellow', 'blue']
27 pets = ['dog', 'snail', 'fox', 'horse', 'zebra']
28 drinks = ['coffee', 'tea', 'milk', 'orange juice', 'water']
29 hobbies = ['dancing', 'painting', 'reading', 'football', 'chess']
30
31 # Generate all possible permutations for each category
32 for nationality_perm in itertools.permutations(nationalities):
33 # Constraint 10: The Norwegian lives in the first house
34 if nationality_perm[0] != 'Norwegian':
35 continue
36
37 for color_perm in itertools.permutations(colors):
38 # Constraint 15: The Norwegian lives next to the blue house
39 if color_perm[1] != 'blue':
40 continue
41
42 # Constraint 6: The green house is immediately to the right of the ivory house
43 found_ivory_green = False
44 for i in range(4):
45 if color_perm[i] == 'ivory' and color_perm[i+1] == 'green':
46 found_ivory_green = True
47 break
48 if not found_ivory_green:
49 continue
50
51 # Constraint 2: The Englishman lives in the red house
52 red_index = color_perm.index('red')
53 if nationality_perm[red_index] != 'Englishman':
54 continue
55
56 # Constraint 8: The person in the yellow house is a painter
57 # This will be checked later with hobbies
58
59 for pet_perm in itertools.permutations(pets):
60 # Constraint 3: The Spaniard owns the dog
61 if nationality_perm[pet_perm.index('dog')] != 'Spaniard':
62 continue
63
64 for drink_perm in itertools.permutations(drinks):
65 # Constraint 9: The person in the middle house drinks milk
66 if drink_perm[2] != 'milk':
67 continue
68
69 # Constraint 4: The person in the green house drinks coffee
70 if drink_perm[color_perm.index('green')] != 'coffee':
71 continue
72
73 # Constraint 5: The Ukrainian drinks tea
74 if nationality_perm[drink_perm.index('tea')] != 'Ukrainian':
75 continue
76
77 for hobby_perm in itertools.permutations(hobbies):
78 # Constraint 7: The snail owner likes to go dancing
79 if hobby_perm[pet_perm.index('snail')] != 'dancing':
80 continue
81
82 # Constraint 8: The person in the yellow house is a painter
83 if hobby_perm[color_perm.index('yellow')] != 'painting':
84 continue
85
86 # Constraint 13: The person who plays football drinks orange juice
87 if hobby_perm[drink_perm.index('orange juice')] != 'football':
88 continue
89
90 # Constraint 14: The Japanese person plays chess
91 if hobby_perm[nationality_perm.index('Japanese')] != 'chess':
92 continue
93
94 # Constraint 11: The person who enjoys reading lives next to the person with the fox
95 reading_index = hobby_perm.index('reading')
96 fox_index = pet_perm.index('fox')
97 if abs(reading_index - fox_index) != 1:
98 continue
99
100 # Constraint 12: The painter's house is next to the house with the horse
101 painting_index = hobby_perm.index('painting')
102 horse_index = pet_perm.index('horse')
103 if abs(painting_index - horse_index) != 1:
104 continue
105
106 # If we reach here, we have a valid solution
107 houses = []
108 for i in range(5):
109 house = {
110 'nationality': nationality_perm[i],
111 'color': color_perm[i],
112 'pet': pet_perm[i],
113 'drink': drink_perm[i],
114 'hobby': hobby_perm[i]
115 }
116 houses.append(house)
117 return houses
118
119 # This should not happen as the puzzle has a solution
120 return None
121
122# Handled Edge Cases: All constraints from the puzzle are implemented as checks in the solution process
Test NameStatus
test_resident_who_drinks_water
Pass
test_resident_who_owns_zebra
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.