Николай обнови решението на 31.03.2013 23:07 (преди над 11 години)
+from queue import Queue
+
+
+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._children = []
+ if father is not None:
+ father._children.append(self)
+ if mother is not None:
+ mother._children.append(self)
+
+ def children(self, gender=''):
+ result = []
+ for child in self._children:
+ if child.gender == gender:
+ result.append(child)
+ return result
+
+ def _get_siblings(self, gender):
+ result = set()
+ if self.father is not None:
+ result |= set(self.father.children(gender))
+ if self.mother is not None:
+ result |= set(self.mother.children(gender))
+ return list(result - {self})
+
+ def get_brothers(self):
+ return self._get_siblings('M')
+
+ def get_sisters(self):
+ return self._get_siblings('F')
+
+ def is_direct_successor(self, other):
+ visited = set()
+ q = Queue(0)
+ q.put_nowait(other)
+ while q.qsize() > 0:
+ current = q.get_nowait()
+ if current in visited:
+ continue
+ visited.add(current)
+ if current is self:
+ return True
+ if current.father is not None:
+ q.put_nowait(current.father)
+ if current.mother is not None:
+ q.put_nowait(current.mother)
+ return False