Решение на Родословно дърво от Георги Урумов

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

Към профила на Георги Урумов

Резултати

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

Код

class Person (object):
def __init__(self, name, gender, birth_year, father=None, mother=None):
self.name = name
self.gender = gender
self.birth_year = birth_year
self.kids = []
self.siblings = []
if (father and birth_year - father.birth_year >= 18):
father.kids.append(self)
self.father = father
if (mother and birth_year - mother.birth_year >= 18):
mother.kids.append(self)
self.mother = mother
def get_siblings(self):
if hasattr(self, "father"):
self.siblings.extend(self.father.kids)
if hasattr(self, "mother"):
self.siblings.extend(self.mother.kids)
def get_brothers(self):
self.get_siblings()
return list({x for x in self.siblings if x is not self
and x.gender == 'M'})
def get_sisters(self):
self.get_siblings()
return list({x for x in self.siblings if x is not self
and x.gender == 'F'})
def children(self, gender=None):
if (gender):
return [x for x in self.kids if x.gender == gender]
else:
return self.kids
def is_direct_successor(self, other):
return other in self.kids

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

...........
----------------------------------------------------------------------
Ran 11 tests in 0.007s

OK

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

Георги обнови решението на 30.03.2013 02:45 (преди над 11 години)

+class Person (object):
+
+ def __init__(self, name, gender, birth_year, father=None, mother=None):
+ self.name = name
+ self.gender = gender
+ self.birth_year = birth_year
+ self.kids = []
+ self.siblings = []
+ if (father and birth_year - father.birth_year >= 18):
+ father.kids.append(self)
+ self.father = father
+ if (mother and birth_year - mother.birth_year >= 18):
+ mother.kids.append(self)
+ self.mother = mother
+
+ def get_siblings(self):
+ if hasattr(self, "father"):
+ self.siblings.extend(self.father.kids)
+ if hasattr(self, "mother"):
+ self.siblings.extend(self.mother.kids)
+
+ def get_brothers(self):
+ self.get_siblings()
+ return list({x for x in self.siblings if x is not self
+ and x.gender == 'M'})
+
+ def get_sisters(self):
+ self.get_siblings()
+ return list({x for x in self.siblings if x is not self
+ and x.gender == 'F'})
+
+ def children(self, gender=None):
+ if (gender):
+ return [x for x in self.kids if x.gender == gender]
+ else:
+ return self.kids
+
+ def is_direct_successor(self, other):
+ return other in self.kids