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

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

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

Резултати

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

Код

class Person:
def __init__(self, name, gender, birth_year, father=None, mother=None):
self.name = name
self.gender = gender
self.birth_year = birth_year
self.father = father
self.mother = mother
self.successors = {'M': set(), 'F': set()}
self.siblings = {'M': set(), 'F': set()}
if self.mother:
self.siblings['M'].update(self.mother.successors['M'])
self.siblings['F'].update(self.mother.successors['F'])
for sibling in self.mother.successors['M'].union(
self.mother.successors['F']):
sibling.siblings[self.gender].add(self)
self.mother.successors[self.gender].add(self)
if self.father:
self.siblings['M'].update(self.father.successors['M'])
self.siblings['F'].update(self.father.successors['F'])
for sibling in self.father.successors['M'].union(
self.father.successors['F']):
sibling.siblings[self.gender].add(self)
self.father.successors[self.gender].add(self)
def get_brothers(self):
return list(self.siblings['M'])
def get_sisters(self):
return list(self.siblings['F'])
def children(self, gender='MF'):
if gender is 'MF':
return list(self.successors['M'].union(self.successors['F']))
else:
return list(self.successors[gender])
def is_direct_successor(self, successor):
return successor in self.successors['M'].union(self.successors['F'])

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

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

OK

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

Георги обнови решението на 30.03.2013 11:04 (преди над 11 години)

+class Person:
+
+ def __init__(self, name, gender, birth_year, father=None, mother=None):
+
+ self.name = name
+ self.gender = gender
+ self.birth_year = birth_year
+
+ self.father = father
+ self.mother = mother
+
+ self.successors = {'M': set(), 'F': set()}
+ self.siblings = {'M': set(), 'F': set()}
+
+ if self.mother:
+ self.siblings['M'].update(self.mother.successors['M'])
+ self.siblings['F'].update(self.mother.successors['F'])
+
+ for sibling in self.mother.successors['M'].union(self.mother.successors['F']):
+ sibling.siblings[self.gender].add(self)
+
+ self.mother.successors[self.gender].add(self)
+
+ if self.father:
+ self.siblings['M'].update(self.father.successors['M'])
+ self.siblings['F'].update(self.father.successors['F'])
+
+ for sibling in self.father.successors['M'].union(self.father.successors['F']):
+ sibling.siblings[self.gender].add(self)
+
+ self.father.successors[self.gender].add(self)
+
+ def get_brothers(self):
+ return list(self.siblings['M'])
+
+ def get_sisters(self):
+ return list(self.siblings['F'])
+
+ def children(self, gender='MF'):
+ if gender is 'MF':
+ return list(self.successors['M'].union(self.successors['F']))
+ else:
+ return list(self.successors[gender])
+
+ def is_direct_successor(self, target):
+ return target in self.successors['M'].union(self.successors['F'])

Георги обнови решението на 30.03.2013 19:39 (преди над 11 години)

class Person:
def __init__(self, name, gender, birth_year, father=None, mother=None):
self.name = name
self.gender = gender
self.birth_year = birth_year
self.father = father
self.mother = mother
self.successors = {'M': set(), 'F': set()}
self.siblings = {'M': set(), 'F': set()}
if self.mother:
self.siblings['M'].update(self.mother.successors['M'])
self.siblings['F'].update(self.mother.successors['F'])
- for sibling in self.mother.successors['M'].union(self.mother.successors['F']):
+ for sibling in self.mother.successors['M'].union(
+ self.mother.successors['F']):
sibling.siblings[self.gender].add(self)
self.mother.successors[self.gender].add(self)
if self.father:
self.siblings['M'].update(self.father.successors['M'])
self.siblings['F'].update(self.father.successors['F'])
- for sibling in self.father.successors['M'].union(self.father.successors['F']):
+ for sibling in self.father.successors['M'].union(
+ self.father.successors['F']):
sibling.siblings[self.gender].add(self)
self.father.successors[self.gender].add(self)
def get_brothers(self):
return list(self.siblings['M'])
def get_sisters(self):
return list(self.siblings['F'])
def children(self, gender='MF'):
if gender is 'MF':
return list(self.successors['M'].union(self.successors['F']))
else:
return list(self.successors[gender])
def is_direct_successor(self, target):
return target in self.successors['M'].union(self.successors['F'])

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

class Person:
def __init__(self, name, gender, birth_year, father=None, mother=None):
self.name = name
self.gender = gender
self.birth_year = birth_year
self.father = father
self.mother = mother
self.successors = {'M': set(), 'F': set()}
self.siblings = {'M': set(), 'F': set()}
if self.mother:
self.siblings['M'].update(self.mother.successors['M'])
self.siblings['F'].update(self.mother.successors['F'])
for sibling in self.mother.successors['M'].union(
self.mother.successors['F']):
sibling.siblings[self.gender].add(self)
self.mother.successors[self.gender].add(self)
if self.father:
self.siblings['M'].update(self.father.successors['M'])
self.siblings['F'].update(self.father.successors['F'])
for sibling in self.father.successors['M'].union(
self.father.successors['F']):
sibling.siblings[self.gender].add(self)
self.father.successors[self.gender].add(self)
def get_brothers(self):
return list(self.siblings['M'])
def get_sisters(self):
return list(self.siblings['F'])
def children(self, gender='MF'):
if gender is 'MF':
return list(self.successors['M'].union(self.successors['F']))
else:
return list(self.successors[gender])
- def is_direct_successor(self, target):
- return target in self.successors['M'].union(self.successors['F'])
+ def is_direct_successor(self, successor):
+ return successor in self.successors['M'].union(self.successors['F'])