Решение на Родословно дърво от Милан Миланов

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

Към профила на Милан Миланов

Резултати

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

Код

class Person:
def __init__(self, name, birth_year, gender, father=None, mother=None):
self.name, self.gender, self.birth_year = name, gender, birth_year
self.father, self.mother = father, mother
self._children = set()
self._update_parents_children(father, mother)
def get_brothers(self):
return Person._filter_by_gender(self._siblings(), 'M')
def get_sisters(self):
return Person._filter_by_gender(self._siblings(), 'F')
def children(self, gender=''):
return Person._filter_by_gender(self._children, gender)
def is_direct_successor(self, person):
return self in person.children() or person in self.children()
def _siblings(self):
siblings = set()
for parent in [self.father, self.mother]:
if parent:
siblings.update(parent.children())
siblings.discard(self)
return siblings
def _update_parents_children(self, *parents):
for parent in parents:
if parent:
parent._children.add(self)
@staticmethod
def _filter_by_gender(people, gender):
if gender:
people = {person for person in people if person.gender == gender}
return list(people)

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

...........
----------------------------------------------------------------------
Ran 11 tests in 0.016s

OK

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

Милан обнови решението на 28.03.2013 23:03 (преди над 11 години)

+class Person:
+
+ def __init__(self, name, birth_year, gender, father=None, mother=None):
+ self.name, self.gender, self.birth_year = name, gender, birth_year
+ self.father, self.mother = father, mother
+
+ self._children = set()
+ self._update_parents_children(father, mother)
+
+ def get_brothers(self):
+ return self._filter_by_gender(self._siblings(), 'M')
+
+ def get_sisters(self):
+ return self._filter_by_gender(self._siblings(), 'F')
+
+ def children(self, gender=''):
+ return self._filter_by_gender(self._children, gender)
+
+ def is_direct_successor(self, person):
+ return self in person.children() or person in self.children()
+
+ def _siblings(self):
+ siblings = set()
+
+ for parent in [self.father, self.mother]:
+ if parent:
+ siblings.update(parent.children())
+
+ siblings.discard(self)
+ return siblings
+
+ def _update_parents_children(self, *parents):
+ for parent in parents:
+ if parent:
+ parent._children.add(self)
+
+ @staticmethod
+ def _filter_by_gender(people, gender):
+ if gender:
+ people = {person for person in people if person.gender == gender}
+
+ return list(people)

Милан обнови решението на 29.03.2013 11:20 (преди над 11 години)

class Person:
def __init__(self, name, birth_year, gender, father=None, mother=None):
self.name, self.gender, self.birth_year = name, gender, birth_year
self.father, self.mother = father, mother
-
self._children = set()
+
self._update_parents_children(father, mother)
def get_brothers(self):
- return self._filter_by_gender(self._siblings(), 'M')
+ return Person._filter_by_gender(self._siblings(), 'M')
def get_sisters(self):
- return self._filter_by_gender(self._siblings(), 'F')
+ return Person._filter_by_gender(self._siblings(), 'F')
def children(self, gender=''):
- return self._filter_by_gender(self._children, gender)
+ return Person._filter_by_gender(self._children, gender)
def is_direct_successor(self, person):
return self in person.children() or person in self.children()
def _siblings(self):
siblings = set()
for parent in [self.father, self.mother]:
if parent:
siblings.update(parent.children())
siblings.discard(self)
return siblings
def _update_parents_children(self, *parents):
for parent in parents:
if parent:
parent._children.add(self)
@staticmethod
def _filter_by_gender(people, gender):
if gender:
people = {person for person in people if person.gender == gender}
return list(people)