Решение на Родословно дърво от Александър Димитров

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

Към профила на Александър Димитров

Резултати

  • 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.children_list = []
if father:
father.children_list.append(self)
if mother:
mother.children_list.append(self)
def get_siblings(self, gender):
siblings_list = []
if self.mother:
siblings_list += list(filter(lambda x: x.gender == gender,
self.mother.children_list))
if self.father:
siblings_list += list(filter(lambda x: x.gender == gender,
self.father.children_list))
siblings_list = list(set(siblings_list))
if self in siblings_list:
siblings_list.remove(self)
return siblings_list
def get_brothers(self):
return self.get_siblings('M')
def get_sisters(self):
return self.get_siblings('F')
def children(self, gender=None):
if gender:
return list(filter(lambda x: x.gender == gender,
self.children_list))
else:
return self.children_list
def is_direct_successor(self, other):
return other in self.children_list

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

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

OK

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

Александър обнови решението на 28.03.2013 00:43 (преди над 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.children_list = []
+
+ if father:
+ father.children_list.append(self)
+ if mother:
+ mother.children_list.append(self)
+
+ def get_siblings(self, gender):
+ siblings_list = []
+
+ if self.mother:
+ siblings_list += list(filter(lambda x: x.gender == gender,
+ self.mother.children_list))
+ if self.father:
+ siblings_list += list(filter(lambda x: x.gender == gender,
+ self.father.children_list))
+
+ siblings_list = list(set(siblings_list))
+
+ if self in siblings_list:
+ siblings_list.remove(self)
+
+ return siblings_list
+
+ def get_brothers(self):
+ return self.get_siblings('M')
+
+ def get_sisters(self):
+ return self.get_siblings('F')
+
+ def children(self, gender=None):
+ if gender:
+ return list(filter(lambda x: x.gender == gender,
+ self.children_list))
+ else:
+ return self.children_list
+
+ def is_direct_successor(self, other):
+ return self in other.children_list or other in self.children_list

Александър обнови решението на 28.03.2013 16:04 (преди над 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.children_list = []
if father:
father.children_list.append(self)
if mother:
mother.children_list.append(self)
def get_siblings(self, gender):
siblings_list = []
if self.mother:
siblings_list += list(filter(lambda x: x.gender == gender,
self.mother.children_list))
if self.father:
siblings_list += list(filter(lambda x: x.gender == gender,
self.father.children_list))
siblings_list = list(set(siblings_list))
if self in siblings_list:
siblings_list.remove(self)
return siblings_list
def get_brothers(self):
return self.get_siblings('M')
def get_sisters(self):
return self.get_siblings('F')
def children(self, gender=None):
if gender:
return list(filter(lambda x: x.gender == gender,
self.children_list))
else:
return self.children_list
def is_direct_successor(self, other):
- return self in other.children_list or other in self.children_list
+ return other in self.children_list