Решение на Родословно дърво от Филарета Йорданова

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

Към профила на Филарета Йорданова

Резултати

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

Код

from collections import defaultdict
class Person:
def __init__(self, name, birth_year, gender,
father=None, mother=None):
self.name = name
self.birth_year = birth_year
self.gender = gender
self.children_list = defaultdict(list)
self.father = father
if self.father:
self.father.add_child(self)
self.mother = mother
if self.mother:
self.mother.add_child(self)
def add_child(self, child):
self.children_list[child.gender].append(child)
def get_children(self, gender):
if self.mother and self.father:
children = self.mother.children_list[gender]
for child in self.father.children_list[gender]:
if child not in children:
children.append(child)
return [child for child in children if child != self]
elif self.father:
return [c for c in self.father.children_list[gender] if self != c]
elif self.mother:
return [c for c in self.mother.children_list[gender] if self != c]
else:
return []
def get_brothers(self):
return self.get_children('M')
def get_sisters(self):
return self.get_children('F')
def children(self, **gender):
if gender:
key = gender['gender']
return self.children_list[key]
else:
return self.children_list.values()
def __eq__(self, other):
person1 = [self.name, self.gender, self.birth_year]
parents1 = [self.mother, self.father]
person2 = [other.name, other.gender, other.birth_year]
parents2 = [other.mother, other.father]
return person1 == person2 and parents1 == parents2
def __ne__(self, other):
return not self.__eq__(other)
def is_direct_successor(self, other):
if self.children_list:
for people in self.children_list.values():
if other in people:
return True
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-17usmji/test.py", line 41, in test_children
    self.assertEqual(self.adam.children(), [self.first_son, self.first_daughter])
AssertionError: dict_values([[<solution.Person object at 0xb788df6c>], [<solution.Person object at 0xb788de0c>]]) != [<solution.Person object at 0xb788de0c>, <solution.Person object at 0xb788df6c>]

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

FAILED (failures=1)

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

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

+from collections import defaultdict
+
+
+class Person:
+ def __init__(self, name, birth_year, gender,
+ father=None, mother=None):
+ self.name = name
+ self.birth_year = birth_year
+ self.gender = gender
+ self.child = defaultdict(list)
+ self.father = father
+ if self.father:
+ self.father.add_child(self)
+ self.mother = mother
+ if self.mother:
+ self.mother.add_child(self)
+
+ def add_child(self, child):
+ self.child[child.gender].append(child)
+
+ def get_children(self, gender):
+ if self.mother and self.father:
+ children = self.mother.child[gender]
+ for child in self.father.child[gender]:
+ if child not in children:
+ children.append(child)
+ return [child for child in children if child != self]
+ elif self.father:
+ return [c for c in self.father.child[gender] if self != c]
+ elif self.mother:
+ return [c for c in self.mother.child[gender] if self != c]
+ else:
+ return []
+
+ def get_brothers(self):
+ return self.get_children('M')
+
+ def get_sisters(self):
+ return self.get_children('F')
+
+ def children(self, gender):
+ if gender:
+ return self.child[gender]
+ else:
+ return self.child.values()
+
+ def __eq__(self, other):
+ person1 = [self.name, self.gender, self.birth_year]
+ parents1 = [self.mother, self.father]
+ person2 = [other.name, other.gender, other.birth_year]
+ parents2 = [other.mother, other.father]
+ return person1 == person2 and parents1 == parents2
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def is_direct_successor(self, other):
+ if self.child:
+ for people in self.child.values():
+ if other in people:
+ return True
+ return False

Филарета обнови решението на 30.03.2013 01:07 (преди над 11 години)

from collections import defaultdict
class Person:
def __init__(self, name, birth_year, gender,
father=None, mother=None):
self.name = name
self.birth_year = birth_year
self.gender = gender
- self.child = defaultdict(list)
+ self.children_list = defaultdict(list)
self.father = father
if self.father:
self.father.add_child(self)
self.mother = mother
if self.mother:
self.mother.add_child(self)
def add_child(self, child):
- self.child[child.gender].append(child)
+ self.children_list[child.gender].append(child)
def get_children(self, gender):
if self.mother and self.father:
- children = self.mother.child[gender]
- for child in self.father.child[gender]:
+ children = self.mother.children_list[gender]
+ for child in self.father.children_list[gender]:
if child not in children:
children.append(child)
return [child for child in children if child != self]
elif self.father:
- return [c for c in self.father.child[gender] if self != c]
+ return [c for c in self.father.children_list[gender] if self != c]
elif self.mother:
- return [c for c in self.mother.child[gender] if self != c]
+ return [c for c in self.mother.children_list[gender] if self != c]
else:
return []
def get_brothers(self):
return self.get_children('M')
def get_sisters(self):
return self.get_children('F')
- def children(self, gender):
+ def children(self, **gender):
if gender:
- return self.child[gender]
+ key = gender['gender']
+ return self.children_list[key]
else:
- return self.child.values()
+ return self.children_list.values()
def __eq__(self, other):
person1 = [self.name, self.gender, self.birth_year]
parents1 = [self.mother, self.father]
person2 = [other.name, other.gender, other.birth_year]
parents2 = [other.mother, other.father]
return person1 == person2 and parents1 == parents2
def __ne__(self, other):
return not self.__eq__(other)
def is_direct_successor(self, other):
- if self.child:
- for people in self.child.values():
+ if self.children_list:
+ for people in self.children_list.values():
if other in people:
return True
- return False
+ return False