Решение на Родословно дърво от Георги Жухов

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

Към профила на Георги Жухов

Резултати

  • 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.sons = []
self.daughters = []
self.mother = mother
if self.mother:
mother.add_children(self)
self.father = father
if self.father:
father.add_children(self)
def get_brothers(self):
if self.mother:
temp_bros = self.mother.sons
if self.father:
temp_bros += self.father.sons
bros_only = set(temp_bros)
if self in bros_only:
bros_only.remove(self)
return list(bros_only)
#return [each_bro.name for each_bro in bros_only]
def get_sisters(self):
if self.mother:
temp_sis = self.mother.daughters
if self.father:
temp_sis += self.father.daughters
sis_only = set(temp_sis)
if self in sis_only:
sis_only.remove(self)
return list(sis_only)
#return [each_sis.name for each_sis in sis_only]
def add_children(self, new_child):
if new_child.gender == 'M':
self.sons.append(new_child)
elif new_child.gender == 'F':
self.daughters.append(new_child)
def children(self, gender=''):
temp_children = []
if gender == 'M':
temp_children = self.sons
elif gender == 'F':
temp_children = self.daughters
else:
temp_children = self.sons + self.daughters
return list(temp_children)
#return [each_child.name for each_child in temp_children]
def is_direct_successor(self, successor):
temp_children = successor.sons + successor.daughters
if self in temp_children:
return True
elif successor.mother == self or successor.father == self:
return True
else:
return False

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

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

OK

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

Георги обнови решението на 01.04.2013 16:55 (преди над 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.sons = []
+ self.daughters = []
+ self.mother = mother
+ if self.mother:
+ mother.add_children(self)
+ self.father = father
+ if self.father:
+ father.add_children(self)
+
+ def get_brothers(self):
+ if self.mother:
+ temp_bros = self.mother.sons
+ if self.father:
+ temp_bros += self.father.sons
+ bros_only = set(temp_bros)
+ if self in bros_only:
+ bros_only.remove(self)
+ return list(bros_only)
+ #return [each_bro.name for each_bro in bros_only]
+
+ def get_sisters(self):
+ if self.mother:
+ temp_sis = self.mother.daughters
+ if self.father:
+ temp_sis += self.father.daughters
+ sis_only = set(temp_sis)
+ if self in sis_only:
+ sis_only.remove(self)
+ return list(sis_only)
+ #return [each_sis.name for each_sis in sis_only]
+
+ def add_children(self, new_child):
+ if new_child.gender == 'M':
+ self.sons.append(new_child)
+ elif new_child.gender == 'F':
+ self.daughters.append(new_child)
+
+ def children(self, gender=''):
+ temp_children = []
+ if gender == 'M':
+ temp_children = self.sons
+ elif gender == 'F':
+ temp_children = self.daughters
+ else:
+ temp_children = self.sons + self.daughters
+ return list(temp_children)
+ #return [each_child.name for each_child in temp_children]
+
+ def is_direct_successor(self, successor):
+ temp_children = successor.sons + successor.daughters
+ if self in temp_children:
+ return True
+ elif successor.mother == self or successor.father == self:
+ return True
+ else:
+ return False