Решение на Родословно дърво от Йордан Джамбазов

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

Към профила на Йордан Джамбазов

Резултати

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

Код

class Person:
MIN_AGE_TO_HAVE_CHILDREN = 18
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
for parent in ('father', 'mother'):
if getattr(self, parent, False):
getattr(self, parent).add_child(self)
self._children = []
def get_coevals(self, gender):
coevals = {parent_child for parent in ('father', 'mother')
if getattr(self, parent, False) for parent_child in
getattr(self, parent).children(gender) if parent_child is
not self}
return list(coevals)
def get_brothers(self):
return self.get_coevals('M')
def get_sisters(self):
return self.get_coevals('F')
def add_parent(self, parent):
if self not in parent.children():
return parent.add_child(self)
def add_child(self, child):
if isinstance(child, Person) and child not in self.children():
parent_age = child.birth_year - self.birth_year
if parent_age < self.MIN_AGE_TO_HAVE_CHILDREN:
return
self._children.append(child)
setattr(child, 'father' if self.gender == 'M' else 'mother', self)
def children(self, gender=None):
return list(filter(lambda child: child.gender == gender or
gender is None, self._children))
def is_direct_successor(self, person):
return person in self.children() or self in person.children()

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

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

OK

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

Йордан обнови решението на 26.03.2013 13:04 (преди над 11 години)

+MIN_PARENT_AGE = 18
+
+
+class Person:
+ def __init__(self, name, birth_year, gender):
+ self.name = name
+ self.birth_year = birth_year
+ self.gender = gender
+ self.father = None
+ self.mother = None
+ self._children = []
+
+ def get_brothers(self):
+ brothers = []
+ if self.father is not None:
+ brothers += self.father.children('male')
+ if self.mother is not None:
+ brothers += self.mother.children('male')
+ return list(set(brothers))
+
+ def get_sisters(self):
+ sisters = []
+ if self.father is not None:
+ sisters += self.father.children('female')
+ if self.mother is not None:
+ sisters += self.mother.children('female')
+ return list(set(sisters))
+
+ def add_parent(self, parent):
+ if self not in parent.children():
+ return parent.add_child(self)
+
+ def add_child(self, child):
+ if isinstance(child, Person) and child not in self.children():
+ if (child.birth_year - self.birth_year) < MIN_PARENT_AGE:
+ return
+
+ self._children.append(child)
+ if self.gender == 'male':
+ child.father = self
+ if self.gender == 'female':
+ child.mother = self
+
+ def children(self, gender=None):
+ return list(filter(lambda child: child.gender == gender or
+ gender is None, self._children))
+
+ def is_direct_successor(self, person):
+ return person in self.children() or self in person.children()

Йордан обнови решението на 26.03.2013 13:17 (преди над 11 години)

MIN_PARENT_AGE = 18
class Person:
- def __init__(self, name, birth_year, gender):
+ def __init__(self, name, birth_year, gender, father=None, mother=None):
self.name = name
self.birth_year = birth_year
self.gender = gender
- self.father = None
- self.mother = None
+ self.father = father
+ self.mother = mother
+
+ if self.father is not None:
+ self.father.add_child(self)
+ if self.mother is not None:
+ self.mother.add_child(self)
+
self._children = []
def get_brothers(self):
brothers = []
if self.father is not None:
- brothers += self.father.children('male')
+ brothers += self.father.children('M')
if self.mother is not None:
- brothers += self.mother.children('male')
+ brothers += self.mother.children('M')
return list(set(brothers))
def get_sisters(self):
sisters = []
if self.father is not None:
- sisters += self.father.children('female')
+ sisters += self.father.children('F')
if self.mother is not None:
- sisters += self.mother.children('female')
+ sisters += self.mother.children('F')
return list(set(sisters))
def add_parent(self, parent):
if self not in parent.children():
return parent.add_child(self)
def add_child(self, child):
if isinstance(child, Person) and child not in self.children():
if (child.birth_year - self.birth_year) < MIN_PARENT_AGE:
return
self._children.append(child)
- if self.gender == 'male':
+ if self.gender == 'M':
child.father = self
- if self.gender == 'female':
+ if self.gender == 'F':
child.mother = self
def children(self, gender=None):
return list(filter(lambda child: child.gender == gender or
gender is None, self._children))
def is_direct_successor(self, person):
return person in self.children() or self in person.children()

Йордан обнови решението на 27.03.2013 11:49 (преди над 11 години)

-MIN_PARENT_AGE = 18
-
-
class Person:
+ MIN_AGE_TO_HAVE_CHILDREN = 18
+
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
-
- if self.father is not None:
- self.father.add_child(self)
- if self.mother is not None:
- self.mother.add_child(self)
-
+
+ for parent in ('father', 'mother'):
+ if getattr(self, parent, False):
+ getattr(self, parent).add_child(self)
+
self._children = []
+ def get_coevals(self, gender):
+ coevals = {parent_child for parent in ('father', 'mother')
+ if getattr(self, parent, False) for parent_child in
+ getattr(self, parent).children(gender) if parent_child is
+ not self}
+ return list(coevals)
+
def get_brothers(self):
- brothers = []
- if self.father is not None:
- brothers += self.father.children('M')
- if self.mother is not None:
- brothers += self.mother.children('M')
- return list(set(brothers))
+ return self.get_coevals('M')
def get_sisters(self):
- sisters = []
- if self.father is not None:
- sisters += self.father.children('F')
- if self.mother is not None:
- sisters += self.mother.children('F')
- return list(set(sisters))
+ return self.get_coevals('F')
def add_parent(self, parent):
if self not in parent.children():
return parent.add_child(self)
def add_child(self, child):
if isinstance(child, Person) and child not in self.children():
- if (child.birth_year - self.birth_year) < MIN_PARENT_AGE:
+ parent_age = child.birth_year - self.birth_year
+ if parent_age < self.MIN_AGE_TO_HAVE_CHILDREN:
return
-
self._children.append(child)
- if self.gender == 'M':
- child.father = self
- if self.gender == 'F':
- child.mother = self
+ setattr(child, 'father' if self.gender == 'M' else 'mother', self)
def children(self, gender=None):
return list(filter(lambda child: child.gender == gender or
gender is None, self._children))
def is_direct_successor(self, person):
return person in self.children() or self in person.children()