Решение на Родословно дърво от Камелия Пандаклиева

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

Към профила на Камелия Пандаклиева

Резултати

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

Код

class Person:
def __init__(self, name, gender, birth_year,
mother=None, father=None):
self.name = name
self.gender = gender
self.birth_year = birth_year
self.mother = mother
self.father = father
self.children_list = []
if mother is not None:
mother.children_list.append(self)
if father is not None:
father.children_list.append(self)
def get_siblings(self, _gender):
result = {}
if self.mother is not None:
result = set(self.mother.children(gender=_gender))
if self.father is not None:
result = result.union(set(self.father.children(gender=_gender)))
if self in result:
result = result.remove(self)
return list(result)
def get_brothers(self):
return self.get_siblings('M')
def get_sisters(self):
return self.get_siblings('F')
def children(self, gender=None):
result = []
if gender is not None:
for child in self.children_list:
if child.gender == gender:
result.append(child)
else:
result = self.children_list
return result
def is_direct_successor(self, other):
return other in self.children()

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

.....EE....
======================================================================
ERROR: test_has_no_brother (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-ask6k8/test.py", line 38, in test_has_no_brother
    self.assertEqual(self.first_son.get_brothers(), [])
  File "/tmp/d20130408-29081-ask6k8/solution.py", line 27, in get_brothers
    return self.get_siblings('M')
  File "/tmp/d20130408-29081-ask6k8/solution.py", line 24, in get_siblings
    return list(result)
TypeError: 'NoneType' object is not iterable

======================================================================
ERROR: test_has_no_sister (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-ask6k8/test.py", line 35, in test_has_no_sister
    self.assertEqual(self.first_daughter.get_sisters(), [])
  File "/tmp/d20130408-29081-ask6k8/solution.py", line 30, in get_sisters
    return self.get_siblings('F')
  File "/tmp/d20130408-29081-ask6k8/solution.py", line 24, in get_siblings
    return list(result)
TypeError: 'NoneType' object is not iterable

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

FAILED (errors=2)

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

Камелия обнови решението на 29.03.2013 12:08 (преди над 11 години)

+class Person:
+
+ def __init__(self, name, gender, birth_year,
+ mother=None, father=None):
+ self.name = name
+ self.gender = gender
+ self.birth_year = birth_year
+ self.mother = mother
+ self.father = father
+ self.children_list = []
+ if mother is not None:
+ mother.children_list.append(self)
+ if father is not None:
+ father.children_list.append(self)
+
+ def get_siblings(self, _gender):
+ result = {}
+ if self.mother is not None:
+ result = set(self.mother.children(gender=_gender))
+ if self.father is not None:
+ result = result.union(set(self.father.children(gender=_gender)))
+ if self in result:
+ result = result.remove(self)
+ return list(result)
+
+ def get_brothers(self):
+ return self.get_siblings('M')
+
+ def get_sisters(self):
+ return self.get_siblings('F')
+
+ def children(self, gender=None):
+ result = []
+ if gender is not None:
+ for child in self.children_list:
+ if child.gender == gender:
+ result.append(child)
+ else:
+ result = self.children_list
+ return result
+
+ def is_direct_successor(self, other):
+ return other in self.children()