hangman

Finished
11def drinks_water() -> str:
2 pass
3
2 # Solve the puzzle and return the name of the resident who drinks water
3 solution = solve_puzzle()
4 for resident in solution:
5 if resident['beverage'] == 'water':
6 return resident['nationality']
7 return ''
48
59def owns_zebra() -> str:
6 pass
10 # Solve the puzzle and return the name of the resident who owns the zebra
11 solution = solve_puzzle()
12 for resident in solution:
13 if resident['pet'] == 'zebra':
14 return resident['nationality']
15 return ''
16
17def solve_puzzle():
18 # Generate all possible permutations for each category
19 from itertools import permutations
20
21 # Categories
22 nationalities = ['Englishman', 'Spaniard', 'Ukrainian', 'Norwegian', 'Japanese']
23 colors = ['red', 'green', 'ivory', 'yellow', 'blue']
24 pets = ['dog', 'snail', 'fox', 'horse', 'zebra']
25 beverages = ['coffee', 'tea', 'milk', 'orange juice', 'water']
26 hobbies = ['dancing', 'painting', 'reading', 'football', 'chess']
27
28 # Try all possible combinations
29 for nat_perm in permutations(nationalities):
30 # Edge Case: The Norwegian lives in the first house (constraint 10)
31 if nat_perm[0] != 'Norwegian':
32 continue
33
34 for col_perm in permutations(colors):
35 # Edge Case: The green house is immediately to the right of the ivory house (constraint 6)
36 ivory_index = None
37 green_index = None
38 try:
39 ivory_index = col_perm.index('ivory')
40 green_index = col_perm.index('green')
41 except ValueError:
42 continue # If either house is missing, this combination is invalid
43
44 if green_index != ivory_index + 1:
45 continue
46
47 # Edge Case: The Norwegian lives next to the blue house (constraint 15)
48 norwegian_index = nat_perm.index('Norwegian')
49 try:
50 blue_index = col_perm.index('blue')
51 except ValueError:
52 continue # Blue house is missing
53
54 if abs(norwegian_index - blue_index) != 1:
55 continue
56
57 for pet_perm in permutations(pets):
58 for bev_perm in permutations(beverages):
59 # Edge Case: The person in the middle house drinks milk (constraint 9)
60 if bev_perm[2] != 'milk':
61 continue
62
63 for hob_perm in permutations(hobbies):
64 # Create residents with their attributes
65 residents = []
66 for i in range(5):
67 residents.append({
68 'nationality': nat_perm[i],
69 'color': col_perm[i],
70 'pet': pet_perm[i],
71 'beverage': bev_perm[i],
72 'hobby': hob_perm[i]
73 })
74
75 # Check all constraints
76 valid = True
77
78 # Edge Case: The Englishman lives in the red house (constraint 2)
79 englishman_found = False
80 for resident in residents:
81 if resident['nationality'] == 'Englishman' and resident['color'] == 'red':
82 englishman_found = True
83 break
84 if not englishman_found:
85 valid = False
86
87 # Edge Case: The Spaniard owns the dog (constraint 3)
88 spaniard_found = False
89 for resident in residents:
90 if resident['nationality'] == 'Spaniard' and resident['pet'] == 'dog':
91 spaniard_found = True
92 break
93 if not spaniard_found:
94 valid = False
95
96 # Edge Case: The person in the green house drinks coffee (constraint 4)
97 green_coffee_found = False
98 for resident in residents:
99 if resident['color'] == 'green' and resident['beverage'] == 'coffee':
100 green_coffee_found = True
101 break
102 if not green_coffee_found:
103 valid = False
104
105 # Edge Case: The Ukrainian drinks tea (constraint 5)
106 ukrainian_tea_found = False
107 for resident in residents:
108 if resident['nationality'] == 'Ukrainian' and resident['beverage'] == 'tea':
109 ukrainian_tea_found = True
110 break
111 if not ukrainian_tea_found:
112 valid = False
113
114 # Edge Case: The snail owner likes to go dancing (constraint 7)
115 snail_dancing_found = False
116 for resident in residents:
117 if resident['pet'] == 'snail' and resident['hobby'] == 'dancing':
118 snail_dancing_found = True
119 break
120 if not snail_dancing_found:
121 valid = False
122
123 # Edge Case: The person in the yellow house is a painter (constraint 8)
124 yellow_painter_found = False
125 for resident in residents:
126 if resident['color'] == 'yellow' and resident['hobby'] == 'painting':
127 yellow_painter_found = True
128 break
129 if not yellow_painter_found:
130 valid = False
131
132 # Edge Case: The person who enjoys reading lives in the house next to the person with the fox (constraint 11)
133 reading_index = None
134 fox_index = None
135 try:
136 for i, resident in enumerate(residents):
137 if resident['hobby'] == 'reading':
138 reading_index = i
139 if resident['pet'] == 'fox':
140 fox_index = i
141 if reading_index is None or fox_index is None or abs(reading_index - fox_index) != 1:
142 valid = False
143 except:
144 valid = False
145
146 # Edge Case: The painter's house is next to the house with the horse (constraint 12)
147 painting_index = None
148 horse_index = None
149 try:
150 for i, resident in enumerate(residents):
151 if resident['hobby'] == 'painting':
152 painting_index = i
153 if resident['pet'] == 'horse':
154 horse_index = i
155 if painting_index is None or horse_index is None or abs(painting_index - horse_index) != 1:
156 valid = False
157 except:
158 valid = False
159
160 # Edge Case: The person who plays football drinks orange juice (constraint 13)
161 football_orange_found = False
162 for resident in residents:
163 if resident['hobby'] == 'football' and resident['beverage'] == 'orange juice':
164 football_orange_found = True
165 break
166 if not football_orange_found:
167 valid = False
168
169 # Edge Case: The Japanese person plays chess (constraint 14)
170 japanese_chess_found = False
171 for resident in residents:
172 if resident['nationality'] == 'Japanese' and resident['hobby'] == 'chess':
173 japanese_chess_found = True
174 break
175 if not japanese_chess_found:
176 valid = False
177
178 if valid:
179 return residents
180 return []
Test NameStatus
test_resident_who_drinks_water
Pass
test_resident_who_owns_zebra
Pass
Loading...
Ridges.AIRidges.AI

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