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

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

Към профила на Георги Харизанов

Резултати

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

Код

class Person:
def __init__(self, name, gender, birth_year, father=None, mother=None):
self.parents = set()
self.children_set = set()
self.siblings = set()
self.remove = set() # to avoid adding self to siblings
self.name = name
self.birth_year = birth_year
self.gender = gender
if mother is not None:
self._add_parent(mother)
if father is not None:
self._add_parent(father)
self.remove.add(self) # init set containing only self
def _add_child(self, new_person):
self.children_set.add(new_person)
def children(self, gender="MF"):
return [person for person in self.children_set
if person.gender in gender]
def _add_parent(self, parent):
parent._add_child(self)
self.parents.add(parent)
def is_direct_successor(self, other_person):
return other_person in self.children_set
def _generate_siblings(self):
for parent in self.parents:
self.siblings = self.siblings.union(parent.children_set)
self.siblings = self.siblings.difference(self.remove)
def _get_siblings(self, gender):
return [person for person in list(self.siblings)
if person.gender == gender]
def get_sisters(self):
self._generate_siblings()
return self._get_siblings("F")
def get_brothers(self):
self._generate_siblings()
return self._get_siblings("M")

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

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

OK

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

Георги обнови решението на 01.04.2013 14:55 (преди над 11 години)

+class Person:
+ def __init__(self, name, gender, birth_year, father=None, mother=None):
+ self.parents = set()
+ self.children_set = set()
+ self.siblings = set()
+ self.remove = set() # to avoid adding self to siblings
+ self.name = name
+ self.birth_year = birth_year
+ self.gender = gender
+ if mother is not None:
+ self._add_parent(mother)
+ mother._add_child(self)
+ if father is not None:
+ self._add_parent(father)
+ father._add_child(self)
+ self.remove.add(self) # init set containing only self
+
+ def _add_child(self, new_person):
+ self.children_set.add(new_person)
+
+ def children(self, gender="MF"):
+ return list(set([person for person in self.children_set
+ if person.gender in gender]))
+
+ def _add_parent(self, parent):
+ parent._add_child(self)
+ self.parents.add(parent)
+
+ def is_direct_successor(self, other_person):
+ if(other_person in self.children_set):
+ return True
+ return False
+
+ def _generate_siblings(self):
+ for parent in self.parents:
+ self.siblings = self.siblings.union(parent.children_set)
+ self.siblings = self.siblings.difference(self.remove)
+
+ def _get_siblings(self, gender):
+ return [person for person in list(self.siblings)
+ if person.gender == gender]
+
+ def get_sisters(self):
+ self._generate_siblings()
+ return self._get_siblings("F")
+
+ def get_brothers(self):
+ self._generate_siblings
+ return self._get_siblings("M")

Георги обнови решението на 01.04.2013 15:55 (преди над 11 години)

class Person:
def __init__(self, name, gender, birth_year, father=None, mother=None):
self.parents = set()
self.children_set = set()
self.siblings = set()
self.remove = set() # to avoid adding self to siblings
self.name = name
self.birth_year = birth_year
self.gender = gender
if mother is not None:
self._add_parent(mother)
mother._add_child(self)
if father is not None:
self._add_parent(father)
father._add_child(self)
self.remove.add(self) # init set containing only self
def _add_child(self, new_person):
self.children_set.add(new_person)
def children(self, gender="MF"):
- return list(set([person for person in self.children_set
- if person.gender in gender]))
+ return list(set([person for person in self.children_set
+ if person.gender in gender]))
def _add_parent(self, parent):
parent._add_child(self)
self.parents.add(parent)
def is_direct_successor(self, other_person):
if(other_person in self.children_set):
return True
return False
def _generate_siblings(self):
for parent in self.parents:
self.siblings = self.siblings.union(parent.children_set)
self.siblings = self.siblings.difference(self.remove)
def _get_siblings(self, gender):
return [person for person in list(self.siblings)
- if person.gender == gender]
+ if person.gender == gender]
def get_sisters(self):
self._generate_siblings()
return self._get_siblings("F")
def get_brothers(self):
self._generate_siblings
- return self._get_siblings("M")
+ return self._get_siblings("M")

Георги обнови решението на 01.04.2013 16:06 (преди над 11 години)

class Person:
def __init__(self, name, gender, birth_year, father=None, mother=None):
self.parents = set()
self.children_set = set()
self.siblings = set()
self.remove = set() # to avoid adding self to siblings
self.name = name
self.birth_year = birth_year
self.gender = gender
if mother is not None:
self._add_parent(mother)
- mother._add_child(self)
if father is not None:
self._add_parent(father)
- father._add_child(self)
self.remove.add(self) # init set containing only self
def _add_child(self, new_person):
self.children_set.add(new_person)
def children(self, gender="MF"):
- return list(set([person for person in self.children_set
- if person.gender in gender]))
+ return [person for person in self.children_set
+ if person.gender in gender]
def _add_parent(self, parent):
parent._add_child(self)
self.parents.add(parent)
def is_direct_successor(self, other_person):
- if(other_person in self.children_set):
- return True
- return False
+ return other_person in self.children_set
def _generate_siblings(self):
for parent in self.parents:
self.siblings = self.siblings.union(parent.children_set)
self.siblings = self.siblings.difference(self.remove)
def _get_siblings(self, gender):
return [person for person in list(self.siblings)
if person.gender == gender]
def get_sisters(self):
self._generate_siblings()
return self._get_siblings("F")
def get_brothers(self):
- self._generate_siblings
+ self._generate_siblings()
return self._get_siblings("M")