Решение на Родословно дърво от Михаил Станин

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

Към профила на Михаил Станин

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 10 успешни тест(а)
  • 1 неуспешни тест(а)

Код

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')

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

........E..
======================================================================
ERROR: test_indirect_successor (test.PersonTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20130408-29081-166a0kq/test.py", line 57, in test_indirect_successor
    self.assertFalse(self.adam.is_direct_successor(irrelevant))
  File "/tmp/d20130408-29081-166a0kq/solution.py", line 28, in is_direct_successor
    if (self.name == the_other_person.father.name or
AttributeError: 'NoneType' object has no attribute 'name'

----------------------------------------------------------------------
Ran 11 tests in 0.007s

FAILED (errors=1)

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

Михаил обнови решението на 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')