Георги обнови решението на 30.03.2013 11:04 (преди над 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.successors = {'M': set(), 'F': set()}
+ self.siblings = {'M': set(), 'F': set()}
+
+ if self.mother:
+ self.siblings['M'].update(self.mother.successors['M'])
+ self.siblings['F'].update(self.mother.successors['F'])
+
+ for sibling in self.mother.successors['M'].union(self.mother.successors['F']):
+ sibling.siblings[self.gender].add(self)
+
+ self.mother.successors[self.gender].add(self)
+
+ if self.father:
+ self.siblings['M'].update(self.father.successors['M'])
+ self.siblings['F'].update(self.father.successors['F'])
+
+ for sibling in self.father.successors['M'].union(self.father.successors['F']):
+ sibling.siblings[self.gender].add(self)
+
+ self.father.successors[self.gender].add(self)
+
+ def get_brothers(self):
+ return list(self.siblings['M'])
+
+ def get_sisters(self):
+ return list(self.siblings['F'])
+
+ def children(self, gender='MF'):
+ if gender is 'MF':
+ return list(self.successors['M'].union(self.successors['F']))
+ else:
+ return list(self.successors[gender])
+
+ def is_direct_successor(self, target):
+ return target in self.successors['M'].union(self.successors['F'])