Question: 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
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
