Answered step by step
Verified Expert Solution
Link Copied!

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 print_solution(houses):
for i, house in enumerate(houses):
print(f"House {i +1}:")
print(f"- Nationality: {house['nationality']}")
print(f"- Color: {house['color']}")
print(f"- Drink: {house['drink']}")
print(f"- Smoke: {house['smoke']}")
print(f"- Pet: {house['pet']}
")
# function to solve the actual puzzle
def solve_houses():
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 backtrack(house_index):
if house_index == len(houses):
return True
house = houses[house_index] # Get the current house being considered
# Manipulate attributes for the current house
if house_index ==0:
house['color']= 'yellow'
elif house_index ==1:
house['color']= 'blue'
elif house_index ==2:
house['color']= 'red'
elif house_index ==3:
house['color']= 'green'
elif house_index ==4:
house['color']= 'white'
if house_index ==0:
house['nationality']= 'Norwegian'
elif house_index ==1:
house['nationality']= 'Dane'
elif house_index ==2:
house['nationality']= 'Brit'
elif house_index ==3:
house['nationality']= 'German'
elif house_index ==4:
house['nationality']= 'Swede'
if house_index ==0:
house['drink']= 'water'
elif house_index ==1:
house['drink']= 'tea'
elif house_index ==2:
house['drink']= 'milk'
elif house_index ==3:
house['drink']= 'coffee'
elif house_index ==4:
house['drink']= 'beer'
if house_index ==0:
house['smoke']= 'Dunhills'
elif house_index ==1:
house['smoke']= 'Blends'
elif house_index ==2:
house['smoke']= 'Pall Malls'
elif house_index ==3:
house['smoke']= 'Princes'
elif house_index ==4:
house['smoke']= 'Bluemasters'
if house_index ==0:
house['pet']= 'cat'
elif house_index ==1:
house['pet']= 'horses'
elif house_index ==2:
house['pet']= 'birds'
elif house_index ==3:
house['pet']= 'fish'
elif house_index ==4:
house['pet']= 'dogs'
if check_constraints(house_index):
if backtrack(house_index +1):
return True
reset_house(house) # Reset attributes for the current house
return False
# checking the constraints of the houses
def check_constraints(house_index):
if house_index ==0:
return houses[1]['nationality']== 'Norwegian' and houses[1]['color']== 'yellow' and houses[1]['drink']== 'water' and houses[1]['smoke']== 'Dunhills' and houses[1]['pet']== 'cats'
elif house_index ==1:
return houses[0]['nationality']== 'Dane' and houses[0]['color']== 'blue' and houses[0]['drink']== 'tea' and houses[0]['smoke']== 'Blends' and houses[0]['pet']== 'horses'
elif house_index ==2:
return houses[2]['nationality']== 'Brit' and houses[2]['color']== 'red' and houses[2]['drink']== 'milk' and houses[2]['smoke']== 'Pall Malls' and houses[2]['pet']== 'birds'
elif house_index ==3:
return houses[3]['nationality']== 'German' and houses[3]['color']== 'green' and houses[3]['drink']== 'coffee' and houses[3]['smoke']== 'Princes' and houses[3]['pet']== 'fish :D' # Adjusted constraint for German house
elif house_index ==4:
return houses[4]['nationality']== 'Swede' and houses[4]['color']== 'white' and houses[4]['drink']== 'beer' and houses[4]['smoke']== 'Bluemasters' and houses[4]['pet']== 'dogs'
def reset_house(house):
initial_house ={'nationality': 'Norwegian', 'color': 'yellow', 'drink': 'water', 'smoke': 'Dunhills', 'pet': 'cats'}
for key, value in initial_house.items():
house[key]= value
backtrack(0) # Call backtrack function with initial house index
print_solution(houses) # Print the solution after solving
if __name__=="__main__":
solve_houses() # Call solve_houses function to solve the houses

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Databases The Essentials

Authors: Mary E. S. Loomis

1st Edition

020156341X, 978-0201563412

More Books

Students also viewed these Databases questions