Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Is it possible to implement backtracking with this code manually: WHO OWNS THE FISH PROGRAM Names: Emily Dogbatse, Davis Carson Solves the
Is it possible to implement backtracking with this code manually:
WHO OWNS THE FISH PROGRAM
Names: Emily Dogbatse, Davis Carson
Solves the algorithm with backtracking
Expectation: The German owns the fish.
# printing out the board
def printsolutionhouses:
for i house in enumeratehouses:
printfHouse i :
printf Nationality: housenationality
printf Color: housecolor
printf Drink: housedrink
printf Smoke: housesmoke
printf Pet: housepet
# function to solve the actual puzzle
def solvehouses:
houses nationality: 'Norwegian', 'color': 'yellow', 'drink': 'water', 'smoke': 'Dunhills', 'pet': 'walleye pike'
nationality: 'Dane', 'color': 'blue', 'drink': 'tea', 'smoke': 'Blends', 'pet': 'horses'
nationality: 'Brit', 'color': 'red', 'drink': 'milk', 'smoke': 'Pall Malls', 'pet': 'birds'
nationality: 'German', 'color': 'green', 'drink': 'coffee', 'smoke': 'Princes', 'pet': 'FISH'
nationality: 'Swede', 'color': 'white', 'drink': 'beer', 'smoke': 'Bluemasters', 'pet': 'dogs'
def backtrackhouseindex:
if houseindex lenhouses:
return True
house houseshouseindex # Get the current house being considered
# Manipulate attributes for the current house
if houseindex :
housecolor 'yellow'
elif houseindex :
housecolor 'blue'
elif houseindex :
housecolor 'red'
elif houseindex :
housecolor 'green'
elif houseindex :
housecolor 'white'
if houseindex :
housenationality 'Norwegian'
elif houseindex :
housenationality 'Dane'
elif houseindex :
housenationality 'Brit'
elif houseindex :
housenationality 'German'
elif houseindex :
housenationality 'Swede'
if houseindex :
housedrink 'water'
elif houseindex :
housedrink 'tea'
elif houseindex :
housedrink 'milk'
elif houseindex :
housedrink 'coffee'
elif houseindex :
housedrink 'beer'
if houseindex :
housesmoke 'Dunhills'
elif houseindex :
housesmoke 'Blends'
elif houseindex :
housesmoke 'Pall Malls'
elif houseindex :
housesmoke 'Princes'
elif houseindex :
housesmoke 'Bluemasters'
if houseindex :
housepet 'cat'
elif houseindex :
housepet 'horses'
elif houseindex :
housepet 'birds'
elif houseindex :
housepet 'fish'
elif houseindex :
housepet 'dogs'
if checkconstraintshouseindex:
if backtrackhouseindex :
return True
resethousehouse # Reset attributes for the current house
return False
# checking the constraints of the houses
def checkconstraintshouseindex:
if houseindex :
return housesnationality 'Norwegian' and housescolor 'yellow' and housesdrink 'water' and housessmoke 'Dunhills' and housespet 'cats'
elif houseindex :
return housesnationality 'Dane' and housescolor 'blue' and housesdrink 'tea' and housessmoke 'Blends' and housespet 'horses'
elif houseindex :
return housesnationality 'Brit' and housescolor 'red' and housesdrink 'milk' and housessmoke 'Pall Malls' and housespet 'birds'
elif houseindex :
return housesnationality 'German' and housescolor 'green' and housesdrink 'coffee' and housessmoke 'Princes' and housespet 'fish :D # Adjusted constraint for German house
elif houseindex :
return housesnationality 'Swede' and housescolor 'white' and housesdrink 'beer' and housessmoke 'Bluemasters' and housespet 'dogs'
def resethousehouse:
initialhouse nationality: 'Norwegian', 'color': 'yellow', 'drink': 'water', 'smoke': 'Dunhills', 'pet': 'cats'
for key, value in initialhouse.items:
housekey value
backtrack # Call backtrack function with initial house index
printsolutionhouses # Print the solution after solving
if namemain:
solvehouses # Call solvehouses function to solve the houses
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started