Решение на Родословно дърво от Момчил Джамбазов

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

Към профила на Момчил Джамбазов

Резултати

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

Код

class Person:
MIN_PARENT_AGE = 18
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
for parent in ('father', 'mother'):
if getattr(self, parent, False):
getattr(self, parent).add_child(self)
self._children = []
def get_siblings(self, gender):
siblings = set()
for parent in ('father', 'mother'):
if getattr(self, parent, False):
for parent_child in getattr(self, parent).children(gender):
if parent_child is not self:
siblings.add(parent_child)
return list(siblings)
def get_brothers(self):
return self.get_siblings(gender='M')
def get_sisters(self):
return self.get_siblings(gender='F')
def add_parent(self, parent):
parent.add_child(self)
def add_child(self, child):
parent_age = child.birth_year - self.birth_year
if child not in self.children() and parent_age > self.MIN_PARENT_AGE:
self._children.append(child)
setattr(child, 'father' if self.gender == 'M' else 'mother', self)
def children(self, gender=None):
return list(filter(lambda child: gender is None or
child.gender == gender, self._children))
def is_direct_successor(self, person):
return person in self.children()

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

...........
----------------------------------------------------------------------
Ran 11 tests in 0.022s

OK

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

Момчил обнови решението на 01.04.2013 12:31 (преди над 11 години)

+class Person:
+ MIN_PARENT_AGE = 18
+
+ 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
+
+ for parent in ('father', 'mother'):
+ if getattr(self, parent, False):
+ getattr(self, parent).add_child(self)
+
+ self._children = []
+
+ def get_siblings(self, gender):
+ siblings = set()
+ for parent in ('father', 'mother'):
+ if getattr(self, parent, False):
+ for parent_child in getattr(self, parent).children(gender):
+ if parent_child is not self:
+ siblings.add(parent_child)
+ return list(siblings)
+
+ def get_brothers(self):
+ return self.get_siblings(gender='M')
+
+ def get_sisters(self):
+ return self.get_siblings(gender='F')
+
+ def add_parent(self, parent):
+ parent.add_child(self)
+
+ def add_child(self, child):
+ parent_age = child.birth_year - self.birth_year
+ if child not in self.children() and parent_age > self.MIN_PARENT_AGE:
+ self._children.append(child)
+ setattr(child, 'father' if self.gender == 'M' else 'mother', self)
+
+ def children(self, gender=None):
+ return list(filter(lambda child: gender is None or
+ child.gender == gender, self._children))
+
+ def is_direct_successor(self, person):
+ return person in self.children()