Михаил обнови решението на 01.04.2013 01:04 (преди над 11 години)
+from itertools import chain
+
+class Person:
+ def __init__(self, name, birth_year, gender, father = None, mother = None):
+ self.name = name
+ self.birth_year = birth_year
+ self.gender = gender
+ if (father == None or father.gender != 'M' or
+ father.birth_year > birth_year+18):
+ self.father = None
+ else:
+ self.father = father
+ father.kids.append(self)
+ if (mother == None or mother.gender != 'F' or
+ mother.birth_year > birth_year+18):
+ self.mother = None
+ else:
+ self.mother = mother
+ mother.kids.append(self)
+ self.kids = []
+
+ def children(self, gender = None):
+ if gender == None:
+ return self.kids
+ return list(filter(lambda x: x.gender == gender, self.kids))
+
+ def is_direct_successor(self, the_other_person):
+ if (self.name == the_other_person.father.name or
+ self.name == the_other_person.mother.name):
+ return True
+ return False
+
+ def get_siblings(self, gender):
+ siblings_from_father = self.father.children(gender)
+ siblings_from_mother = self.mother.children(gender)
+ all_siblings = set(chain(siblings_from_father, siblings_from_mother))
+ if self.gender == gender:
+ all_siblings.remove(self)
+ return list(all_siblings)
+
+ def get_brothers(self):
+ return self.get_siblings('M')
+
+ def get_sisters(self):
+ return self.get_siblings('F')