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

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

Към профила на Станислав Гатев

Резултати

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

Код

class Person():
class Filters():
@staticmethod
def gender(gender=None):
return lambda person: gender in (person.gender, None)
ATTRIBUTES = ('gender', 'name', 'birth_year')
PARENT_TITLES = ('father', 'mother')
(FEMALE, MALE) = ('F', 'M')
(FATHER_TITLE, MOTHER_TITLE) = PARENT_TITLES
def __init__(self, **kwargs):
self.__children = set()
self.__parents = dict()
for attribute in self.ATTRIBUTES:
if attribute in kwargs:
self.__setattr__(attribute, kwargs[attribute])
for parent_title in self.PARENT_TITLES:
if parent_title in kwargs:
self.__set_parent(parent_title, kwargs[parent_title])
def mother(self):
return self.__get_parent(self.MOTHER_TITLE)
def father(self):
return self.__get_parent(self.FATHER_TITLE)
def children(self, gender=None):
return list(filter(Person.Filters.gender(gender), self.__children))
def get_siblings(self, gender=None):
siblings = {child for parent in self.__parents.values()
for child in parent.children() if child is not self}
return list(filter(Person.Filters.gender(gender), siblings))
def get_sisters(self):
return self.get_siblings(gender=self.FEMALE)
def get_brothers(self):
return self.get_siblings(gender=self.MALE)
def add_child(self, child):
self.__children.add(child)
def is_direct_successor(self, person):
return person in self.children()
def __get_parent(self, parent_title):
if parent_title in self.__parents:
return self.__parents[parent_title]
def __set_parent(self, parent_title, parent):
self.__parents[parent_title] = parent
parent.add_child(self)

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

.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-1k1e6sp/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... != [<solution.Person object at 0x...

First differing element 0:
<solution.Person object at 0xb77eb40c>
<solution.Person object at 0xb77eb3cc>

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

+ [<solution.Person object at 0xb77eb3cc>,
?                                    ^^

-  <solution.Person object at 0xb77eb3cc>]
?                                    ^^

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


----------------------------------------------------------------------
Ran 11 tests in 0.010s

FAILED (failures=1)

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

Станислав обнови решението на 30.03.2013 19:06 (преди над 11 години)

+class Person():
+ def __init__(self, **kwargs):
+ for arg in ('gender', 'name', 'birth_year'):
+ if arg in kwargs:
+ self.__setattr__(arg, kwargs[arg])
+ self.__children = set()
+ self.__parents = {parent: kwargs[parent]
+ for parent in ('father', 'mother')
+ if parent in kwargs}
+ for parent in self.__parents.values():
+ parent.add_child(self)
+
+ def children(self, gender=None):
+ return [child for child in self.__children
+ if gender in (child.gender, None)]
+
+ def get_siblings(self, gender=None):
+ return list({child for parent in self.__parents.values()
+ for child in parent.children()
+ if child is not self and gender in (child.gender, None)})
+
+ def get_sisters(self):
+ return self.get_siblings(gender='F')
+
+ def get_brothers(self):
+ return self.get_siblings(gender='M')
+
+ def add_child(self, child):
+ self.__children.add(child)
+
+ def is_direct_successor(self, person):
+ return person in self.children()

Станислав обнови решението на 30.03.2013 21:10 (преди над 11 години)

class Person():
+ class Filters():
+ @staticmethod
+ def by_gender(gender):
+ return lambda person: gender in (person.gender, None)
+
def __init__(self, **kwargs):
for arg in ('gender', 'name', 'birth_year'):
if arg in kwargs:
self.__setattr__(arg, kwargs[arg])
self.__children = set()
self.__parents = {parent: kwargs[parent]
for parent in ('father', 'mother')
if parent in kwargs}
for parent in self.__parents.values():
parent.add_child(self)
def children(self, gender=None):
- return [child for child in self.__children
- if gender in (child.gender, None)]
+ return list(filter(Person.Filters.by_gender(gender), self.__children))
def get_siblings(self, gender=None):
- return list({child for parent in self.__parents.values()
- for child in parent.children()
- if child is not self and gender in (child.gender, None)})
+ siblings = {child for parent in self.__parents.values()
+ for child in parent.children() if child is not self}
+ return list(filter(Person.Filters.by_gender(gender), siblings))
def get_sisters(self):
return self.get_siblings(gender='F')
def get_brothers(self):
return self.get_siblings(gender='M')
def add_child(self, child):
self.__children.add(child)
def is_direct_successor(self, person):
return person in self.children()

Станислав обнови решението на 30.03.2013 21:55 (преди над 11 години)

class Person():
class Filters():
@staticmethod
- def by_gender(gender):
+ def gender(gender=None):
return lambda person: gender in (person.gender, None)
def __init__(self, **kwargs):
- for arg in ('gender', 'name', 'birth_year'):
- if arg in kwargs:
- self.__setattr__(arg, kwargs[arg])
self.__children = set()
- self.__parents = {parent: kwargs[parent]
- for parent in ('father', 'mother')
- if parent in kwargs}
- for parent in self.__parents.values():
- parent.add_child(self)
+ self.__parents = dict()
+ for attribute in ('gender', 'name', 'birth_year'):
+ if attribute in kwargs:
+ self.__setattr__(attribute, kwargs[attribute])
+
+ for parent_title in ('father', 'mother'):
+ if parent_title in kwargs:
+ self.__set_parent(parent_title, kwargs[parent_title])
+
+ def mother(self):
+ return self.__get_parent('mother')
+
+ def father(self):
+ return self.__get_parent('father')
+
def children(self, gender=None):
- return list(filter(Person.Filters.by_gender(gender), self.__children))
+ return list(filter(Person.Filters.gender(gender), self.__children))
def get_siblings(self, gender=None):
siblings = {child for parent in self.__parents.values()
for child in parent.children() if child is not self}
- return list(filter(Person.Filters.by_gender(gender), siblings))
+ return list(filter(Person.Filters.gender(gender), siblings))
+
def get_sisters(self):
return self.get_siblings(gender='F')
def get_brothers(self):
return self.get_siblings(gender='M')
def add_child(self, child):
self.__children.add(child)
def is_direct_successor(self, person):
return person in self.children()
+
+ def __get_parent(self, parent_title):
+ if parent_title in self.__parents:
+ return self.__parents[parent_title]
+
+ def __set_parent(self, parent_title, parent):
+ self.__parents[parent_title] = parent
+ parent.add_child(self)

Станислав обнови решението на 30.03.2013 22:13 (преди над 11 години)

class Person():
class Filters():
@staticmethod
def gender(gender=None):
return lambda person: gender in (person.gender, None)
+ FEMALE = 'F'
+ MALE = 'M'
+ ATTRIBUTES = ('gender', 'name', 'birth_year')
+ PARENT_TITLES = ('father', 'mother')
+ (FATHER_TITLE, MOTHER_TITLE) = PARENT_TITLES
+
def __init__(self, **kwargs):
self.__children = set()
self.__parents = dict()
- for attribute in ('gender', 'name', 'birth_year'):
+ for attribute in self.ATTRIBUTES:
if attribute in kwargs:
self.__setattr__(attribute, kwargs[attribute])
- for parent_title in ('father', 'mother'):
+ for parent_title in self.PARENT_TITLES:
if parent_title in kwargs:
self.__set_parent(parent_title, kwargs[parent_title])
def mother(self):
- return self.__get_parent('mother')
+ return self.__get_parent(self.MOTHER_TITLE)
def father(self):
- return self.__get_parent('father')
+ return self.__get_parent(self.FATHER_TITLE)
def children(self, gender=None):
return list(filter(Person.Filters.gender(gender), self.__children))
def get_siblings(self, gender=None):
siblings = {child for parent in self.__parents.values()
for child in parent.children() if child is not self}
return list(filter(Person.Filters.gender(gender), siblings))
def get_sisters(self):
- return self.get_siblings(gender='F')
+ return self.get_siblings(gender=self.FEMALE)
def get_brothers(self):
- return self.get_siblings(gender='M')
+ return self.get_siblings(gender=self.MALE)
def add_child(self, child):
self.__children.add(child)
def is_direct_successor(self, person):
return person in self.children()
def __get_parent(self, parent_title):
if parent_title in self.__parents:
return self.__parents[parent_title]
def __set_parent(self, parent_title, parent):
self.__parents[parent_title] = parent
parent.add_child(self)

Станислав обнови решението на 30.03.2013 22:16 (преди над 11 години)

class Person():
class Filters():
@staticmethod
def gender(gender=None):
return lambda person: gender in (person.gender, None)
- FEMALE = 'F'
- MALE = 'M'
ATTRIBUTES = ('gender', 'name', 'birth_year')
PARENT_TITLES = ('father', 'mother')
+ (FEMALE, MALE) = ('F', 'M')
(FATHER_TITLE, MOTHER_TITLE) = PARENT_TITLES
def __init__(self, **kwargs):
self.__children = set()
self.__parents = dict()
for attribute in self.ATTRIBUTES:
if attribute in kwargs:
self.__setattr__(attribute, kwargs[attribute])
for parent_title in self.PARENT_TITLES:
if parent_title in kwargs:
self.__set_parent(parent_title, kwargs[parent_title])
def mother(self):
return self.__get_parent(self.MOTHER_TITLE)
def father(self):
return self.__get_parent(self.FATHER_TITLE)
def children(self, gender=None):
return list(filter(Person.Filters.gender(gender), self.__children))
def get_siblings(self, gender=None):
siblings = {child for parent in self.__parents.values()
for child in parent.children() if child is not self}
return list(filter(Person.Filters.gender(gender), siblings))
def get_sisters(self):
return self.get_siblings(gender=self.FEMALE)
def get_brothers(self):
return self.get_siblings(gender=self.MALE)
def add_child(self, child):
self.__children.add(child)
def is_direct_successor(self, person):
return person in self.children()
def __get_parent(self, parent_title):
if parent_title in self.__parents:
return self.__parents[parent_title]
def __set_parent(self, parent_title, parent):
self.__parents[parent_title] = parent
parent.add_child(self)