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

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

Към профила на Свилен Андонов

Резултати

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

Код

class Person:
def __init__(self, name, gender, birth_year, **kwargs):
self.name = name
self.gender = gender
self.birth_year = birth_year
self.current_children = set()
mother = kwargs.get('mother', None)
father = kwargs.get('father', None)
if father is not None:
father.current_children.add(self)
if mother is not None:
mother.current_children.add(self)
self.father = father
self.mother = mother
def get_all_clidren(self, gender):
all_children = []
if self.mother is not None:
all_children += (list(filter(lambda x: x.gender == gender
and x is not self,
self.mother.current_children)))
if self.father is not None:
all_children += (list(filter(lambda x: x.gender == gender
and x is not self
and not x in all_children,
self.father.current_children)))
return all_children
def is_direct_successor(self, other):
return other in self.current_children
def get_brothers(self):
return self.get_all_clidren('M')
def get_sisters(self):
return self.get_all_clidren('F')
def children(self, gender=None):
if gender is not None:
return list(filter(lambda x: x.gender == gender,
self.current_children))
else:
return self.current_children

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

.F.........
======================================================================
FAIL: test_children (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-pajjfp/test.py", line 41, in test_children
    self.assertEqual(self.adam.children(), [self.first_son, self.first_daughter])
AssertionError: {<solution.Person object at 0xb78afd4c>, <solution.Person object at 0xb78afe6c>} != [<solution.Person object at 0xb78afd4c>, <solution.Person object at 0xb78afe6c>]

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

FAILED (failures=1)

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

Свилен обнови решението на 29.03.2013 23:25 (преди над 11 години)

+class Person:
+
+ def __init__(self, name, gender, birth_year, **kwargs):
+ self.name = name
+ self.gender = gender
+ self.birth_year = birth_year
+ self.current_children = set()
+
+ mother = kwargs.get('mother', None)
+ father = kwargs.get('father', None)
+
+ if father is not None:
+ father.current_children.add(self)
+ if mother is not None:
+ mother.current_children.add(self)
+
+ self.father = father
+ self.mother = mother
+
+ def get_all_clidren(self, gender):
+ all_children = []
+ if self.mother is not None:
+ all_children += (list(filter(lambda x: x.gender == gender
+ and x is not self,
+ self.mother.current_children)))
+ if self.father is not None:
+ all_children += (list(filter(lambda x: x.gender == gender
+ and x is not self
+ and not x in all_children,
+ self.father.current_children)))
+ return all_children
+
+ def is_direct_successor(self, other):
+ return other in self.current_children
+
+ def get_brothers(self):
+ return self.get_all_clidren('M')
+
+ def get_sisters(self):
+ return self.get_all_clidren('F')
+
+ def children(self, gender=None):
+ if gender is not None:
+ return list(filter(lambda x: x.gender == gender,
+ self.current_children))
+ else:
+ return self.current_children