Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This code won't output anything: def print _ solution ( houses ) : for i , house in enumerate ( houses ) : print (

This code won't output anything:
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']}
")
def solve_houses():
houses =[{'nationality': 'Norwegian', 'color': 'yellow', 'drink': 'milk', 'smoke': 'Dunhills', 'pet': 'walleye pike'},
{'nationality': 'Brit', 'color': 'red', 'drink': 'tea', 'smoke': 'Blends', 'pet': 'birds'},
{'nationality': 'Swede', 'color': 'blue', 'drink': 'coffee', 'smoke': 'Pall Malls', 'pet': 'dogs'},
{'nationality': 'Dane', 'color': 'white', 'drink': 'tea', 'smoke': 'Princes', 'pet': 'cats'},
{'nationality': 'German', 'color': 'green', 'drink': 'beer', 'smoke': 'Bluemasters', 'pet': 'horses'}]
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']}
")
def backtrack(house_index):
if house_index == len(houses):
return True
for house in houses:
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_index)
return False
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'
elif house_index ==4:
return houses[4]['nationality']== 'Swede' and houses[4]['color']== 'white' and houses[4]['drink']== 'beer' and houses[4]['smoke']== 'Blue Masters' and houses[4]['pet']== 'dogs'
def reset_house(house):
initial_house ={'nationality': 'Norwegian', 'color': 'yellow', 'drink': 'milk', 'smoke': 'Dunhills', 'pet': 'walleye pike'}
for key, value in initial_house.items():
house[key]= value
if __name__=="__main__":
houses =[{'nationality': 'Norwegian', 'color': 'yellow', 'drink': 'milk', 'smoke': 'Dunhills', 'pet': 'walleye pike'},
{'nationality': 'Brit', 'color': 'red', 'drink': 'tea', '

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

Real Time Database Systems Architecture And Techniques

Authors: Kam-Yiu Lam ,Tei-Wei Kuo

1st Edition

1475784023, 978-1475784022

More Books

Students also viewed these Databases questions

Question

Explain in detail the different methods of performance appraisal .

Answered: 1 week ago