Симеон обнови решението на 26.03.2013 14:43 (преди около 12 години)
+class Person:
+
+ def __init__(self, name, birth_year, gender, father=None, mother=None):
+ self.name = name
+ self.birth_year = birth_year
+ self.age = 2013 - birth_year
+ self.list_of_children = []
+ self.gender = gender
+ self.father = father
+ if father is not None:
+ self.father.list_of_children.append(self)
+ self.mother = mother
+ if mother is not None:
+ self.mother.list_of_children.append(self)
+
+ def children(self, gender=None):
+ if gender is None:
+ return self.list_of_children
+ elif gender == "M":
+ return [children for children
+ in self.list_of_children if children.gender == "M"]
+ elif gender == "F":
+ return [children for children
+ in self.list_of_children if children.gender == "F"]
+
+ def get_sisters(self):
+ if self.mother is not None and self.father is not None:
+ sisters_from_mother = [children for children
+ in self.mother.children()
+ if children.gender == "F"]
+ sisters_from_father = [children for children
+ in self.father.children()
+ if children.gender == "F"]
+ return list(set(sisters_from_mother + sisters_from_father))
+ else:
+ return []
+
+ def get_brothers(self):
+ if self.mother is not None and self.father is not None:
+ brothers_from_mother = [children for children
+ in self.mother.children()
+ if children.gender == "M"]
+ brothers_from_father = [children for children
+ in self.father.children()
+ if children.gender == "M"]
+ return list(set(brothers_from_mother + brothers_from_father))
+ else:
+ return []
+
+ def is_direct_successor(self, person):
+ if person in self.get_sisters():
+ return True
+ elif person in self.get_brothers():
+ return True
+ elif person in self.children():
+ return True
+ elif person is self.mother:
+ return True
+ elif person is self.father:
+ return True
+ return False
Минава тестовеje, и pep8 проверка, но все пак ще е хубаво ако има нещо за дооправяне, да споделите скоро :)
Последната функция исках да я направя с or, or, or вместо с elif, elif и т.н. но не се получи нещо ... затова е в такъв вариант. Ако имате някакви забележки, споделете, днес съм в отпуска :)