Решение на Родословно дърво от Виктория Христова

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

Към профила на Виктория Христова

Резултати

  • 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.mother = mother
self.father = father
self.kids = []
if mother is not None:
mother.kids.append(self)
if father is not None:
father.kids.append(self)
def female(self):
return self.gender == 'F'
def male(self):
return self.gender == 'M'
def children(self, gender=None):
if gender:
return [kid for kid in self.kids if kid.gender == gender]
else:
return self.kids
def get_brothers_and_sisters(self):
mother_kids = self.mother.kids if self.mother is not None else []
father_kids = self.father.kids if self.father is not None else []
return set(mother_kids + father_kids) - {self}
def get_brothers(self):
return [kid for kid in self.get_brothers_and_sisters() if kid.male()]
def get_sisters(self):
return [kid for kid in self.get_brothers_and_sisters() if kid.female()]
def is_direct_successor(self, person):
return person in self.kids

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

...........
----------------------------------------------------------------------
Ran 11 tests in 0.009s

OK

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

Виктория обнови решението на 01.04.2013 15:11 (преди над 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.mother = mother
+ self.father = father
+ self.kids = []
+ if mother is not None:
+ mother.kids.append(self)
+ if father is not None:
+ father.kids.append(self)
+
+ def female(self):
+ return self.gender == 'F'
+
+ def male(self):
+ return self.gender == 'M'
+
+ def children(self, gender=None):
+ if gender:
+ return list(filter(lambda kid: kid.gender == gender, self.kids))
+ else:
+ return self.kids
+
+ def get_brothers_and_sisters(self):
+ mother_kids = self.mother.kids if self.mother is not None else []
+ father_kids = self.father.kids if self.father is not None else []
+ return set(mother_kids + father_kids) - {self}
+
+ def get_brothers(self):
+ return [kid for kid in self.get_brothers_and_sisters() if kid.male()]
+
+ def get_sisters(self):
+ return [kid for kid in self.get_brothers_and_sisters() if kid.female()]
+
+ def is_direct_successor(self, person):
+ return person in self.kids

Виктория обнови решението на 01.04.2013 15:14 (преди над 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.mother = mother
self.father = father
self.kids = []
if mother is not None:
mother.kids.append(self)
if father is not None:
father.kids.append(self)
def female(self):
return self.gender == 'F'
def male(self):
return self.gender == 'M'
def children(self, gender=None):
if gender:
- return list(filter(lambda kid: kid.gender == gender, self.kids))
+ return [kid for kid in self.kids if kid.gender == gender]
else:
return self.kids
def get_brothers_and_sisters(self):
mother_kids = self.mother.kids if self.mother is not None else []
father_kids = self.father.kids if self.father is not None else []
return set(mother_kids + father_kids) - {self}
def get_brothers(self):
return [kid for kid in self.get_brothers_and_sisters() if kid.male()]
def get_sisters(self):
return [kid for kid in self.get_brothers_and_sisters() if kid.female()]
def is_direct_successor(self, person):
return person in self.kids