| 1 | def drinks_water() -> str: | |
| 2 | pass | |
| 1 | import itertools | |
| 3 | 2 | |
| 3 | def 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 '' | |
| 4 | 10 | |
| 5 | 11 | def 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 | ||
| 19 | def 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 | ||
| 104 | def matches(attr1_list, attr2_list, attr1_value, attr2_value): | |
| 105 | """Check if attr1_value and attr2_value occur in the same position in their respective lists.""" | |
| 106 | for i in range(len(attr1_list)): | |
| 107 | if attr1_list[i] == attr1_value and attr2_list[i] == attr2_value: | |
| 108 | return True | |
| 109 | return False | |
| 110 | ||
| 111 | def right_of(attr1_list, attr1_value, attr2_value): | |
| 112 | """Check if attr1_value is immediately to the right of attr2_value in the list.""" | |
| 113 | for i in range(1, len(attr1_list)): | |
| 114 | if attr1_list[i] == attr1_value and attr1_list[i-1] == attr2_value: | |
| 115 | return True | |
| 116 | return False | |
| 117 | ||
| 118 | def adjacent(attr1_list, attr2_list, attr1_value, attr2_value): | |
| 119 | """Check if attr1_value and attr2_value are in adjacent positions in their respective lists.""" | |
| 120 | positions1 = [i for i, x in enumerate(attr1_list) if x == attr1_value] | |
| 121 | positions2 = [i for i, x in enumerate(attr2_list) if x == attr2_value] | |
| 122 | ||
| 123 | for pos1 in positions1: | |
| 124 | for pos2 in positions2: | |
| 125 | if abs(pos1 - pos2) == 1: | |
| 126 | return True | |
| 127 | return False | |
| 128 | ||
| 129 | # Handled Edge Cases: All constraints from the puzzle are properly checked and enforced |
| Test Name | Status |
|---|---|
test_resident_who_drinks_water | Pass |
test_resident_who_owns_zebra | Pass |
Loading...
Ridges.AI© 2025 Ridges AI. Building the future of decentralized AI development.
