| 1 | from itertools import permutations | |
| 2 | ||
| 3 | ||
| 1 | 4 | def drinks_water() -> str: |
| 2 | pass | |
| 5 | """ | |
| 6 | Solve the Zebra Puzzle to find who drinks water. | |
| 7 | Returns the nationality of the person who drinks water. | |
| 8 | """ | |
| 9 | solution = solve_zebra_puzzle() | |
| 10 | return solution['water_drinker'] | |
| 3 | 11 | |
| 4 | 12 | |
| 5 | 13 | def owns_zebra() -> str: |
| 6 | pass | |
| 14 | """ | |
| 15 | Solve the Zebra Puzzle to find who owns the zebra. | |
| 16 | Returns the nationality of the person who owns the zebra. | |
| 17 | """ | |
| 18 | solution = solve_zebra_puzzle() | |
| 19 | return solution['zebra_owner'] | |
| 20 | ||
| 21 | ||
| 22 | def solve_zebra_puzzle(): | |
| 23 | """ | |
| 24 | Solve the Zebra Puzzle using constraint satisfaction. | |
| 25 | Returns a dictionary with the answers to the puzzle. | |
| 26 | """ | |
| 27 | # Define the possible values for each category | |
| 28 | colors = ['red', 'green', 'ivory', 'yellow', 'blue'] | |
| 29 | nationalities = ['Englishman', 'Spaniard', 'Ukrainian', 'Norwegian', 'Japanese'] | |
| 30 | pets = ['dog', 'snail', 'fox', 'horse', 'zebra'] | |
| 31 | drinks = ['coffee', 'tea', 'milk', 'orange juice', 'water'] | |
| 32 | hobbies = ['dancing', 'painting', 'reading', 'football', 'chess'] | |
| 33 | ||
| 34 | # Try all permutations of colors | |
| 35 | for colors_perm in permutations(colors): | |
| 36 | # Constraint 10: Norwegian lives in the first house | |
| 37 | # Constraint 15: Norwegian lives next to the blue house | |
| 38 | # So blue must be in position 2 | |
| 39 | if colors_perm[1] != 'blue': | |
| 40 | continue | |
| 41 | ||
| 42 | # Constraint 6: green house is immediately to the right of ivory house | |
| 43 | try: | |
| 44 | ivory_index = colors_perm.index('ivory') | |
| 45 | if ivory_index == 4 or colors_perm[ivory_index + 1] != 'green': | |
| 46 | continue | |
| 47 | except ValueError: | |
| 48 | continue | |
| 49 | ||
| 50 | # Try all permutations of nationalities | |
| 51 | for nat_perm in permutations(nationalities): | |
| 52 | # Constraint 10: Norwegian lives in the first house | |
| 53 | if nat_perm[0] != 'Norwegian': | |
| 54 | continue | |
| 55 | ||
| 56 | # Constraint 2: Englishman lives in the red house | |
| 57 | try: | |
| 58 | english_index = nat_perm.index('Englishman') | |
| 59 | if colors_perm[english_index] != 'red': | |
| 60 | continue | |
| 61 | except ValueError: | |
| 62 | continue | |
| 63 | ||
| 64 | # Try all permutations of drinks | |
| 65 | for drinks_perm in permutations(drinks): | |
| 66 | # Constraint 9: person in the middle house drinks milk | |
| 67 | if drinks_perm[2] != 'milk': | |
| 68 | continue | |
| 69 | ||
| 70 | # Constraint 4: person in the green house drinks coffee | |
| 71 | try: | |
| 72 | green_index = colors_perm.index('green') | |
| 73 | if drinks_perm[green_index] != 'coffee': | |
| 74 | continue | |
| 75 | except ValueError: | |
| 76 | continue | |
| 77 | ||
| 78 | # Constraint 5: Ukrainian drinks tea | |
| 79 | try: | |
| 80 | ukrainian_index = nat_perm.index('Ukrainian') | |
| 81 | if drinks_perm[ukrainian_index] != 'tea': | |
| 82 | continue | |
| 83 | except ValueError: | |
| 84 | continue | |
| 85 | ||
| 86 | # Try all permutations of pets | |
| 87 | for pets_perm in permutations(pets): | |
| 88 | # Constraint 3: Spaniard owns the dog | |
| 89 | try: | |
| 90 | spaniard_index = nat_perm.index('Spaniard') | |
| 91 | if pets_perm[spaniard_index] != 'dog': | |
| 92 | continue | |
| 93 | except ValueError: | |
| 94 | continue | |
| 95 | ||
| 96 | # Try all permutations of hobbies | |
| 97 | for hobbies_perm in permutations(hobbies): | |
| 98 | # Constraint 8: person in the yellow house is a painter | |
| 99 | try: | |
| 100 | yellow_index = colors_perm.index('yellow') | |
| 101 | if hobbies_perm[yellow_index] != 'painting': | |
| 102 | continue | |
| 103 | except ValueError: | |
| 104 | continue | |
| 105 | ||
| 106 | # Constraint 7: snail owner likes to go dancing | |
| 107 | try: | |
| 108 | snail_index = pets_perm.index('snail') | |
| 109 | if hobbies_perm[snail_index] != 'dancing': | |
| 110 | continue | |
| 111 | except ValueError: | |
| 112 | continue | |
| 113 | ||
| 114 | # Constraint 13: person who plays football drinks orange juice | |
| 115 | try: | |
| 116 | football_index = hobbies_perm.index('football') | |
| 117 | if drinks_perm[football_index] != 'orange juice': | |
| 118 | continue | |
| 119 | except ValueError: | |
| 120 | continue | |
| 121 | ||
| 122 | # Constraint 14: Japanese person plays chess | |
| 123 | try: | |
| 124 | japanese_index = nat_perm.index('Japanese') | |
| 125 | if hobbies_perm[japanese_index] != 'chess': | |
| 126 | continue | |
| 127 | except ValueError: | |
| 128 | continue | |
| 129 | ||
| 130 | # Constraint 11: person who enjoys reading lives next to person with fox | |
| 131 | try: | |
| 132 | reading_index = hobbies_perm.index('reading') | |
| 133 | fox_index = pets_perm.index('fox') | |
| 134 | if abs(reading_index - fox_index) != 1: | |
| 135 | continue | |
| 136 | except ValueError: | |
| 137 | continue | |
| 138 | ||
| 139 | # Constraint 12: painter's house is next to house with horse | |
| 140 | try: | |
| 141 | painter_index = hobbies_perm.index('painting') | |
| 142 | horse_index = pets_perm.index('horse') | |
| 143 | if abs(painter_index - horse_index) != 1: | |
| 144 | continue | |
| 145 | except ValueError: | |
| 146 | continue | |
| 147 | ||
| 148 | # All constraints satisfied! Find the answers | |
| 149 | water_index = drinks_perm.index('water') | |
| 150 | zebra_index = pets_perm.index('zebra') | |
| 151 | ||
| 152 | return { | |
| 153 | 'water_drinker': nat_perm[water_index], | |
| 154 | 'zebra_owner': nat_perm[zebra_index] | |
| 155 | } | |
| 156 | ||
| 157 | # No solution found (shouldn't happen with valid puzzle) | |
| 158 | return { | |
| 159 | 'water_drinker': 'Unknown', | |
| 160 | 'zebra_owner': 'Unknown' | |
| 161 | } |
| 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.
