Решение на Родословно дърво от Вероника Стоилова

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

Към профила на Вероника Стоилова

Резултати

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

Код

class Person:
def __init__(
self, name, birth_year,
gender, mother=None, father=None,
):
self.name = name
self.birth_year = birth_year
self.gender = gender
self.mother = mother
self.father = father
self.kids = []
if mother is not None:
self.mother.kids.append(self)
if father is not None:
self.father.kids.append(self)
def get_brothers(self):
all_brothers = []
if self.mother is not None:
all_brothers += self.mother.children('M')
if self.father is not None:
all_brothers += self.father.children('M')
non_doubling_brothers = {x for x in all_brothers if x != self}
return list(non_doubling_brothers)
def get_sisters(self):
all_sisters = []
if self.mother is not None:
all_sisters += self.mother.children('F')
if self.father is not None:
all_sisters += self.father.children('F')
non_doubling_sisters = {x for x in all_sisters if x != self}
return list(non_doubling_sisters)
def children(self, gender="both"):
if gender == 'F':
return [child for child in self.kids if child.gender is 'F']
elif gender == 'M':
return [child for child in self.kids if child.gender is 'M']
else:
return self.kids
def is_direct_successor(self, other_person):
return self in other_person.kids or other_person in self.kids

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

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

OK

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

Вероника обнови решението на 01.04.2013 16:06 (преди почти 11 години)

+class Person:
+
+ def __init__(
+ self, name, birth_year,
+ gender, mother=None, father=None,
+ ):
+ self.name = name
+ self.birth_year = birth_year
+ self.gender = gender
+ self.mother = mother
+ self.father = father
+ self.kids = []
+ if mother is not None:
+ self.mother.kids.append(self)
+ if father is not None:
+ self.father.kids.append(self)
+
+ def get_brothers(self):
+ all_brothers = []
+ if self.mother is not None:
+ all_brothers += self.mother.children('M')
+ if self.father is not None:
+ all_brothers += self.father.children('M')
+ non_doubling_brothers = {x for x in all_brothers if x != self}
+ return list(non_doubling_brothers)
+
+ def get_sisters(self):
+ all_sisters = []
+ if self.mother is not None:
+ all_sisters += self.mother.children('F')
+ if self.father is not None:
+ all_sisters += self.father.children('F')
+ non_doubling_sisters = {x for x in all_sisters if x != self}
+ return list(non_doubling_sisters)
+
+ def children(self, gender="both"):
+ if gender == 'F':
+ return [child for child in self.kids if child.gender is 'F']
+ elif gender == 'M':
+ return [child for child in self.kids if child.gender is 'M']
+ else:
+ return self.kids
+
+ def is_direct_successor(self, other_person):
+ return self in other_person.kids or other_person in self.kids