Skip to content

Commit 3af8795

Browse files
authored
Create puzzle.py
1 parent 2de73ac commit 3af8795

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

Chapter C2/puzzle.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from logpy import *
2+
from logpy.core import lall
3+
4+
# Declare the variable
5+
people = var()
6+
7+
# Define the rules
8+
rules = lall(
9+
# There are 4 people
10+
(eq, (var(), var(), var(), var()), people),
11+
12+
# Steve's car is blue
13+
(membero, ('Steve', var(), 'blue', var()), people),
14+
15+
# Person who owns the cat lives in Canada
16+
(membero, (var(), 'cat', var(), 'Canada'), people),
17+
18+
# Matthew lives in USA
19+
(membero, ('Matthew', var(), var(), 'USA'), people),
20+
21+
# The person who has a black car lives in Australia
22+
(membero, (var(), var(), 'black', 'Australia'), people),
23+
24+
# Jack has a cat
25+
(membero, ('Jack', 'cat', var(), var()), people),
26+
27+
# Alfred lives in Australia
28+
(membero, ('Alfred', var(), var(), 'Australia'), people),
29+
30+
# Person who owns the dog lives in France
31+
(membero, (var(), 'dog', var(), 'France'), people),
32+
33+
# Who is the owner of the rabbit?
34+
(membero, (var(), 'rabbit', var(), var()), people)
35+
)
36+
37+
# Run the solver
38+
solutions = run(0, people, rules)
39+
40+
# Extract the output
41+
output = [house for house in solutions[0] if 'rabbit' in house][0][0]
42+
43+
# Print the output
44+
print('\n' + output + ' is the owner of the rabbit')
45+
print('\nHere are all the details:')
46+
attribs = ['Name', 'Pet', 'Color', 'Country']
47+
print('\n' + '\t\t'.join(attribs))
48+
print('=' * 57)
49+
for item in solutions[0]:
50+
print('')
51+
print('\t\t'.join([str(x) for x in item]))

0 commit comments

Comments
 (0)