Решение на Родословно дърво от Николай Русев

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

Към профила на Николай Русев

Резултати

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

Код

from queue import Queue
class Person:
def __init__(self, name, gender, birth_year, father=None, mother=None):
self.name = name
self.gender = gender
self.birth_year = birth_year
self.father = father
self.mother = mother
self._children = []
if father is not None:
father._children.append(self)
if mother is not None:
mother._children.append(self)
def children(self, gender=''):
result = []
for child in self._children:
if child.gender == gender:
result.append(child)
return result
def _get_siblings(self, gender):
result = set()
if self.father is not None:
result |= set(self.father.children(gender))
if self.mother is not None:
result |= set(self.mother.children(gender))
return list(result - {self})
def get_brothers(self):
return self._get_siblings('M')
def get_sisters(self):
return self._get_siblings('F')
def is_direct_successor(self, other):
visited = set()
q = Queue(0)
q.put_nowait(other)
while q.qsize() > 0:
current = q.get_nowait()
if current in visited:
continue
visited.add(current)
if current is self:
return True
if current.father is not None:
q.put_nowait(current.father)
if current.mother is not None:
q.put_nowait(current.mother)
return False

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

.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-13mhb6u/test.py", line 41, in test_children
    self.assertEqual(self.adam.children(), [self.first_son, self.first_daughter])
AssertionError: Lists differ: [] != [<solution.Person object at 0x...

Second list contains 2 additional elements.
First extra element 0:
<solution.Person object at 0xb77dd8cc>

- []
+ [<solution.Person object at 0xb77dd8cc>,
+  <solution.Person object at 0xb77ddaac>]

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

FAILED (failures=1)

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

Николай обнови решението на 31.03.2013 23:07 (преди около 11 години)

+from queue import Queue
+
+
+class Person:
+ def __init__(self, name, gender, birth_year, father=None, mother=None):
+ self.name = name
+ self.gender = gender
+ self.birth_year = birth_year
+ self.father = father
+ self.mother = mother
+ self._children = []
+ if father is not None:
+ father._children.append(self)
+ if mother is not None:
+ mother._children.append(self)
+
+ def children(self, gender=''):
+ result = []
+ for child in self._children:
+ if child.gender == gender:
+ result.append(child)
+ return result
+
+ def _get_siblings(self, gender):
+ result = set()
+ if self.father is not None:
+ result |= set(self.father.children(gender))
+ if self.mother is not None:
+ result |= set(self.mother.children(gender))
+ return list(result - {self})
+
+ def get_brothers(self):
+ return self._get_siblings('M')
+
+ def get_sisters(self):
+ return self._get_siblings('F')
+
+ def is_direct_successor(self, other):
+ visited = set()
+ q = Queue(0)
+ q.put_nowait(other)
+ while q.qsize() > 0:
+ current = q.get_nowait()
+ if current in visited:
+ continue
+ visited.add(current)
+ if current is self:
+ return True
+ if current.father is not None:
+ q.put_nowait(current.father)
+ if current.mother is not None:
+ q.put_nowait(current.mother)
+ return False