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

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

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

Резултати

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

Код

class Person:
def __init__(self, name=None, gender=None, birth_year=None,
father=None, mother=None):
self.name = name
self.birth_year = birth_year
self.gender = gender
self.kids = set([])
self.siblings = set([])
self.add_parrent(father)
self.add_parrent(mother)
def get_brothers(self):
return [brother for brother in self.siblings if brother.gender == 'M']
def get_sisters(self):
return [sister for sister in self.siblings if sister.gender == 'F']
def add_parrent(self, parrent):
if parrent is not None:
if parrent.gender == 'M':
self.father = parrent
else:
self.mother = parrent
self.add_siblings(parrent.kids)
parrent.kids.add(self)
def add_siblings(self, siblings):
for sib in siblings:
self.siblings.add(sib)
sib.siblings.add(self)
def children(self, gender='MF'):
return [kid for kid in self.kids if kid.gender in gender]
def is_direct_successor(self, predecessor):
return True if predecessor in self.kids else False

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

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

OK

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

Христо обнови решението на 29.03.2013 00:16 (преди над 11 години)

+class Person:
+
+ def __init__(self, name='', gender='', birth_year=None,
+ father=None, mother=None):
+ self.name = name
+ self.birth_year = birth_year
+ self.gender = gender
+ self.siblings = []
+ self._children = []
+ self.add_parrent(father)
+ self.add_parrent(mother)
+
+ def get_brothers(self):
+ return [brother for brother in self.siblings if brother.gender == 'M']
+
+ def get_sisters(self):
+ return [sister for sister in self.siblings if sister.gender == 'F']
+
+ def add_parrent(self, parrent):
+ if parrent is not None:
+ if parrent.gender == 'M':
+ self.father = parrent
+ self.add_siblings(parrent._children)
+ else:
+ self.mother = parrent
+ self.add_siblings(parrent._children)
+ if self not in parrent._children:
+ parrent._children.append(self)
+
+ def add_siblings(self, siblings):
+ for kid in siblings:
+ if kid not in self.siblings:
+ self.siblings.append(kid)
+ if self not in kid.siblings:
+ kid.siblings.append(self)
+
+ def children(self, gender='MF'):
+ return [kid for kid in self._children if kid.gender in gender]
+
+ def is_direct_successor(self, ancestor):
+ if ancestor in self._children:
+ return True
+ return False

Христо обнови решението на 30.03.2013 22:57 (преди над 11 години)

class Person:
- def __init__(self, name='', gender='', birth_year=None,
+ def __init__(self, name=None, gender=None, birth_year=None,
father=None, mother=None):
self.name = name
self.birth_year = birth_year
self.gender = gender
- self.siblings = []
- self._children = []
+ self.kids = set([])
+ self.siblings = set([])
self.add_parrent(father)
self.add_parrent(mother)
def get_brothers(self):
return [brother for brother in self.siblings if brother.gender == 'M']
def get_sisters(self):
return [sister for sister in self.siblings if sister.gender == 'F']
def add_parrent(self, parrent):
if parrent is not None:
if parrent.gender == 'M':
self.father = parrent
- self.add_siblings(parrent._children)
else:
self.mother = parrent
- self.add_siblings(parrent._children)
- if self not in parrent._children:
- parrent._children.append(self)
+ self.add_siblings(parrent.kids)
+ parrent.kids.add(self)
def add_siblings(self, siblings):
- for kid in siblings:
- if kid not in self.siblings:
- self.siblings.append(kid)
- if self not in kid.siblings:
- kid.siblings.append(self)
+ for sib in siblings:
+ self.siblings.add(sib)
+ sib.siblings.add(self)
def children(self, gender='MF'):
- return [kid for kid in self._children if kid.gender in gender]
+ return [kid for kid in self.kids if kid.gender in gender]
- def is_direct_successor(self, ancestor):
- if ancestor in self._children:
+ def is_direct_successor(self, predecessor):
- return True
+ return True if predecessor in self.kids else False
- return False