Решение на Родословно дърво от Мария Терзиева

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

Към профила на Мария Терзиева

Резултати

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

Код

class Person:
instances = list()
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.instances.append(self)
def get_siblings(self, gender):
def we_have_common_parent(my, your):
return (my.father is your.father and my.father is not None or
my.mother is your.mother and my.mother is not None)
siblings_list = list()
for instance in self.instances:
if (we_have_common_parent(self, instance) and
instance.gender == gender and instance is not self):
siblings_list.append(instance)
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):
def gender_filter(my_list, gender):
return filter(lambda instance: instance.gender == gender,
my_list)
children_list = list()
for instance in self.instances:
if instance.father is self or instance.mother is self:
children_list.append(instance)
if gender is not None:
return list(gender_filter(children_list, gender))
else:
return children_list
def is_direct_successor(self, person):
return (person.mother is self or person.father is self)

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

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

OK

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

Мария обнови решението на 31.03.2013 11:08 (преди около 11 години)

+class Person:
+ instances = list()
+
+ 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.instances.append(self)
+
+ def get_siblings(self, gender):
+ def we_have_common_parent(my, your):
+ return (my.father is your.father and my.father is not None or
+ my.mother is your.mother and my.mother is not None)
+
+ siblings_list = list()
+
+ for instance in self.instances:
+ if (we_have_common_parent(self, instance) and
+ instance.gender == gender and instance is not self):
+ siblings_list.append(instance)
+
+ 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):
+ def gender_filter(my_list, gender):
+ return filter(lambda instance: instance.gender == gender,
+ my_list)
+
+ children_list = list()
+
+ for instance in self.instances:
+ if instance.father is self or instance.mother is self:
+ children_list.append(instance)
+
+ if gender is not None:
+ return list(gender_filter(children_list, gender))
+ else:
+ return children_list
+
+ def is_direct_successor(self, person):
+ return (person.mother is self or person.father is self)