Решение на Родословно дърво от Иван Боршуков

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

Към профила на Иван Боршуков

Резултати

  • 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 = []
if father:
father.__children.append(self)
if mother:
mother.__children.append(self)
def __get_gender_filter(self, gender=''):
return lambda person: person.gender.startswith(gender)
def get_brothers(self):
''' Returns list of people, who are brothers to self. '''
brothers = set()
is_male = self.__get_gender_filter('M')
if self.father:
brothers.update(filter(is_male, self.father.__children))
if self.mother:
brothers.update(filter(is_male, self.mother.__children))
brothers.discard(self)
return list(brothers)
def get_sisters(self):
''' Returns list of people. who are sisters to self. '''
sisters = set()
is_female = self.__get_gender_filter('F')
if self.father:
sisters.update(filter(is_female, self.father.__children))
if self.mother:
sisters.update(filter(is_female, self.mother.__children))
sisters.discard(self)
return list(sisters)
def children(self, gender=''):
''' Returns list of children '''
return list(filter(self.__get_gender_filter(gender), self.__children))
def is_direct_successor(self, person):
''' Returns True if person is direct successor to self '''
return person in self.__children

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

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

OK

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

Иван обнови решението на 01.04.2013 10:58 (преди над 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 = []
+ if father:
+ father.__children.append(self)
+ if mother:
+ mother.__children.append(self)
+
+ def __get_gender_filter(self, gender=''):
+ return lambda person: person.gender.startswith(gender)
+
+ def get_brothers(self):
+ ''' Returns list of people, who are brothers to self. '''
+ brothers = set()
+ is_male = self.__get_gender_filter('M')
+ if self.father:
+ brothers.update(filter(is_male, self.father.__children))
+ if self.mother:
+ brothers.update(filter(is_male, self.mother.__children))
+ brothers.discard(self)
+ return list(brothers)
+
+ def get_sisters(self):
+ ''' Returns list of people. who are sisters to self. '''
+ sisters = set()
+ is_female = self.__get_gender_filter('F')
+ if self.father:
+ sisters.update(filter(is_female, self.father.__children))
+ if self.mother:
+ sisters.update(filter(is_female, self.mother.__children))
+ sisters.discard(self)
+ return list(sisters)
+
+ def children(self, gender=''):
+ ''' Returns list of children '''
+ return list(filter(self.__get_gender_filter(gender), self.__children))
+
+ def is_direct_successor(self, person):
+ ''' Returns True if person is direct successor to self '''
+ return person in self.__children