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

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

Към профила на Мартин Маринов

Резултати

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

Код

from itertools import chain
class Person:
def __init__(self, name, birth_year, gender,
father=None, mother=None, *arg, **kwargs):
self.name = name
self.birth_year = birth_year
self.gender = gender
self._parents = [parent for parent in (father, mother) if parent]
self._children = set()
# Update parents' children
for parent in self._parents:
parent._add_child(self)
def get_brothers(self):
return list(self._get_siblings(gender='M'))
def get_sisters(self):
return list(self._get_siblings(gender='F'))
def children(self, gender=None):
if not gender in ('M', 'F'):
return list(self._children)
else:
return [child for child in self._children
if child.gender == gender]
def is_direct_successor(self, other_person):
return other_person in self._parents or other_person in self._children
def _add_child(self, child):
self._children.add(child)
def _get_siblings(self, gender):
return set(sibling for sibling in
chain(*[parent.children() for parent in self._parents])
if not sibling is self and sibling.gender == gender)

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

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

OK

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

Мартин обнови решението на 31.03.2013 23:58 (преди над 11 години)

+from itertools import chain
+
+
+class Person:
+
+ def __init__(self, name, birth_year, gender,
+ father=None, mother=None, *arg, **kwargs):
+ self.name = name
+ self.birth_year = birth_year
+ self.gender = gender
+ self._parents = [parent for parent in (father, mother) if parent]
+ self._children = set()
+
+ # Update parents' children
+ for parent in self._parents:
+ parent._add_child(self)
+
+ def get_brothers(self):
+ return list(self._get_siblings(gender='M'))
+
+ def get_sisters(self):
+ return list(self._get_siblings(gender='F'))
+
+ def children(self, gender=None):
+ if not gender in ('M', 'F'):
+ return list(self._children)
+ else:
+ return [child for child in self._children if child.gender == gender]
+
+ def is_direct_successor(self, other_person):
+ return other_person in self._parents or other_person in self._children
+
+ def _add_child(self, child):
+ self._children.add(child)
+
+ def _get_siblings(self, gender):
+ return set(sibling for sibling in
+ chain(*[parent.children() for parent in self._parents])
+ if not sibling is self and sibling.gender == gender)

Мартин обнови решението на 31.03.2013 23:59 (преди над 11 години)

from itertools import chain
class Person:
def __init__(self, name, birth_year, gender,
father=None, mother=None, *arg, **kwargs):
self.name = name
self.birth_year = birth_year
self.gender = gender
self._parents = [parent for parent in (father, mother) if parent]
self._children = set()
# Update parents' children
for parent in self._parents:
parent._add_child(self)
def get_brothers(self):
return list(self._get_siblings(gender='M'))
def get_sisters(self):
return list(self._get_siblings(gender='F'))
def children(self, gender=None):
if not gender in ('M', 'F'):
return list(self._children)
else:
- return [child for child in self._children if child.gender == gender]
+ return [child for child in self._children
+ if child.gender == gender]
def is_direct_successor(self, other_person):
return other_person in self._parents or other_person in self._children
def _add_child(self, child):
self._children.add(child)
def _get_siblings(self, gender):
return set(sibling for sibling in
chain(*[parent.children() for parent in self._parents])
if not sibling is self and sibling.gender == gender)