Решение на Родословно дърво от Деян Спиров

Обратно към всички решения

Към профила на Деян Спиров

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 11 успешни тест(а)
  • 0 неуспешни тест(а)

Код

class Person:
def __init__(self, name, birth_year, gender, father=None, mother=None):
self.name = name
self.birth_year = birth_year
self.gender = gender
self.father = father
self.mother = mother
self._children = set()
if father is not None:
father._add_child(self)
if mother is not None:
mother._add_child(self)
def _add_child(self, child):
self._children.add(child)
def get_siblings(self):
siblings = set()
if self.mother is not None:
siblings |= self.mother._children
if self.father is not None:
siblings |= self.father._children
if self in siblings:
siblings.remove(self)
return siblings
def get_brothers(self):
return list(filter(lambda child: child.gender == 'M',
self.get_siblings()))
def get_sisters(self):
return list(filter(lambda child: child.gender == 'F',
self.get_siblings()))
def children(self, gender=None):
if gender is None:
return list(self._children)
else:
return list(filter(lambda child: child.gender == gender,
self._children))
def is_direct_successor(self, child):
return child in self._children

Лог от изпълнението

...........
----------------------------------------------------------------------
Ran 11 tests in 0.006s

OK

История (1 версия и 0 коментара)

Деян обнови решението на 31.03.2013 20:05 (преди около 11 години)

+class Person:
+ def __init__(self, name, birth_year, gender, father=None, mother=None):
+ self.name = name
+ self.birth_year = birth_year
+ self.gender = gender
+ self.father = father
+ self.mother = mother
+ self._children = set()
+
+ if father is not None:
+ father._add_child(self)
+ if mother is not None:
+ mother._add_child(self)
+
+ def _add_child(self, child):
+ self._children.add(child)
+
+ def get_siblings(self):
+ siblings = set()
+ if self.mother is not None:
+ siblings |= self.mother._children
+ if self.father is not None:
+ siblings |= self.father._children
+ if self in siblings:
+ siblings.remove(self)
+ return siblings
+
+ def get_brothers(self):
+ return list(filter(lambda child: child.gender == 'M',
+ self.get_siblings()))
+
+ def get_sisters(self):
+ return list(filter(lambda child: child.gender == 'F',
+ self.get_siblings()))
+
+ def children(self, gender=None):
+ if gender is None:
+ return list(self._children)
+ else:
+ return list(filter(lambda child: child.gender == gender,
+ self._children))
+
+ def is_direct_successor(self, child):
+ return child in self._children