YAPC::EU 2018 Glasgow Update!
This commit is contained in:
parent
22f33d4004
commit
4e2d22a71d
1170 changed files with 15042 additions and 3047 deletions
54
Task/Zebra-puzzle/Prolog/zebra-puzzle-5.pro
Normal file
54
Task/Zebra-puzzle/Prolog/zebra-puzzle-5.pro
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
:- use_module(library(clpfd)).
|
||||
|
||||
zebra :-
|
||||
Nation = [Englishman, Spaniard, Japanese, Ukrainian, Norwegian ],
|
||||
Color = [Red, Green, White, Yellow, Blue ],
|
||||
Smoke = [Oldgold, Kools, Chesterfield, Luckystrike, Parliament],
|
||||
Pet = [Dog, Snails, Fox, Horse, Zebra ],
|
||||
Drink = [Tea, Coffee, Milk, Orangejuice, Water ],
|
||||
|
||||
% house numbers 1 to 5
|
||||
Nation ins 1..5,
|
||||
Color ins 1..5,
|
||||
Smoke ins 1..5,
|
||||
Pet ins 1..5,
|
||||
Drink ins 1..5,
|
||||
|
||||
% the values in each list are exclusive
|
||||
all_different(Nation),
|
||||
all_different(Color),
|
||||
all_different(Smoke),
|
||||
all_different(Pet),
|
||||
all_different(Drink),
|
||||
|
||||
% actual constraints
|
||||
Englishman #= Red,
|
||||
Spaniard #= Dog,
|
||||
Green #= Coffee,
|
||||
Ukrainian #= Tea,
|
||||
Green #= White + 1,
|
||||
Oldgold #= Snails,
|
||||
Yellow #= Kools,
|
||||
Milk #= 3,
|
||||
Norwegian #= 1,
|
||||
(Chesterfield #= Fox - 1 #\/ Chesterfield #= Fox + 1),
|
||||
(Kools #= Horse - 1 #\/ Kools #= Horse + 1),
|
||||
Luckystrike #= Orangejuice,
|
||||
Japanese #= Parliament,
|
||||
(Norwegian #= Blue - 1 #\/ Norwegian #= Blue + 1),
|
||||
|
||||
% get solution
|
||||
flatten([Nation, Color, Smoke, Pet, Drink], List), label(List),
|
||||
|
||||
% print the answers
|
||||
sort([Englishman-englishman, Spaniard-spaniard, Japanese-japanese, Ukrainian-ukrainian, Norwegian-norwegian], NationNames),
|
||||
sort([Red-red, Green-green, White-white, Yellow-yellow, Blue-bule], ColorNames),
|
||||
sort([Oldgold-oldgold, Kools-kools, Chesterfield-chesterfield, Luckystrike-luckystrike, Parliament-parliament], SmokeNames),
|
||||
sort([Dog-dog, Snails-snails, Fox-fox, Horse-horse, Zebra-zebra], PetNames),
|
||||
sort([Tea-tea, Coffee-coffee, Milk-milk, Orangejuice-orangejuice, Water-water], DrinkNames),
|
||||
Fmt = '~w~16|~w~32|~w~48|~w~64|~w~n',
|
||||
format(Fmt, NationNames),
|
||||
format(Fmt, ColorNames),
|
||||
format(Fmt, SmokeNames),
|
||||
format(Fmt, PetNames),
|
||||
format(Fmt, DrinkNames).
|
||||
61
Task/Zebra-puzzle/Python/zebra-puzzle-4.py
Normal file
61
Task/Zebra-puzzle/Python/zebra-puzzle-4.py
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
from constraint import *
|
||||
|
||||
problem = Problem()
|
||||
|
||||
Nation = ["Englishman", "Spaniard", "Japanese", "Ukrainian", "Norwegian" ]
|
||||
Color = ["Red", "Green", "White", "Yellow", "Blue" ]
|
||||
Smoke = ["Oldgold", "Kools", "Chesterfield", "Luckystrike", "Parliament"]
|
||||
Pet = ["Dog", "Snails", "Fox", "Horse", "Zebra" ]
|
||||
Drink = ["Tea", "Coffee", "Milk", "Orangejuice", "Water" ]
|
||||
|
||||
# add variables: house numbers 1 to 5
|
||||
problem.addVariables(Nation, range(1,5+1))
|
||||
problem.addVariables(Color, range(1,5+1))
|
||||
problem.addVariables(Smoke, range(1,5+1))
|
||||
problem.addVariables(Pet, range(1,5+1))
|
||||
problem.addVariables(Drink, range(1,5+1))
|
||||
|
||||
# add constraint: the values in each list are exclusive
|
||||
problem.addConstraint(AllDifferentConstraint(), Nation)
|
||||
problem.addConstraint(AllDifferentConstraint(), Color)
|
||||
problem.addConstraint(AllDifferentConstraint(), Smoke)
|
||||
problem.addConstraint(AllDifferentConstraint(), Pet)
|
||||
problem.addConstraint(AllDifferentConstraint(), Drink)
|
||||
|
||||
# add constraint: actual constraints
|
||||
problem.addConstraint(lambda a, b: a == b, ["Englishman", "Red" ])
|
||||
problem.addConstraint(lambda a, b: a == b, ["Spaniard", "Dog" ])
|
||||
problem.addConstraint(lambda a, b: a == b, ["Green", "Coffee" ])
|
||||
problem.addConstraint(lambda a, b: a == b, ["Ukrainian", "Tea" ])
|
||||
problem.addConstraint(lambda a, b: a == b + 1, ["Green", "White" ])
|
||||
problem.addConstraint(lambda a, b: a == b, ["Oldgold", "Snails" ])
|
||||
problem.addConstraint(lambda a, b: a == b, ["Yellow", "Kools" ])
|
||||
problem.addConstraint(lambda a: a == 3, ["Milk" ])
|
||||
problem.addConstraint(lambda a: a == 1, ["Norwegian" ])
|
||||
problem.addConstraint(lambda a, b: a == b - 1 or a == b + 1, ["Chesterfield", "Fox" ])
|
||||
problem.addConstraint(lambda a, b: a == b - 1 or a == b + 1, ["Kools", "Horse" ])
|
||||
problem.addConstraint(lambda a, b: a == b, ["Luckystrike", "Orangejuice"])
|
||||
problem.addConstraint(lambda a, b: a == b, ["Japanese", "Parliament" ])
|
||||
problem.addConstraint(lambda a, b: a == b - 1 or a == b + 1, ["Norwegian", "Blue" ])
|
||||
|
||||
# get solution
|
||||
sol = problem.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:
|
||||
nation[sol[n]] = n
|
||||
for n in Color:
|
||||
color[sol[n]] = n
|
||||
for n in Smoke:
|
||||
smoke[sol[n]] = n
|
||||
for n in Pet:
|
||||
pet[sol[n]] = n
|
||||
for n in Drink:
|
||||
drink[sol[n]] = n
|
||||
for d in [nation, color, smoke, pet, drink]:
|
||||
print("%6s: %14s%14s%14s%14s%14s" % (d[0], d[1], d[2], d[3], d[4], d[5]))
|
||||
Loading…
Add table
Add a link
Reference in a new issue