Решение на Родословно дърво от Нели Хатева

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

Към профила на Нели Хатева

Резултати

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

Код

class Person:
def __init__(self, name, gender, birth_year, father=None, mother=None):
self.name, self.gender, self.birth_year = name, gender, birth_year
self.father, self.mother = father, mother
if(self.father is not None):
self.father.successor.append(self)
if(self.mother is not None):
self.mother.successor.append(self)
self.successor = []
def children(self, gender=None):
if(gender == 'M'):
return [child for child in self.successor if child.gender == 'M']
elif(gender == 'F'):
return [child for child in self.successor if child.gender == 'F']
else:
return self.successor
def is_direct_successor(self, child):
return child in self.successor
def get_sisters(self):
sisters_from_mother, sisters_from_father = [], []
if(self.mother is not None):
sisters_from_mother = \
[child for child in self.mother.children('F') if self is not child]
if(self.father is not None):
sisters_from_father = \
[child for child in self.father.children('F') if self is not child]
return list(set(sisters_from_mother + sisters_from_father))
def get_brothers(self):
brothers_from_mother, brothers_from_father = [], []
if(self.mother is not None):
brothers_from_mother = \
[child for child in self.mother.children('M') if self is not child]
if(self.father is not None):
brothers_from_father = \
[child for child in self.father.children('M') if self is not child]
return list(set(brothers_from_mother + brothers_from_father))

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

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

OK

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

Нели обнови решението на 29.03.2013 15:48 (преди около 11 години)

+class Person:
+ def __init__(self, name, gender, birth_year, father=None, mother=None):
+ self.name, self.gender, self.birth_year = name, gender, birth_year
+ self.father, self.mother = father, mother
+ if(self.father is not None):
+ self.father.successor.append(self)
+ if(self.mother is not None):
+ self.mother.successor.append(self)
+ self.successor = []
+
+ def children(self, gender=None):
+ if(gender == 'M'):
+ return [child for child in self.successor if child.gender == 'M']
+ elif(gender == 'F'):
+ return [child for child in self.successor if child.gender == 'F']
+ else:
+ return self.successor
+
+ def is_direct_successor(self, child):
+ return child in self.successor
+
+ def get_sisters(self):
+ sisters_from_mother = self.mother.children('F')
+ sisters_from_father = self.father.children('F')
+ return list(set(sisters_from_mother + sisters_from_father))
+
+ def get_brothers(self):
+ brothers_from_mother = self.mother.children('M')
+ brothers_from_father = self.father.children('M')
+ return list(set(brothers_from_mother + brothers_from_father))

Нели обнови решението на 29.03.2013 16:41 (преди около 11 години)

class Person:
def __init__(self, name, gender, birth_year, father=None, mother=None):
self.name, self.gender, self.birth_year = name, gender, birth_year
self.father, self.mother = father, mother
if(self.father is not None):
self.father.successor.append(self)
if(self.mother is not None):
self.mother.successor.append(self)
self.successor = []
def children(self, gender=None):
if(gender == 'M'):
return [child for child in self.successor if child.gender == 'M']
elif(gender == 'F'):
return [child for child in self.successor if child.gender == 'F']
else:
return self.successor
def is_direct_successor(self, child):
return child in self.successor
def get_sisters(self):
- sisters_from_mother = self.mother.children('F')
- sisters_from_father = self.father.children('F')
+ sisters_from_mother, sisters_from_father = [], []
+ if(self.mother is not None):
+ sisters_from_mother = \
+ [child for child in self.mother.children('F') if self is not child]
+ if(self.father is not None):
+ sisters_from_father = \
+ [child for child in self.father.children('F') if self is not child]
return list(set(sisters_from_mother + sisters_from_father))
def get_brothers(self):
- brothers_from_mother = self.mother.children('M')
- brothers_from_father = self.father.children('M')
+ brothers_from_mother, brothers_from_father = [], []
+ if(self.mother is not None):
+ brothers_from_mother = \
+ [child for child in self.mother.children('M') if self is not child]
+ if(self.father is not None):
+ brothers_from_father = \
+ [child for child in self.father.children('M') if self is not child]
return list(set(brothers_from_mother + brothers_from_father))