Решение на Родословно дърво от Мартин Костов

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

Към профила на Мартин Костов

Резултати

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

Код

class Person:
def __init__(self, name, birth_year,
gender, mother=None, father=None):
self.name = name
self.birth_year = birth_year
self.gender = gender
self.mother = mother
self.father = father
self._allchildren = []
if self.mother:
self.mother._allchildren.append(self)
if self.father:
self.father._allchildren.append(self)
def children(self, gender=''):
if gender:
return [child for child in self._allchildren if child.gender == gender]
else:
return self._allchildren
def _get_brothers_or_sisters(self, gender):
brothers_or_sisters = set()
if self.mother:
for child in self.mother.children(gender):
if child is not self:
brothers_or_sisters.add(child)
if self.father:
for child in self.father.children(gender):
if child is not self:
brothers_or_sisters.add(child)
return list(brothers_or_sisters)
def get_brothers(self):
return self._get_brothers_or_sisters('M')
def get_sisters(self):
return self._get_brothers_or_sisters('F')
def is_direct_successor(self, successor):
return True if successor in self._allchildren else False

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

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

OK

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

Мартин обнови решението на 29.03.2013 10:05 (преди над 11 години)

+class Person:
+
+ def __init__(self, name, birth_year,
+ gender, mother=None, father=None):
+ self.name = name
+ self.birth_year = birth_year
+ self.gender = gender
+ self.mother = mother
+ self.father = father
+ self.__allchildren = []
+ if self.mother:
+ self.mother.__allchildren.append(self)
+ if self.father:
+ self.father.__allchildren.append(self)
+
+ def children(self, gender=''):
+ if gender:
+ return [child for child in self.__allchildren if child.gender == gender]
+ else:
+ return self.__allchildren
+
+ def __get_brothers_or_sisters(self, gender):
+ brothers_or_sisters = set()
+ if self.mother:
+ for child in self.mother.children(gender):
+ if child is not self:
+ brothers_or_sisters.add(child)
+ if self.father:
+ for child in self.father.children(gender):
+ if child is not self:
+ brothers_or_sisters.add(child)
+ return list(brothers_or_sisters)
+
+ def get_brothers(self):
+ return self.__get_brothers_or_sisters('M')
+
+ def get_sisters(self):
+ return self.__get_brothers_or_sisters('F')
+
+ def is_direct_successor(self, successor):
+ return True if successor in self.__allchildren else False

Мартин обнови решението на 29.03.2013 10:08 (преди над 11 години)

class Person:
def __init__(self, name, birth_year,
gender, mother=None, father=None):
self.name = name
self.birth_year = birth_year
self.gender = gender
self.mother = mother
self.father = father
- self.__allchildren = []
+ self._allchildren = []
if self.mother:
- self.mother.__allchildren.append(self)
+ self.mother._allchildren.append(self)
if self.father:
- self.father.__allchildren.append(self)
+ self.father._allchildren.append(self)
def children(self, gender=''):
if gender:
- return [child for child in self.__allchildren if child.gender == gender]
+ return [child for child in self._allchildren if child.gender == gender]
else:
- return self.__allchildren
+ return self._allchildren
- def __get_brothers_or_sisters(self, gender):
+ def _get_brothers_or_sisters(self, gender):
brothers_or_sisters = set()
if self.mother:
for child in self.mother.children(gender):
if child is not self:
brothers_or_sisters.add(child)
if self.father:
for child in self.father.children(gender):
if child is not self:
brothers_or_sisters.add(child)
return list(brothers_or_sisters)
def get_brothers(self):
- return self.__get_brothers_or_sisters('M')
+ return self._get_brothers_or_sisters('M')
def get_sisters(self):
- return self.__get_brothers_or_sisters('F')
+ return self._get_brothers_or_sisters('F')
def is_direct_successor(self, successor):
- return True if successor in self.__allchildren else False
+ return True if successor in self._allchildren else False