Решение на Родословно дърво от Емил Гелев

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

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

Резултати

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

Код

class BreakingTheLawException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class Person:
def __init__(self, name, birth_year, gender, father=None, mother=None):
self.name = name
self.birth_year = birth_year
self.gender = gender
self.validate_years(father)
self.father = father
self.add_child(father)
self.validate_years(mother)
self.mother = mother
self.add_child(mother)
self.sons = []
self.daughters = []
def validate_years(self, parent):
exception_text = "Много сте малки за чаве, сигански работи ще правиш!"
if parent is not None and self.birth_year - parent.birth_year < 18:
raise BreakingTheLawException(exception_text)
def add_child(self, parent):
if parent is None:
return
if self.gender == 'F' or self.gender == 'f':
parent.daughters.append(self)
return
if self.gender == 'M' or self.gender == 'm':
parent.sons.append(self)
return
raise Exception('Not correct gender')
def get_brothers(self):
result = []
if self.father is not None:
result.extend(self.father.sons)
if self.mother is not None:
result.extend(self.mother.sons)
result = list(set(result))
if self in result:
result.remove(self)
return result
def get_sisters(self):
result = []
if self.father is not None:
result.extend(self.father.daughters)
if self.mother is not None:
result.extend(self.mother.daughters)
result = list(set(result))
if self in result:
result.remove(self)
return result
def children(self, gender='all'):
if gender == 'all':
return self.sons.copy() + self.daughters.copy()
if gender == 'F' or gender == 'f':
return self.daughters.copy()
if gender == 'M' or gender == 'm':
return self.sons.copy()
def is_direct_successor(self, person):
if person in self.sons or person in self.daughters:
return True
else:
return False

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

...........
----------------------------------------------------------------------
Ran 11 tests in 0.009s

OK

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

Емил обнови решението на 30.03.2013 15:02 (преди около 11 години)

+class BreakingTheLawException(Exception):
+
+ def __init__(self, value):
+ self.value = value
+
+ def __str__(self):
+ return repr(self.value)
+
+
+class Person:
+
+ def __init__(self, name, birth_year, gender, father=None, mother=None):
+ self.name = name
+ self.birth_year = birth_year
+ self.gender = gender
+ self.validate_years(father)
+ self.father = father
+ self.add_child(father)
+ self.validate_years(mother)
+ self.mother = mother
+ self.add_child(mother)
+ self.sons = []
+ self.daughters = []
+
+ def validate_years(self, parent):
+ exception_text = "Много сте малки за чаве, сигански работи ще правиш!"
+ if parent is not None and self.birth_year - parent.birth_year < 18:
+ raise BreakingTheLawException(exception_text)
+
+ def add_child(self, parent):
+ if parent is None:
+ return
+ if self.gender == 'F' or self.gender == 'f':
+ parent.daughters.append(self)
+ return
+ if self.gender == 'M' or self.gender == 'm':
+ parent.sons.append(self)
+ return
+ raise Exception('Not correct gender')
+
+ def get_brothers(self):
+ result = []
+ if self.father is not None:
+ result.extend(self.father.sons)
+ if self.mother is not None:
+ result.extend(self.mother.sons)
+ result = list(set(result))
+ if self in result:
+ result.remove(self)
+ return result
+
+ def get_sisters(self):
+ result = []
+ if self.father is not None:
+ result.extend(self.father.daughters)
+ if self.mother is not None:
+ result.extend(self.mother.daughters)
+ result = list(set(result))
+ if self in result:
+ result.remove(self)
+ return result
+
+ def children(self, gender='all'):
+ if gender == 'all':
+ return self.sons.copy() + self.daughters.copy()
+ if gender == 'F' or gender == 'f':
+ return self.daughters.copy()
+ if gender == 'M' or gender == 'm':
+ return self.sons.copy()
+
+ def is_direct_successor(self, person):
+ if person in self.sons or person in self.daughters:
+ return True
+ else:
+ return False