Elizabeth 的孙辈是:['Beatrice', 'James', 'William', 'Eugenie', 'Louise', 'Harry', 'Zara', 'Peter']
Traceback (most recent call last):
File "D:\Download\Netease\python\family tree.py", line 161, in <module>
print("Diana 的姐夫/妹夫是:"+str(BrotherInLaw("Diana")))
File "D:\Download\Netease\python\family tree.py", line 74, in BrotherInLaw
l1 = Sister(me) + Sister(spouse(me)) # 我所有的姐妹和我配偶的姐妹
File "D:\Download\Netease\python\family tree.py", line 67, in Sister
l2 = list(set(l2))
TypeError: 'NoneType' object is not iterable
[Finished in 0.4s with exit code 1]
from kanren import run, var
from kanren import Relation, facts
father = Relation()
mother = Relation()
facts(father, ("Charles", "William"), ("Charles", "Harry"), ("Mark", "Peter"), ("Mark", "Zara")
, ("Andrew", "Beatrice"), ("Andrew", "Eugenie"), ("Edward", "Louise"), ("Edward", "James")
, ("Spencer", "Diana"), ("Spencer", "Charles"), ("Philip", "Anne"), ("Philip", "Andrew")
, ("Philip", "Edward"), ("George", "Elizabeth"), ("George", "Margaret"))
facts(mother, ("Diana", "William"), ("Diana", "Harry"), ("Anne", "Peter"), ("Anne", "Zara")
, ("Sarah", "Beatrice"), ("Sarah", "Eugenie"), ("Sophie", "Louise"), ("Sophie", "James")
, ("Kydd", "Diana"), ("Elizabeth", "Charles"), ("Elizabeth", "Anne"), ("Elizabeth", "Andrew")
, ("Elizabeth", "Edward"), ("Mum", "Elizabeth"), ("Mum", "Margaret"))
man = ["William", "Harry", "Peter", "James", "Charles", "Mark", "Andrew", "Edward",
"Spencer", "Philip", "George"]
woman = ["Zara", "Beatrice", "Eugenie", "Louise", "Diana", "Anne", "Sarah", "Sophie",
"Kydd", "Elizabeth", "Margaret", "Mum"]
def GrandChild(grandparent):
someone = var()
someone_parent = var()
l1 = list(run(0, someone, father(grandparent, someone_parent), father(someone_parent, someone)) +
run(0, someone, mother(grandparent, someone_parent), father(someone_parent, someone)) +
run(0, someone, father(grandparent, someone_parent), mother(someone_parent, someone)) +
run(0, someone, mother(grandparent, someone_parent), mother(someone_parent, someone)))
return l1
def Son(parent):
someone = var()
l1 = list(run(0, someone, father(parent, someone)) +
run(0, someone, mother(parent, someone)))
for i in l1:
if i in woman:
l1.remove(i)
return l1
def Daughter(parent):
someone = var()
l1 = list(run(0, someone, father(parent, someone)) +
run(0, someone, mother(parent, someone)))
l2 = []
for i in l1:
if i in woman:
l2.append(i)
def spouse(one):
another = var()
son = Son(one)[0]
l1 = list(run(0, another, father(another, son)) + run(0, another, mother(another, son)))
l1.remove(one)
return l1[0]
def Brother(me):
my_parent = var()
l1 = list(run(0, my_parent, father(my_parent, me)))
l2 = Son(l1[0])
l2 = list(set(l2))
if me in l2:
l2.remove(me)
return l2
def Sister(me):
my_parent = var()
l1 = list(run(0, my_parent, father(my_parent, me)))
l2 = Daughter(l1[0])
l2 = list(set(l2))
if me in l2:
l2.remove(me)
return l2
def BrotherInLaw(me):
someone = var()
l1 = Sister(me) + Sister(spouse(me))
l2, l3 = [], []
for i in l1:
l2 = l2 + Son(i) + Daughter(i)
for i in l2:
l3 = l3 + list(run(0, someone, father(someone, i)))
l3 = list(set(l3))
return l3
def SisterInLaw(me):
someone = var()
l1 = Brother(me) + Brother(spouse(me))
l2, l3 = [], []
for i in l1:
l2 = l2 + Son(i) + Daughter(i)
for i in l2:
l3 = l3 + list(run(0, someone, mother(someone, i)))
l3 = list(set(l3))
return l3
def FirstCousin(me):
my_parent = var()
l1 = Brother(list(run(0, my_parent, father(my_parent, me)))[0]) + \
Sister(list(run(0, my_parent, father(my_parent, me)))[0])
l2 = Brother(list(run(0, my_parent, mother(my_parent, me)))[0]) + \
Sister(list(run(0, my_parent, mother(my_parent, me)))[0])
l3 = []
for i in l1:
l3 = l3 + Son(i) + Daughter(i)
for j in l2:
l3 = l3 + Son(j) + Daughter(j)
l3 = list(set(l3))
return l3
def Aunt(me):
parent = var()
l1 = FirstCousin(me)
l2 = []
for i in l1:
l2 = l2 + list(run(0, parent, mother(parent, i)))
l2 = list(set(l2))
return l2
def Uncle(me):
parent = var()
l1 = FirstCousin(me)
l2 = []
for i in l1:
l2 = l2 + list(run(0, parent, father(parent, i)))
l2 = list(set(l2))
return l2
def GreatGrandparent(grandson):
someone = var()
someone_son = var()
someone_son_son = var()
l1 = list(run(0, someone, father(someone, someone_son), father(someone_son, someone_son_son),
father(someone_son_son, grandson)) +
run(0, someone, father(someone, someone_son), father(someone_son, someone_son_son),
mother(someone_son_son, grandson)) +
run(0, someone, father(someone, someone_son), mother(someone_son, someone_son_son),
father(someone_son_son, grandson)) +
run(0, someone, father(someone, someone_son), mother(someone_son, someone_son_son),
mother(someone_son_son, grandson)) +
run(0, someone, mother(someone, someone_son), mother(someone_son, someone_son_son),
mother(someone_son_son, grandson)) +
run(0, someone, mother(someone, someone_son), mother(someone_son, someone_son_son),
father(someone_son_son, grandson)) +
run(0, someone, mother(someone, someone_son), father(someone_son, someone_son_son),
mother(someone_son_son, grandson)) +
run(0, someone, mother(someone, someone_son), father(someone_son, someone_son_son),
father(someone_son_son, grandson)))
return l1
def Ancestor(me):
parent = var()
l1 = GreatGrandparent(me)
l2 = []
l2 = l2 + l1
while l1:
for i in l1:
l3 = list(run(0, parent, father(parent, i))) + list(run(0, parent, mother(parent, i)))
if l3:
l2 = l2 + l3
l1.remove(i)
return l2
print("Elizabeth 的孙辈是:"+str(GrandChild("Elizabeth")))
print("Diana 的姐夫/妹夫是:"+str(BrotherInLaw("Diana")))
print("Zara 的曾祖父母是:"+str(GreatGrandparent("Zara")))
print("Eugenie 的祖先是:"+str(Ancestor("Eugenie")))