Решение на Родословно дърво от Радослав Платиканов

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

Към профила на Радослав Платиканов

Резултати

  • 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 = set()
if self.father is not None:
self.father.children_set.add(self)
if self.mother is not None:
self.mother.children_set.add(self)
def children(self, gender=None):
children = self.children_set
if gender is not None:
return [child for child in children if child.gender == gender]
else:
return list(children)
def get_siblings(self, gender=None):
siblings = set()
if self.father is not None:
siblings.update(self.father.children(gender))
if self.mother is not None:
siblings.update(self.mother.children(gender))
if self in siblings:
siblings.remove(self)
return list(siblings)
def get_brothers(self):
return self.get_siblings(gender='M')
def get_sisters(self):
return self.get_siblings(gender='F')
def is_direct_successor(self, person):
return person.father is self or person.mother is self

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

...........
----------------------------------------------------------------------
Ran 11 tests in 0.008s

OK

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

Радослав обнови решението на 29.03.2013 16:57 (преди над 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 = set()
+
+ if self.father is not None:
+ self.father.children_set.add(self)
+
+ if self.mother is not None:
+ self.mother.children_set.add(self)
+
+ def children(self, gender=None):
+ children = self.children_set
+ if gender is not None:
+ return [child for child in children if child.gender == gender]
+ else:
+ return list(children)
+
+ def get_siblings(self, gender=None):
+ siblings = set()
+ if self.father is not None:
+ siblings.update(self.father.children(gender))
+
+ if self.mother is not None:
+ siblings.update(self.mother.children(gender))
+
+ if self in siblings:
+ siblings.remove(self)
+
+ return list(siblings)
+
+ def get_brothers(self):
+ return self.get_siblings(gender='M')
+
+ def get_sisters(self):
+ return self.get_siblings(gender='F')
+
+ def is_direct_successor(self, person):
+ return person.father is self or person.mother is self