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 resident in solution:
7 if resident['drink'] == 'water':
8 return resident['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 resident in solution:
15 if resident['pet'] == 'zebra':
16 return resident['nationality']
17 return ''
18
19def solve_puzzle():
20 # Define the possible values for each attribute
21 nationalities = ['Englishman', 'Spaniard', 'Ukrainian', 'Norwegian', 'Japanese']
22 colors = ['red', 'green', 'ivory', 'yellow', 'blue']
23 pets = ['dog', 'snail', '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 for nationality_perm in itertools.permutations(nationalities):
29 # Constraint 10: The Norwegian lives in the first house
30 if nationality_perm[0] != 'Norwegian':
31 continue
32
33 for color_perm in itertools.permutations(colors):
34 # Constraint 2: The Englishman lives in the red house
35 if not matches(nationality_perm, color_perm, 'Englishman', 'red'):
36 continue
37
38 # Constraint 6: The green house is immediately to the right of the ivory house
39 if not right_of(color_perm, 'green', 'ivory'):
40 continue
41
42 # Constraint 15: The Norwegian lives next to the blue house
43 if not adjacent(nationality_perm, color_perm, 'Norwegian', 'blue'):
44 continue
45
46 for pet_perm in itertools.permutations(pets):
47 # Constraint 3: The Spaniard owns the dog
48 if not matches(nationality_perm, pet_perm, 'Spaniard', 'dog'):
49 continue
50
51 for drink_perm in itertools.permutations(drinks):
52 # Constraint 4: The person in the green house drinks coffee
53 if not matches(color_perm, drink_perm, 'green', 'coffee'):
54 continue
55
56 # Constraint 5: The Ukrainian drinks tea
57 if not matches(nationality_perm, drink_perm, 'Ukrainian', 'tea'):
58 continue
59
60 # Constraint 9: The person in the middle house drinks milk
61 if drink_perm[2] != 'milk':
62 continue
63
64 for hobby_perm in itertools.permutations(hobbies):
65 # Constraint 7: The snail owner likes to go dancing
66 if not matches(pet_perm, hobby_perm, 'snail', 'dancing'):
67 continue
68
69 # Constraint 8: The person in the yellow house is a painter
70 if not matches(color_perm, hobby_perm, 'yellow', 'painting'):
71 continue
72
73 # Constraint 11: The person who enjoys reading lives in the house next to the person with the fox
74 if not adjacent(hobby_perm, pet_perm, 'reading', 'fox'):
75 continue
76
77 # Constraint 12: The painter's house is next to the house with the horse
78 if not adjacent(hobby_perm, pet_perm, 'painting', 'horse'):
79 continue
80
81 # Constraint 13: The person who plays football drinks orange juice
82 if not matches(hobby_perm, drink_perm, 'football', 'orange juice'):
83 continue
84
85 # Constraint 14: The Japanese person plays chess
86 if not matches(nationality_perm, hobby_perm, 'Japanese', 'chess'):
87 continue
88
89 # If all constraints are satisfied, construct the solution
90 solution = []
91 for i in range(5):
92 resident = {
93 'nationality': nationality_perm[i],
94 'color': color_perm[i],
95 'pet': pet_perm[i],
96 'drink': drink_perm[i],
97 'hobby': hobby_perm[i]
98 }
99 solution.append(resident)
100 return solution
101
102 return []
103
104def matches(attr1_perm, attr2_perm, attr1_value, attr2_value):
105 """Check if attr1_value and attr2_value occur in the same position in their respective permutations."""
106 for i in range(len(attr1_perm)):
107 if attr1_perm[i] == attr1_value and attr2_perm[i] == attr2_value:
108 return True
109 return False
110
111def right_of(attr_perm, right_value, left_value):
112 """Check if right_value is immediately to the right of left_value in the permutation."""
113 for i in range(len(attr_perm) - 1):
114 if attr_perm[i] == left_value and attr_perm[i + 1] == right_value:
115 return True
116 return False
117
118def adjacent(attr1_perm, attr2_perm, attr1_value, attr2_value):
119 """Check if attr1_value and attr2_value are in adjacent positions."""
120 positions1 = [i for i, x in enumerate(attr1_perm) if x == attr1_value]
121 positions2 = [i for i, x in enumerate(attr2_perm) if x == attr2_value]
122
123 # Edge Case: Handle case where values might not exist (though they should in valid permutations)
124 if not positions1 or not positions2:
125 return False
126
127 pos1 = positions1[0]
128 pos2 = positions2[0]
129 return abs(pos1 - pos2) == 1
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.