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

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

Към профила на Николай Манолов

Резултати

  • 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.father = father
self.mother = mother
self.kids = []
self.add_child()
def add_child(self):
if isinstance(self.father, Person):
self.father.kids.append(self)
if isinstance(self.mother, Person):
self.mother.kids.append(self)
def get_sisters(self):
sisters = []
for parent in [self.father, self.mother]:
if isinstance(parent, Person):
sisters += list(
filter(lambda child: child.gender == 'F' and child != self,
parent.kids))
return list(set(sisters))
def get_brothers(self):
brothers = []
for parent in [self.father, self.mother]:
if isinstance(parent, Person):
brothers += list(
filter(lambda child: child.gender == 'M' and child != self,
parent.kids))
return list(set(brothers))
def children(self, gender=None):
if gender is not None:
return list(filter(lambda child: child.gender == gender,
self.kids))
return self.kids
def is_direct_successor(self, successor):
if successor in self.kids:
return True
return False

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

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

OK

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

Николай обнови решението на 31.03.2013 19:28 (преди над 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.father = father
+ self.mother = mother
+ self.kids = []
+ self.add_child()
+
+ def add_child(self):
+ if type(self.father).__name__ == 'Person':
+ self.father.kids.append(self)
+
+ if type(self.mother).__name__ == 'Person':
+ self.mother.kids.append(self)
+
+ def get_sisters(self):
+ sisters = []
+ for parent in [self.father, self.mother]:
+ if type(parent).__name__ == 'Person':
+ sisters += list(
+ filter(lambda child: child.gender == 'F' and child != self,
+ parent.kids))
+ return list(set(sisters))
+
+ def get_brothers(self):
+ brothers = []
+ for parent in [self.father, self.mother]:
+ if type(parent).__name__ == 'Person':
+ brothers += list(
+ filter(lambda child: child.gender == 'M' and child != self,
+ parent.kids))
+ return list(set(brothers))
+
+ def children(self, gender=None):
+ if gender is not None:
+ return list(filter(lambda child: child.gender == gender,
+ self.kids))
+ return self.kids
+
+ def is_direct_successor(self, successor):
+ if successor in self.kids:
+ return True
+ return False

Николай обнови решението на 31.03.2013 19:33 (преди над 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.father = father
self.mother = mother
self.kids = []
self.add_child()
def add_child(self):
- if type(self.father).__name__ == 'Person':
+ if isinstance(self.father, Person):
self.father.kids.append(self)
- if type(self.mother).__name__ == 'Person':
+ if isinstance(self.mother, Person):
self.mother.kids.append(self)
def get_sisters(self):
sisters = []
for parent in [self.father, self.mother]:
- if type(parent).__name__ == 'Person':
+ if isinstance(parent, Person):
sisters += list(
filter(lambda child: child.gender == 'F' and child != self,
parent.kids))
return list(set(sisters))
def get_brothers(self):
brothers = []
for parent in [self.father, self.mother]:
if type(parent).__name__ == 'Person':
brothers += list(
filter(lambda child: child.gender == 'M' and child != self,
parent.kids))
return list(set(brothers))
def children(self, gender=None):
if gender is not None:
return list(filter(lambda child: child.gender == gender,
self.kids))
return self.kids
def is_direct_successor(self, successor):
if successor in self.kids:
return True
return False

Николай обнови решението на 31.03.2013 19:37 (преди над 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.father = father
self.mother = mother
self.kids = []
self.add_child()
def add_child(self):
if isinstance(self.father, Person):
self.father.kids.append(self)
if isinstance(self.mother, Person):
self.mother.kids.append(self)
def get_sisters(self):
sisters = []
for parent in [self.father, self.mother]:
if isinstance(parent, Person):
sisters += list(
filter(lambda child: child.gender == 'F' and child != self,
parent.kids))
return list(set(sisters))
def get_brothers(self):
brothers = []
for parent in [self.father, self.mother]:
- if type(parent).__name__ == 'Person':
+ if isinstance(parent, Person):
brothers += list(
filter(lambda child: child.gender == 'M' and child != self,
parent.kids))
return list(set(brothers))
def children(self, gender=None):
if gender is not None:
return list(filter(lambda child: child.gender == gender,
self.kids))
return self.kids
def is_direct_successor(self, successor):
if successor in self.kids:
return True
return False