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

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

Към профила на Михаил Петков

Резултати

  • 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.childs = []
if father is not None:
self.father = father
father.childs.append(self)
if mother is not None:
self.mother = mother
mother.childs.append(self)
def get_children_per_gender(self, gender):
all_children = list(set(self.father.childs) | set(self.mother.childs))
one_gender_children = []
for child in all_children:
if child.gender == gender and self is not child:
one_gender_children.append(child)
return one_gender_children
def get_brothers(self):
return self.get_children_per_gender("M")
def get_sisters(self):
return self.get_children_per_gender("F")
def children(self, gender="All"):
if(gender == "All"):
return self.childs
else:
return [child for child in self.childs if child.gender == gender]
def is_direct_successor(self, person):
return True if person in self.childs else False

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

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

OK

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

Михаил обнови решението на 28.03.2013 23:11 (преди над 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.childs = []
+ if father is not None:
+ self.father = father
+ father.childs.append(self)
+ if mother is not None:
+ self.mother = mother
+ mother.childs.append(self)
+
+ def get_brothers(self):
+ all_children = list(set(self.father.childs) | set(self.mother.childs))
+ brothers = []
+ for child in all_children:
+ if child.gender == "M" and self is not child:
+ brothers.append(child)
+ return brothers
+
+ def get_sisters(self):
+ all_children = list(set(self.father.childs) | set(self.mother.childs))
+ sisters = []
+ for child in all_children:
+ if child.gender == "F" and self is not child:
+ sisters.append(child)
+ return sisters
+
+ def children(self, gender="All"):
+ if(gender == "All"):
+ return self.childs
+ else:
+ return [child for child in self.childs if child.gender == gender]
+
+ def is_direct_successor(self, person):
+ if person in self.childs:
+ return True
+ else:
+ return False

Михаил обнови решението на 29.03.2013 13:55 (преди над 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.childs = []
if father is not None:
self.father = father
father.childs.append(self)
if mother is not None:
self.mother = mother
mother.childs.append(self)
- def get_brothers(self):
+ def get_children_per_gender(self, gender):
all_children = list(set(self.father.childs) | set(self.mother.childs))
- brothers = []
+ one_gender_children = []
for child in all_children:
- if child.gender == "M" and self is not child:
- brothers.append(child)
- return brothers
+ if child.gender == gender and self is not child:
+ one_gender_children.append(child)
+ return one_gender_children
+ def get_brothers(self):
+ return self.get_children_per_gender("M")
+
def get_sisters(self):
- all_children = list(set(self.father.childs) | set(self.mother.childs))
- sisters = []
- for child in all_children:
- if child.gender == "F" and self is not child:
- sisters.append(child)
- return sisters
+ return self.get_children_per_gender("F")
def children(self, gender="All"):
if(gender == "All"):
return self.childs
else:
return [child for child in self.childs if child.gender == gender]
def is_direct_successor(self, person):
- if person in self.childs:
- return True
+ return True if person in self.childs else False
- else:
- return False