🤖 AI Programming in Python 💻
Here is the Question ❓:
First, let's take the necessary Imports
from kanren import *
from kanren.core import lall
Here are two Functions Return by me to Calculate the result
def left_to(q, p, list):
return membero((q, p), zip(list,list[1:]))
def next_to(q, p, list):
return conde([left_to(q, p, list)],[left_to(p, q, list)])
Code to find who owns Zebra and who drinks water 🦓 💧
houses = var()
find = lall(
(eq, (var(),var(),var(),var(),var()), houses),
(membero, ("Englishman",var(),var(),var(),"red"), houses),
(membero, ("Swede",var(),var(),"dog",var()), houses),
(membero, ("Dane",var(),"tea",var(),var()), houses),
(left_to, (var(),var(),var(),var(),"green"),(var(),var(),var(),var(),"white"), houses),
(membero, (var(), var(), 'coffee', var(), 'green'), houses),
(membero, (var(), 'Pall Mall', var(), 'birds', var()), houses),
(membero, (var(), 'Dunhill', var(), var(), 'yellow'), houses),
(eq, (var(), var(), (var(), var(), 'milk', var(), var()), var(), var()), houses),
(eq, (('Norwegian', var(), var(), var(), var()), var(), var(), var(), var()), houses),
(next_to, (var(), 'Blend', var(), var(), var()),(var(), var(), var(), 'cats', var()), houses),
(next_to, (var(), 'Dunhill', var(), var(), var()),(var(), var(), var(), 'horse', var()), houses),
(membero, (var(), 'Blue Master', 'beer', var(), var()), houses),
(membero, ('German', 'Prince', var(), var(), var()), houses),
(next_to, ('Norwegian', var(), var(), var(), var()),(var(), var(), var(), var(), 'blue'), houses),
(next_to, (var(), 'Blend', var(), var(), var()),(var(), var(), 'water', var(), var()), houses),
(membero, (var(), var(), var(), 'zebra', var()), houses)
)
Main Program 👨🏻💻
solution = run(0,houses,find)
zebra = [house for house in solution[0] if 'zebra' in house][0][0]
water = [house for house in solution[0] if 'water' in house][0][0]
print("Solution for the puzzle:\n")
print(f"{water} drinks water")
print(f"{zebra} own zebra")