Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Using this code implement backtracking in the code to print the solution: from itertools import permutations as perm def dict _ perms ( category _
Using this code implement backtracking in the code to print the solution:
from itertools import permutations as perm
def dictperms categorylist :
for p in permrange: yield a:b for ab in zipcategorylist,p
def keyd v: # find a key in dictionary d with value v
return name for name, val in ditems if val v
colours colour for colour in dictpermsRed'Green','Indigo','Yellow','Blue'
if colourGreen colourIndigo
colournation colournation for colour in colours
for nation in dictpermsEnglishman'Spaniard','Ukranian','Norweigan','Japanese'
if nationNorweigan
and colourRed nationEnglishman
and absnationNorweigan colourBlue
colournationdrink colour nation, drink for colour,nation in colournation
for drink in dictpermsCoffee'Milk','Orange Juice','Water','Tea'
if drinkCoffee colourGreen
and nationUkranian drinkTea
and drinkMilk
colournationdrinksmoke colour nation, drink, smoke
for colour, nation, drink in colournationdrink
for smoke in dictpermsOld Gold','Kools','Chesterfield','Lucky Strike','Parliament'
if smokeKools colourYellow
and smokeLucky Strike' drinkOrange Juice'
and nationJapanese smokeParliament
solutions colour nation, drink, smoke, pet
for colour, nation, drink, smoke in colournationdrinksmoke
for pet in dictpermsDog'Snails','Fox','Horse','Zebra'
if smokeOld Gold' petSnails
and abssmokeChesterfield petFox
and abssmokeKools petHorse
and nationSpaniard petDog
if lensolutions:
print "multiple solutions"
else:
colournation,drink,smoke,pet solutions
for pos in range:
print pos, : keycolourposkeynationposkeydrinkposkeysmokeposkeypetpos
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