Решение на Родословно дърво от Николай Хубанов

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

Към профила на Николай Хубанов

Резултати

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

Код

class Person:
parent_child_pairs = []
def __init__(self, name='no-name', birth_year=0, gender='MF',
father=None, mother=None):
self.name = name
self.gender = gender
self.birth_year = birth_year
self.father = father
self.mother = mother
if mother is not None:
self.add_parent(mother)
if father is not None:
self.add_parent(father)
def add_child(self, child):
Person.parent_child_pairs.append((self, child))
def add_parent(self, parent):
Person.parent_child_pairs.append((parent, self))
if parent.gender == 'M':
self.father = parent
else:
self.mother = parent
def __get_parents(self):
return {parent for parent, child in Person.parent_child_pairs
if child is self}
def __get_siblings(self, gender):
parents = self.__get_parents()
return {child for parent, child in Person.parent_child_pairs
if parent in parents and child.gender == gender and
not(child is self)}
def get_brothers(self):
return list(self.__get_siblings('M'))
def get_sisters(self):
return list(self.__get_siblings('F'))
def __get_children_by_gender(self, gender):
return {child for parent, child in Person.parent_child_pairs
if (parent is self) and (child.gender == gender)}
def children(self, gender='MF'):
result = set()
for gender_ in gender:
result |= (self.__get_children_by_gender(gender_))
return list(result)
def is_direct_successor(self, child):
return child in self.children()
def __str__(self):
return ("name = {0}, gender = {1}, birth_year = {2}, father = {3}, "
"mother = {4}").format(self.name, self.gender,
self.birth_year, self.father,
self.mother)

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

...........
----------------------------------------------------------------------
Ran 11 tests in 0.007s

OK

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

Николай обнови решението на 01.04.2013 02:14 (преди над 11 години)

+class Person:
+
+ parent_child_pairs = []
+
+ def __init__(self, name='no-name', birth_year=0, gender='MF',
+ father=None, mother=None):
+ self.name = name
+ self.gender = gender
+ self.birth_year = birth_year
+ self.father = father
+ self.mother = mother
+
+ if mother is not None:
+ self.add_parent(mother)
+ if father is not None:
+ self.add_parent(father)
+
+ def add_child(self, child):
+ Person.parent_child_pairs.append((self, child))
+
+ def add_parent(self, parent):
+ Person.parent_child_pairs.append((parent, self))
+
+ if parent.gender == 'M':
+ self.father = parent
+ else:
+ self.mother = parent
+
+ def __get_parents(self):
+ return {parent for parent, child in Person.parent_child_pairs
+ if child is self}
+
+ def __get_siblings(self, gender):
+ parents = self.__get_parents()
+ return {child for parent, child in Person.parent_child_pairs
+ if parent in parents and child.gender == gender and
+ not(child is self)}
+
+ def get_brothers(self):
+ return list(self.__get_siblings('M'))
+
+ def get_sisters(self):
+ return list(self.__get_siblings('F'))
+
+ def __get_children_by_gender(self, gender):
+ return {child for parent, child in Person.parent_child_pairs
+ if (parent is self) and (child.gender == gender)}
+
+ def children(self, gender='MF'):
+ result = set()
+ for gender_ in gender:
+ result |= (self.__get_children_by_gender(gender_))
+
+ return list(result)
+
+ def is_direct_successor(self, child):
+ return child in self.children()
+
+ def __str__(self):
+ return ("name = {0}, gender = {1}, birth_year = {2}, father = {3}, "
+ "mother = {4}").format(self.name, self.gender,
+ self.birth_year, self.father,
+ self.mother)