Йордан обнови решението на 26.03.2013 13:04 (преди над 11 години)
+MIN_PARENT_AGE = 18
+
+
+class Person:
+ def __init__(self, name, birth_year, gender):
+ self.name = name
+ self.birth_year = birth_year
+ self.gender = gender
+ self.father = None
+ self.mother = None
+ self._children = []
+
+ def get_brothers(self):
+ brothers = []
+ if self.father is not None:
+ brothers += self.father.children('male')
+ if self.mother is not None:
+ brothers += self.mother.children('male')
+ return list(set(brothers))
+
+ def get_sisters(self):
+ sisters = []
+ if self.father is not None:
+ sisters += self.father.children('female')
+ if self.mother is not None:
+ sisters += self.mother.children('female')
+ return list(set(sisters))
+
+ def add_parent(self, parent):
+ if self not in parent.children():
+ return parent.add_child(self)
+
+ def add_child(self, child):
+ if isinstance(child, Person) and child not in self.children():
+ if (child.birth_year - self.birth_year) < MIN_PARENT_AGE:
+ return
+
+ self._children.append(child)
+ if self.gender == 'male':
+ child.father = self
+ if self.gender == 'female':
+ child.mother = self
+
+ def children(self, gender=None):
+ return list(filter(lambda child: child.gender == gender or
+ gender is None, self._children))
+
+ def is_direct_successor(self, person):
+ return person in self.children() or self in person.children()