Решение на Родословно дърво от Марина Узунова

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

Към профила на Марина Узунова

Резултати

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

Код

class Person:
def __init__(self, name, birth_year, gender, mother=None, father=None):
self.name = name
self.birth_year = birth_year
self.gender = gender
self.kids = []
self.mother = mother
self.father = father
if self.mother:
self.mother.kids.append(self)
if self.father:
self.father.kids.append(self)
def children(self, **kwargs):
if kwargs:
child = []
for x in self.kids:
if x.gender == kwargs['gender']:
child.append(x)
return child
else:
return self.kids
def get_brothers(self):
mothers_kids = []
fathers_kids = []
if self.mother:
mothers_kids = list({child for child in self.mother.children()
if (child.gender == 'M' or child.gender == 'm')
and child is not self})
if self.father:
fathers_kids = list({child for child in self.father.children()
if (child.gender == 'M' or child.gender == 'm')
and child is not self})
return mothers_kids + fathers_kids
def get_sisters(self):
mothers_kids = []
fathers_kids = []
if self.mother:
mothers_kids = list({child for child in self.mother.children()
if (child.gender == 'F' or child.gender == 'f')
and child is not self})
if self.father:
fathers_kids = list({child for child in self.children()
if (child.gender == 'F' or child.gender == 'f')
and child is not self})
return mothers_kids + fathers_kids
def is_direct_successor(self, successor):
if successor in self.kids:
return True
else:
return False

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

....F......
======================================================================
FAIL: test_has_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-py7y45/test.py", line 32, in test_has_brother
    self.assertEqual(self.first_daughter.get_brothers(), [self.first_son])
AssertionError: Lists differ: [<solution.Person object at 0x... != [<solution.Person object at 0x...

First list contains 1 additional elements.
First extra element 1:
<solution.Person object at 0xb781346c>

- [<solution.Person object at 0xb781346c>,
?                                        ^

+ [<solution.Person object at 0xb781346c>]
?                                        ^

-  <solution.Person object at 0xb781346c>]

----------------------------------------------------------------------
Ran 11 tests in 0.011s

FAILED (failures=1)

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

Марина обнови решението на 01.04.2013 01:52 (преди над 11 години)

+class Person:
+ def __init__(self, name, birth_year, gender, mother=None, father=None):
+ self.name = name
+ self.birth_year = birth_year
+ self.gender = gender
+ self.kids = []
+ self.mother = mother
+ self.father = father
+
+ if self.mother:
+ self.mother.kids.append(self)
+
+ if self.father:
+ self.father.kids.append(self)
+
+ def children(self, **kwargs):
+ if kwargs:
+ child = []
+ for x in self.kids:
+ if x.gender == kwargs['gender']:
+ child.append(x)
+ return child
+
+ else:
+ return self.kids
+
+ def get_brothers(self):
+ mothers_kids = []
+ fathers_kids = []
+
+ if self.mother:
+ mothers_kids = list({child for child in self.mother.children()
+ if (child.gender == 'M' or child.gender == 'm')
+ and child is not self})
+
+ if self.father:
+ fathers_kids = list({child for child in self.father.children()
+ if (child.gender == 'M' or child.gender == 'm')
+ and child is not self})
+
+ return mothers_kids + fathers_kids
+
+ def get_sisters(self):
+ mothers_kids = []
+ fathers_kids = []
+
+ if self.mother:
+ mothers_kids = list({child for child in self.mother.children()
+ if (child.gender == 'F' or child.gender == 'f')
+ and child is not self})
+
+ if self.father:
+ fathers_kids = list({child for child in self.children()
+ if (child.gender == 'F' or child.gender == 'f')
+ and child is not self})
+
+ return mothers_kids + fathers_kids
+
+ def is_direct_successor(self, successor):
+
+ if successor in self.kids:
+ return True
+
+ else:
+ return False

Марина обнови решението на 01.04.2013 01:53 (преди над 11 години)

class Person:
def __init__(self, name, birth_year, gender, mother=None, father=None):
self.name = name
self.birth_year = birth_year
self.gender = gender
self.kids = []
self.mother = mother
self.father = father
if self.mother:
self.mother.kids.append(self)
if self.father:
self.father.kids.append(self)
def children(self, **kwargs):
if kwargs:
child = []
for x in self.kids:
if x.gender == kwargs['gender']:
child.append(x)
return child
else:
return self.kids
def get_brothers(self):
mothers_kids = []
fathers_kids = []
if self.mother:
mothers_kids = list({child for child in self.mother.children()
if (child.gender == 'M' or child.gender == 'm')
and child is not self})
if self.father:
fathers_kids = list({child for child in self.father.children()
if (child.gender == 'M' or child.gender == 'm')
and child is not self})
return mothers_kids + fathers_kids
def get_sisters(self):
mothers_kids = []
fathers_kids = []
if self.mother:
mothers_kids = list({child for child in self.mother.children()
if (child.gender == 'F' or child.gender == 'f')
and child is not self})
if self.father:
fathers_kids = list({child for child in self.children()
if (child.gender == 'F' or child.gender == 'f')
and child is not self})
return mothers_kids + fathers_kids
def is_direct_successor(self, successor):
if successor in self.kids:
return True
else:
return False
+