파이썬에서 재귀 함수를 사용하여 주어진 부모로부터 모든 부모를 얻으려고합니다. 부모로부터 B와 C까지, B가 부모 D와 E를, C가 부모 F와 G를 가지고 있기 때문에 함수가 {B, C, D, E, F, G} 나는 모든 사람들이 self.people (인스턴스가 아닌 이름)에 저장되어있는 GenTree 클래스와 get_parents() 메소드를 사용하여 False 또는 부모 튜플을 반환하는 클래스 Person (클래스의 2 인스턴스 사람) 여기 파이썬에서 for 루프를 사용하는 재귀 함수
는 클래스와 메소드로 전체 파일입니다Ales;m;c
Alexandr;m;c
Anna;f;h
Dana;f;e
Daniela;f;u
David;m;u
Hana;f;h
Jana;f;u
Jarda;m;c
Jindra;m;u
Jirka;m;u
Jitka;f;h
Juraj;m;u
Karel;m;e
Katka;f;c
Lenka;f;h
Leon;m;h
Leona;f;c
Leos;m;e
Lida;f;e
Ludmila;f;h
Magdalena;f;c
Matej;m;u
Michaela;f;h
Michal;m;e
Patricia;f;h
Petr;m;h
Richard;m;e
Sasa;f;u
Stefan;m;h
Tereza;f;h
Tomas;m;e
Vaclav;m;e
Vojtech;m;c
Zdena;f;e
Zdenek;m;h
Ales => Zdenek
Tereza => Zdenek
Alexandr => Vojtech
Zdena => Vojtech
David => Anna
Sasa => Anna
Jarda => Daniela
Patricia => Daniela
Vojtech => Jindra
Daniela => Jindra
Zdenek => Jirka
Anna => Jirka
Vaclav => Juraj
Michaela => Juraj
Ales => Ludmila
Tereza => Ludmila
Ludmila => Magdalena
Juraj => Magdalena
Juraj => Tomas
Anna => Tomas
Zdenek => Lida
Sasa => Lida
Lida => Leona
Zdenek => Leona
Tomas => Stefan
Anna => Stefan
Tomas => Karel
Leona => Karel
Tomas => Leos
Leona => Leos
Tomas => Lenka
Leona => Lenka
Juraj => Matej
Lenka => Matej
Juraj => Leon
Anna => Leon
Juraj => Richard
Lenka => Richard
Richard => Petr
Ludmila => Petr
Petr => Michal
Dana => Michal
Stefan => Dana
Anna => Dana
Michal => Hana
Lida => Hana
Michal => Jana
Lida => Jana
Petr => Jitka
Dana => Jitka
Jirka => Katka
Dana => Katka
: 여기
class Person:
def __init__(self, name, gender, education, father = False, mother = False):
self.name = name
self.gender = gender
self.education = education
self.father = father
self.mother = mother
self.children = []
def add_parent(self, inst):
if inst.gender=="m":
self.father = inst
else:
self.mother = inst
def add_child(self, inst):
self.children.append(inst)
def has_parent(self):
return True if self.father or self.mother else False
def get_parents(self):
if self.has_parent():
if self.father and self.mother: return self.father, self.mother
if self.father and not self.mother: return self.father
if self.mother and not self.father: return self.mother
else:
return()
class GenTree:
def __init__(self):
self.people = {}
def load_from_file(self, file_name):
data = open(file_name, "r")
people = {}
reading = "person"
for line in data:
line = line.rstrip()
reading = "fam" if line=="" else reading
if reading=="person":
thisInfo = line.split(";")
thisName = thisInfo[0]
thisGender = thisInfo[1]
thisEd = thisInfo[2]
self.people[thisName] = Person(thisName, thisGender, thisEd)
else:
if line == "":
continue
thisInfo = line.split("=>")
for i in range(len(thisInfo)):
thisInfo[i] = thisInfo[i].rstrip()
thisInfo[i] = thisInfo[i].strip(" ")
self.people[thisInfo[1]].add_parent(self.people[thisInfo[0]])
self.people[thisInfo[0]].add_child(self.people[thisInfo[1]])
def get_all_parents(self, child_name):
child = self.people[child_name]
parents = child.get_parents()
if parents:
for parent in parents:
return parents + self.get_all_parents(parent.name)
return parents
g = GenTree()
g.load_from_file("data_a")
print([i.name for i in g.get_all_parents('Katka')])
및 주어진 데이터 파일입니다 "
{'Jirka', 'Dana', 'Zdenek', 'Anna', 'Stefan', 'Ales', 'Tereza', 'David', 'Sasa', 'Tomas', 'Juraj', 'Vaclav', 'Michaela'}
을하지만
['Jirka', 'Dana', 'Zdenek', 'Anna', 'Ales', 'Tereza']
를 반환하지만 항상 수행합니다
자,"인쇄 ([g.get_all_parents의 난에 대한 i.name ('Katka은')]) "반환해야 self.get_all_parents (parent.name) "for 루프의 첫 번째 노드에만 해당 또한 get_all_parents (self, child_name, allparents = set())을 시도한 집합으로 반환하는 방법을 모르겠습니다. 하지만 모든 기능 호출로 설정된 전신을 재설정하지 않는 것 같습니다)
예제가 실행 가능하지 않으므로 검증 할 수 없습니다. 기능을 실행하는 데 필요한만큼의 코드를 게시하십시오 (그 이상은 아닙니다). – timgeb
나무 모양이나'GenTree' 코드를 게시하십시오. – Ajax1234
나는 그것을 매우 간단하게 만들 수는 없었지만 지금은 쓸모없는 것을 없애려고 노력했다. 내가 코드 –