Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
1.9 kB
4
Indexable
Never
Create a new Python file & import from packages.

from logpy import *
from logpy.core import lall

Specified the variable person.

# Specified the variable
person = var()

Specified all the rules using lall. The first rule is that there are four persons.

# Specified the rules
rules = lall(
# There are 4 persons
(eq, (var(), var(), var(), var()), person),

The people named Steve has a blue car-

# Steves has a blue car
(membero, ('Steve', var(), 'blue', var()), person),

The man who has a cat lives in Canada:

# The man who has a cat lives in Canada
(membero, (var(), 'cat', var(), 'Canada'), person),

# Matthew lives in USA
(membero, ('Matthew', var(), var(), 'USA'), person),

# The man who has a black car lives in Australia
(membero, (var(), var(), 'black', 'Australia'), person),

The man named Jack has a cat:

# Jack has a cat
(membero, ('Jack', 'cat', var(), var()), person),

The man named Alfred lives in Australia:

# Alfred lives in Australia
(membero, ('Alfred', var(), var(), 'Australia'), person),

The man who has a dog lives in France:

# Person who owns the dog lives in France
(membero, (var(), 'dog', var(), 'France'), person),

One of the person in this group has a rabbit. Who is that man?

# Who has a rabbit?
(membero, (var(), 'rabbit', var(), var()), person)
)

Run the solver with the preceding constraints:

# Run the solver
results = run(0, person, rules)

Take out the output from the result-

output = [house for house in results[0] if 'rabbit' in house][0][0]

print the o/p:

print('\n' + output + ' is the owning of the rabbit')

print('\nAll the details are here:')

attribs = ['Name', 'Pet', 'Car Color', 'Country']

print('\n' + '\t\t\t'.join(attribs))

print('=' * 60)

for item in result[0]:

print('')

print('\t\t\t'.join([str(x) for x in item]))

Matthew is the boss of the rabbit.