Zebra puzzle: Difference between revisions

Used more intelligible and readable variables.
m (clarity of problem description. constraints.)
imported>SNWI
(Used more intelligible and readable variables.)
Line 5,985:
Using 'python-constraint': http://labix.org/python-constraint,
Original source code is 'ECLiPSe ( http://eclipseclp.org/ )' example: http://eclipseclp.org/examples/zebra.ecl.txt
<syntaxhighlight lang="python">from constraint import *'''
Improved version: Instead of simple variables (a, b) more intelligble and readable (?) vartiables are emplyed.
'''
def doc1():
'''
There are five houses.
The English man lives in the red house.
The Swede has a dog.
The Dane drinks tea.
The green house is immediately to the left of the white house.
They drink coffee in the green house.
The man who smokes Pall Mall has a bird.
In the yellow house they smoke Dunhill.
In the middle house they drink milk.
The Norwegian lives in the first house.
The man who smokes Blend lives in the house next to the house with a cat.
In a house next to the house where they have a horse, they smoke Dunhill.
The man who smokes Blue Master drinks beer.
The German smokes Prince.
The Norwegian lives next to the blue house.
They drink water in a house next to the house where they smoke Blend.
 
The solution:
problem = Problem()
 
Nation: Norwegian Dane = ["Englishman", "Spaniard", "Japanese", "Ukrainian", German "Norwegian" ]Swede
Color: = ["Red", Yellow "Green", "White", Blue "Yellow", "Blue" Red ] Green White
Smoke: Dunhill Blend PallMall Prince BlueMaster
Smoke = ["Oldgold", "Kools", "Chesterfield", "Luckystrike", "Parliament"]
Pet = ["Dog", Pet: "Snails", "Fox", Cat "Horse", " Bird Zebra" ] Dog
Drink: = ["Tea", Water Tea "Coffee", "Milk", Coffee "Orangejuice", "Water" ]Beer
'''
 
import os
print(os.system('cls') if os.name == 'nt' else os.system('clear'))
 
print(doc1.__doc__)
input('<key>')
 
from constraint import *
 
p = Problem()
 
Nation = ['Englishman', 'Dane', 'German', 'Norwegian', 'Swede']
Color = [ 'Blue', 'Green', 'Red', 'White', 'Yellow']
Smoke = [ 'Blend', 'BlueMaster', 'Dunhill', 'PallMall', 'Prince']
Pet = [ 'Bird', 'Cat', 'Dog', 'Horse', 'Zebra']
Drink = [ 'Beer', 'Coffee', 'Milk', 'Tea', 'Water']
 
# add variables: house numbers 1 to 5
problemp.addVariables(Nation, range(1,5+16))
problemp.addVariables(Color, range(1,5+16))
problemp.addVariables(Smoke, range(1,5+16))
problemp.addVariables(Pet, range(1,5+16))
problemp.addVariables(Drink, range(1,5+16))
 
# add constraint: the values in each list are exclusive
problemp.addConstraint(AllDifferentConstraint(), Nation)
problemp.addConstraint(AllDifferentConstraint(), Color)
problemp.addConstraint(AllDifferentConstraint(), Smoke)
problemp.addConstraint(AllDifferentConstraint(), Pet)
problemp.addConstraint(AllDifferentConstraint(), Drink)
 
# add constraint: actual constraints
# The English man lives in the red house.
problem.addConstraint(lambda a, b: a == b, ["Englishman", "Red" ])
p.addConstraint(
problem.addConstraint(lambda a, b: a == b, ["Spaniard", "Dog" ])
lambda house_englishman, red:
problem.addConstraint(lambda a, b: a == b, ["Green", "Coffee" ])
house_englishman is red,
problem.addConstraint(lambda a, b: a == b, ["Ukrainian", "Tea" ])
['Englishman', 'Red'])
problem.addConstraint(lambda a, b: a == b + 1, ["Green", "White" ])
 
problem.addConstraint(lambda a, b: a == b, ["Oldgold", "Snails" ])
# The Swede has a dog.
problem.addConstraint(lambda a, b: a == b, ["Yellow", "Kools" ])
p.addConstraint(
problem.addConstraint(lambda a: a == 3, ["Milk" ])
lambda pet_swede, dog:
problem.addConstraint(lambda a: a == 1, ["Norwegian" ])
problem.addConstraint(lambda a, b: a ==pet_swede is dog, b - 1 or a == b + 1, ["Chesterfield", "Fox" ])
['Swede', 'Dog'])
problem.addConstraint(lambda a, b: a == b - 1 or a == b + 1, ["Kools", "Horse" ])
 
problem.addConstraint(lambda a, b: a == b, ["Luckystrike", "Orangejuice"])
# The Dane drinks tea.
problem.addConstraint(lambda a, b: a == b, ["Japanese", "Parliament" ])
p.addConstraint(
problem.addConstraint(lambda a, b: a == b - 1 or a == b + 1, ["Norwegian", "Blue" ])
lambda drink_dane, tea:
drink_dane is tea,
['Dane', 'Tea'])
 
# The green house is immediately to the left of the white house.
p.addConstraint(
lambda green_house, white_house:
# Houses 1 .. 5 -> green house is 1 lower than white house
green_house is white_house - 1,
['Green', 'White'])
 
# They drink coffee in the green house.
p.addConstraint(
lambda drink_green_house, coffee:
drink_green_house is coffee,
['Green', 'Coffee'])
 
# The man who smokes Pall Mall has a bird.
p.addConstraint(
lambda pet_of_pallmall_smoker, a_bird:
pet_of_pallmall_smoker is a_bird,
['PallMall', 'Bird'])
 
# In the yellow house they smoke Dunhill.
p.addConstraint(
lambda owner_yellow_house, dunhill_smoker:
owner_yellow_house is dunhill_smoker,
['Yellow', 'Dunhill'])
 
# In the middle house they drink milk.
p.addConstraint(
lambda house_number_milk_drinker:
house_number_milk_drinker is 3,
['Milk'])
 
# The Norwegian lives in the first house.
p.addConstraint(
lambda house_number_norwegian:
house_number_norwegian is 1,
['Norwegian'])
 
# The man who smokes Blend lives in the house next to the house with a cat.
p.addConstraint(
lambda house_number_blend_smoker, number_of_house_with_cat:
# next -> housenumber +/- 1
house_number_blend_smoker is number_of_house_with_cat + 1 or
house_number_blend_smoker is number_of_house_with_cat - 1,
['Blend', 'Cat'])
 
# In a house next to the house where they have a horse, they smoke Dunhill.
p.addConstraint(
lambda house_number_dunhill_smoker, number_of_house_with_horse:
# next -> housenumber +/- 1
house_number_dunhill_smoker is number_of_house_with_horse + 1 or
house_number_dunhill_smoker is number_of_house_with_horse - 1,
['Dunhill', 'Horse'])
 
# The man who smokes Blue Master drinks beer.
p.addConstraint(
lambda drink_bluemaster_smoker, beer:
drink_bluemaster_smoker is beer,
['BlueMaster', 'Beer'])
 
# The German smokes Prince.
p.addConstraint(
lambda prince_smoker, german:
prince_smoker is german,
['Prince', 'German'])
 
# The Norwegian lives next to the blue house.
p.addConstraint(
lambda house_number_norwegian, house_number_blue_house:
house_number_norwegian is house_number_blue_house + 1 or
house_number_norwegian is house_number_blue_house - 1,
['Norwegian', 'Blue'])
 
# They drink water in a house next to the house where they smoke Blend.
p.addConstraint(
lambda house_number_water_drinker, house_number_blend_smoker:
house_number_water_drinker is house_number_blend_smoker + 1 or
house_number_water_drinker is house_number_blend_smoker - 1,
['Water', 'Blend'])
 
# get solution
sol = problemp.getSolution()
 
# print the answers
nation = ["'Nation"' if i == 0 else ""'' for i in range(6)]
color = ["'Color"' if i == 0 else ""'' for i in range(6)]
smoke = ["'Smoke"' if i == 0 else ""'' for i in range(6)]
pet = ["'Pet"' if i == 0 else ""'' for i in range(6)]
drink = ["'Drink"' if i == 0 else ""'' for i in range(6)]
 
for n in Nation:
for n in Nation: nation[sol[n]] = n
for n in Color: color[sol[n]] = n
for n in colorSmoke: smoke[sol[n]] = n
for n in Smoke Pet: pet[sol[n]] = n
for n in smokeDrink: drink[sol[n]] = n
 
for n in Pet:
# put the answers in the correct order of solution
pet[sol[n]] = n
print('\n\n', 'The calculated solution:', '\n')
for n in Drink:
 
drink[sol[n]] = n
for dr in [nation, color, smoke, pet, drink]:
print(f'{r[0]:>13s}: ', end='')
print("%6s: %14s%14s%14s%14s%14s" % (d[0], d[1], d[2], d[3], d[4], d[5]))
for i in range(1, 6):
print(f'{r[i]:>14s}',end='')
print()
print()
</syntaxhighlight>
Output:
Anonymous user