Георги обнови решението на 01.04.2013 14:55 (преди над 11 години)
+class Person:
+ def __init__(self, name, gender, birth_year, father=None, mother=None):
+ self.parents = set()
+ self.children_set = set()
+ self.siblings = set()
+ self.remove = set() # to avoid adding self to siblings
+ self.name = name
+ self.birth_year = birth_year
+ self.gender = gender
+ if mother is not None:
+ self._add_parent(mother)
+ mother._add_child(self)
+ if father is not None:
+ self._add_parent(father)
+ father._add_child(self)
+ self.remove.add(self) # init set containing only self
+
+ def _add_child(self, new_person):
+ self.children_set.add(new_person)
+
+ def children(self, gender="MF"):
+ return list(set([person for person in self.children_set
+ if person.gender in gender]))
+
+ def _add_parent(self, parent):
+ parent._add_child(self)
+ self.parents.add(parent)
+
+ def is_direct_successor(self, other_person):
+ if(other_person in self.children_set):
+ return True
+ return False
+
+ def _generate_siblings(self):
+ for parent in self.parents:
+ self.siblings = self.siblings.union(parent.children_set)
+ self.siblings = self.siblings.difference(self.remove)
+
+ def _get_siblings(self, gender):
+ return [person for person in list(self.siblings)
+ if person.gender == gender]
+
+ def get_sisters(self):
+ self._generate_siblings()
+ return self._get_siblings("F")
+
+ def get_brothers(self):
+ self._generate_siblings
+ return self._get_siblings("M")