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

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

Към профила на Беатрис Бонева

Резултати

  • 0 точки от тестове
  • 0 бонус точки
  • 0 точки общо
  • 0 успешни тест(а)
  • 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._children = set()
for parent in [self.mother, self.father]:
if parent is not None:
self.add_parent(parent)
def add_parent(self, parent):
parent.add_child(self)
def add_child(self, child):
if (child.birth_year - self.birth_year) >= 18
and child not in self._children:
self._children.add(child)
if self.gender == "F":
child.mother = self
elif self.gender == "M":
child.father = self
def children(self, gender=None):
return [filter(lambda child:
child.gender == gender or gender is None, self._children)]
def get_brothers(self):
brothers = []
if self.mother is not None:
for son in self.mother.children(gender='M'):
if son not in brothers:
brothers += self.mother.children(gender="M")
if self.father is not None:
for son in self.father.children(gender='M'):
if son not in brothers:
brothers += self.father.children(gender="M")
if self in brothers:
brothers.remove(self)
return brothers
def get_sisters(self):
sisters = []
if self.mother is not None:
for daughter in self.mother.children(gender='F'):
if daughter not in sisters:
sisters += self.mother.children(gender="F")
if self.father is not None:
for daughter in self.father.children(gender='F'):
if daughter not in sisters:
sisters += self.father.children(gender="F")
if self in sisters:
sisters.remove(self)
return sisters
def is_direct_successor(self, person):
return self in person.children() or person in self.children()

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

File "lib/language/python/runner.py", line 99, in main
    test = imp.load_source('test', test_module)
  File "/opt/python3.3/lib/python3.3/imp.py", line 109, in load_source
    return _LoadSourceCompatibility(name, pathname, file).load_module(name)
  File "<frozen importlib._bootstrap>", line 586, in _check_name_wrapper
  File "<frozen importlib._bootstrap>", line 1023, in load_module
  File "<frozen importlib._bootstrap>", line 1004, in load_module
  File "<frozen importlib._bootstrap>", line 562, in module_for_loader_wrapper
  File "<frozen importlib._bootstrap>", line 869, in _load_module
  File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
  File "/tmp/d20130408-29081-1e4gve3/test.py", line 2, in <module>
    import solution

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

Беатрис обнови решението на 01.04.2013 16:48 (преди над 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._children = set()
+
+ for parent in [self.mother, self.father]:
+ if parent is not None:
+ self.add_parent(parent)
+
+ def add_parent(self, parent):
+ parent.add_child(self)
+
+ def add_child(self, child):
+ if (child.birth_year - self.birth_year) >= 18
+ and child not in self._children:
+ self._children.add(child)
+ if self.gender == "F":
+ child.mother = self
+ elif self.gender == "M":
+ child.father = self
+
+ def children(self, gender=None):
+ return [filter(lambda child:
+ child.gender == gender or gender is None, self._children)]
+
+ def get_brothers(self):
+ brothers = []
+ if self.mother is not None:
+ for son in self.mother.children(gender='M'):
+ if son not in brothers:
+ brothers += self.mother.children(gender="M")
+ if self.father is not None:
+ for son in self.father.children(gender='M'):
+ if son not in brothers:
+ brothers += self.father.children(gender="M")
+ if self in brothers:
+ brothers.remove(self)
+ return brothers
+
+ def get_sisters(self):
+ sisters = []
+ if self.mother is not None:
+ for daughter in self.mother.children(gender='F'):
+ if daughter not in sisters:
+ sisters += self.mother.children(gender="F")
+ if self.father is not None:
+ for daughter in self.father.children(gender='F'):
+ if daughter not in sisters:
+ sisters += self.father.children(gender="F")
+ if self in sisters:
+ sisters.remove(self)
+ return sisters
+
+ def is_direct_successor(self, person):
+ return self in person.children() or person in self.children()