Решение на Родословно дърво от Десислава Петрова

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

Към профила на Десислава Петрова

Резултати

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

Код

class Person(object):
def __init__(self, name, birth_year, gender, **keywords):
self._siblings_observers = []
self.successors = set()
self.name = name
self.birth_year = birth_year
self.gender = gender
self.father = None
self.mother = None
for kw in keywords.keys():
if kw == 'mother':
self.mother = keywords[kw]
self._siblings_observers.append(keywords[kw].children)
keywords[kw].successors.add(self)
elif kw == 'father':
self.father = keywords[kw]
self._siblings_observers.append(keywords[kw].children)
keywords[kw].successors.add(self)
def get_brothers(self):
brothers = set()
for observer in self._siblings_observers:
brothers.update(observer("M"))
brothers.discard(self)
return list(brothers)
def get_sisters(self):
sisters = set()
for observer in self._siblings_observers:
sisters.update(observer("F"))
sisters.discard(self)
return list(sisters)
def children(self, gender="both"):
if gender == "both":
return self.successors
else:
return list(filter(lambda child: child.gender == gender,
list(self.successors)))
def is_direct_successor(self, other):
return other in self.successors or self in other.successors

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

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

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

FAILED (failures=1)

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

Десислава обнови решението на 31.03.2013 13:46 (преди над 11 години)

+class Person(object):
+
+ def __init__(self, name, birth_year, gender, **keywords):
+ self._siblings_observers = []
+ self.successors = set()
+ self.name = name
+ self.birth_year = birth_year
+ self.gender = gender
+ self.father = None
+ self.mother = None
+ for kw in keywords.keys():
+ if kw == 'mother':
+ self.mother = keywords[kw]
+ self._siblings_observers.append(keywords[kw].children)
+ keywords[kw].successors.add(self)
+ elif kw == 'father':
+ self.father = keywords[kw]
+ self._siblings_observers.append(keywords[kw].children)
+ keywords[kw].successors.add(self)
+
+ def get_brothers(self):
+ brothers = set()
+ for observer in self._siblings_observers:
+ brothers.update(observer("M"))
+ brothers.discard(self)
+ return list(brothers)
+
+ def get_sisters(self):
+ sisters = set()
+ for observer in self._siblings_observers:
+ sisters.update(observer("F"))
+ sisters.discard(self)
+ return list(sisters)
+
+ def children(self, gender="both"):
+ if gender == "both":
+ return self.successors
+ else:
+ return list(filter(lambda child: child.gender == gender,
+ list(self.successors)))
+
+ def is_direct_successor(self, other):
+ return other in self.successors or self in other.successors