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

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

Към профила на Ралица Маркова

Резултати

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

Код

class Person:
def __init__(self, name, gender, birth_year, father=None, mother=None):
self.name = name
self.gender = gender
self.birth_year = birth_year
self.father = father
self.mother = mother
self.my_children = []
if father is not None:
father.my_children.append(self)
if mother is not None:
mother.my_children.append(self)
def get_parents_children(self):
all_children = []
if self.mother is not None:
for child in self.mother.children():
if child not in all_children:
all_children.append(child)
if self.father is not None:
for child in self.father.children():
if child not in all_children:
all_children.append(child)
return all_children
def get_brothers(self):
brothers = []
for child in self.get_parents_children():
if child is not self and child.gender == 'M':
brothers.append(child)
return brothers
def get_sisters(self):
sisters = []
for child in self.get_parents_children():
if child is not self and child.gender == 'F':
sisters.append(child)
return sisters
def children(self, gender='All'):
if gender == 'M' or gender == 'F':
same_gender_children = []
for child in self.my_children:
if child.gender == gender:
same_gender_children.append(child)
return same_gender_children
return self.my_children
def is_direct_successor(self, successor):
if self is successor.mother or self is successor.father:
return True
elif (successor.mother is not None and
self.is_direct_successor(successor.mother)):
return True
elif (successor.father is not None and
self.is_direct_successor(successor.father)):
return True
return False

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

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

OK

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

Ралица обнови решението на 31.03.2013 15:11 (преди над 11 години)

+class Person:
+ def __init__(self, name, gender, birth_year, father=None, mother=None):
+ self.name = name
+ self.gender = gender
+ self.birth_year = birth_year
+ self.father = father
+ self.mother = mother
+ self.my_children = []
+ if father is not None:
+ father.my_children.append(self)
+ if mother is not None:
+ mother.my_children.append(self)
+
+ def get_parents_children(self):
+ all_children = []
+ if self.mother is not None:
+ for child in self.mother.children():
+ if child not in all_children:
+ all_children.append(child)
+
+ if self.father is not None:
+ for child in self.father.children():
+ if child not in all_children:
+ all_children.append(child)
+
+ return all_children
+
+ def get_brothers(self):
+ brothers = []
+ for child in self.get_parents_children():
+ if child is not self and child.gender == 'M':
+ brothers.append(child)
+
+ return brothers
+
+ def get_sisters(self):
+ sisters = []
+ for child in self.get_parents_children():
+ if child is not self and child.gender == 'F':
+ sisters.append(child)
+
+ return sisters
+
+ def children(self, gender='All'):
+ if gender == 'M' or gender == 'F':
+ same_gender_children = []
+ for child in self.my_children:
+ if child.gender == gender:
+ same_gender_children.append(child)
+ return same_gender_children
+ return self.my_children
+
+ def is_direct_successor(self, successor):
+ if self is successor.mother or self is successor.father:
+ return True
+ elif (successor.mother is not None and
+ self.is_direct_successor(successor.mother)):
+ return True
+ elif (successor.father is not None and
+ self.is_direct_successor(successor.father)):
+ return True
+
+ return False