Милан обнови решението на 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)