python

Finished
1def drinks_water() -> str:
2 pass
1import itertools
32
3def drinks_water() -> str:
4 # Solve the puzzle and return the name of the resident who drinks water
5 solution = solve_puzzle()
6 for i, resident in enumerate(solution):
7 if resident[3] == 'water':
8 return resident[1] # Return the nationality
9 return ""
410
511def owns_zebra() -> str:
6 pass
12 # Solve the puzzle and return the name of the resident who owns the zebra
13 solution = solve_puzzle()
14 for i, resident in enumerate(solution):
15 if resident[2] == 'zebra':
16 return resident[1] # Return the nationality
17 return ""
18
19def solve_puzzle():
20 # Define the possible values for each attribute
21 colors = ['red', 'green', 'ivory', 'yellow', 'blue']
22 nationalities = ['Englishman', 'Spaniard', 'Ukrainian', 'Norwegian', 'Japanese']
23 pets = ['dog', 'snails', 'fox', 'horse', 'zebra']
24 drinks = ['coffee', 'tea', 'milk', 'orange juice', 'water']
25 hobbies = ['dancing', 'painting', 'reading', 'football', 'chess']
26
27 # Generate all possible permutations for each attribute
28 color_perms = list(itertools.permutations(colors))
29 nationality_perms = list(itertools.permutations(nationalities))
30 pet_perms = list(itertools.permutations(pets))
31 drink_perms = list(itertools.permutations(drinks))
32 hobby_perms = list(itertools.permutations(hobbies))
33
34 # Iterate through all possible combinations
35 for color_combo in color_perms:
36 # Constraint 6: The green house is immediately to the right of the ivory house
37 # Edge Case: Green house must be immediately to the right of ivory house
38 if not any(color_combo[i] == 'ivory' and color_combo[i+1] == 'green' for i in range(4)):
39 continue
40
41 for nationality_combo in nationality_perms:
42 # Constraint 10: The Norwegian lives in the first house
43 # Edge Case: Norwegian must be in the first house (index 0)
44 if nationality_combo[0] != 'Norwegian':
45 continue
46
47 # Constraint 15: The Norwegian lives next to the blue house
48 # Edge Case: Norwegian (in house 0) must be next to blue house (house 1)
49 if color_combo[1] != 'blue':
50 continue
51
52 for pet_combo in pet_perms:
53 # Constraint 3: The Spaniard owns the dog
54 # Edge Case: Find where Spaniard and dog are in the same house
55 spaniard_index = nationality_combo.index('Spaniard')
56 if pet_combo[spaniard_index] != 'dog':
57 continue
58
59 for drink_combo in drink_perms:
60 # Constraint 9: The person in the middle house drinks milk
61 # Edge Case: Person in middle house (index 2) must drink milk
62 if drink_combo[2] != 'milk':
63 continue
64
65 # Constraint 4: The person in the green house drinks coffee
66 # Edge Case: Find where green house and coffee are in the same house
67 green_index = color_combo.index('green')
68 if drink_combo[green_index] != 'coffee':
69 continue
70
71 # Constraint 5: The Ukrainian drinks tea
72 # Edge Case: Find where Ukrainian and tea are in the same house
73 ukrainian_index = nationality_combo.index('Ukrainian')
74 if drink_combo[ukrainian_index] != 'tea':
75 continue
76
77 for hobby_combo in hobby_perms:
78 # Constraint 2: The Englishman lives in the red house
79 # Edge Case: Find where Englishman and red house are in the same house
80 englishman_index = nationality_combo.index('Englishman')
81 if color_combo[englishman_index] != 'red':
82 continue
83
84 # Constraint 7: The snail owner likes to go dancing
85 # Edge Case: Find where snails and dancing are in the same house
86 snail_index = pet_combo.index('snails')
87 if hobby_combo[snail_index] != 'dancing':
88 continue
89
90 # Constraint 8: The person in the yellow house is a painter
91 # Edge Case: Find where yellow house and painting are in the same house
92 yellow_index = color_combo.index('yellow')
93 if hobby_combo[yellow_index] != 'painting':
94 continue
95
96 # Constraint 13: The person who plays football drinks orange juice
97 # Edge Case: Find where football and orange juice are in the same house
98 football_index = hobby_combo.index('football')
99 if drink_combo[football_index] != 'orange juice':
100 continue
101
102 # Constraint 14: The Japanese person plays chess
103 # Edge Case: Find where Japanese and chess are in the same house
104 japanese_index = nationality_combo.index('Japanese')
105 if hobby_combo[japanese_index] != 'chess':
106 continue
107
108 # Constraint 11: The person who enjoys reading lives in the house next to the person with the fox
109 # Edge Case: Find where reading and fox are in adjacent houses
110 reading_index = hobby_combo.index('reading')
111 fox_index = pet_combo.index('fox')
112 if abs(reading_index - fox_index) != 1:
113 continue
114
115 # Constraint 12: The painter's house is next to the house with the horse
116 # Edge Case: Find where painting and horse are in adjacent houses
117 painting_index = hobby_combo.index('painting')
118 horse_index = pet_combo.index('horse')
119 if abs(painting_index - horse_index) != 1:
120 continue
121
122 # If all constraints are satisfied, we have a solution
123 solution = []
124 for i in range(5):
125 solution.append((
126 color_combo[i],
127 nationality_combo[i],
128 pet_combo[i],
129 drink_combo[i],
130 hobby_combo[i]
131 ))
132 return solution
133
134 # Handled Edge Cases: All constraints from the puzzle as edge cases
135 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.