Решение на Родословно дърво от Петър Добрев

Обратно към всички решения

Към профила на Петър Добрев

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 11 успешни тест(а)
  • 0 неуспешни тест(а)

Код

class Person:
def __init__(self, name, gender, birth_year, father=None, mother=None):
self.name = name
self.gender = gender
self.birth_year = birth_year
self.father = father
self.mother = mother
self.kids = []
if father is not None:
father.add_child(self)
if mother is not None:
mother.add_child(self)
def children(self, gender=None):
if gender is None:
return self.kids
else:
return list(filter(lambda x: x.gender is gender, self.kids))
def add_child(self, person):
self.kids.append(person)
def get_sisters(self):
sisters = []
if self.father is not None:
for x in self.father.kids:
if x.gender == "F" and x is not self:
sisters.append(x)
if self.mother is not None:
for x in self.mother.kids:
if x.gender == "F" and x not in sisters and x is not self:
sisters.append(x)
return sisters
def get_brothers(self):
brothers = []
if self.father is not None:
for x in self.father.kids:
if x.gender == "M" and x is not self:
brothers.append(x)
if self.mother is not None:
for x in self.mother.kids:
if x.gender == "M" and x not in brothers and x is not self:
brothers.append(x)
return brothers
def is_direct_successor(self, person):
return person in self.kids

Лог от изпълнението

...........
----------------------------------------------------------------------
Ran 11 tests in 0.006s

OK

История (1 версия и 0 коментара)

Петър обнови решението на 01.04.2013 11:54 (преди около 11 години)

+class Person:
+ def __init__(self, name, gender, birth_year, father=None, mother=None):
+ self.name = name
+ self.gender = gender
+ self.birth_year = birth_year
+ self.father = father
+ self.mother = mother
+ self.kids = []
+ if father is not None:
+ father.add_child(self)
+ if mother is not None:
+ mother.add_child(self)
+
+ def children(self, gender=None):
+ if gender is None:
+ return self.kids
+ else:
+ return list(filter(lambda x: x.gender is gender, self.kids))
+
+ def add_child(self, person):
+ self.kids.append(person)
+
+ def get_sisters(self):
+ sisters = []
+ if self.father is not None:
+ for x in self.father.kids:
+ if x.gender == "F" and x is not self:
+ sisters.append(x)
+ if self.mother is not None:
+ for x in self.mother.kids:
+ if x.gender == "F" and x not in sisters and x is not self:
+ sisters.append(x)
+ return sisters
+
+ def get_brothers(self):
+ brothers = []
+ if self.father is not None:
+ for x in self.father.kids:
+ if x.gender == "M" and x is not self:
+ brothers.append(x)
+ if self.mother is not None:
+ for x in self.mother.kids:
+ if x.gender == "M" and x not in brothers and x is not self:
+ brothers.append(x)
+ return brothers
+
+ def is_direct_successor(self, person):
+ return person in self.kids