Решение на Родословно дърво от Цанислава Русева

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

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

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 7 успешни тест(а)
  • 4 неуспешни тест(а)

Код

people = []
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
people.append(self)
def __setattr__(self, name, value):
if (name in ["mother", "father"] and value != None and
value.birth_year > self.birth_year - 18):
raise AttributeError("{} is too young!".format(name))
super().__setattr__(name, value)
def __str__(self):
return "{}, {}, born in {}".format(self.name,
self.gender,
self.birth_year)
def get_siblings(self, gender='both'):
siblings = []
for person in people:
if ((person != self) and
(gender == 'both' or
gender == person.gender) and
((person.mother == self.mother) or
(person.father == person.father))):
siblings.append(person)
return siblings
def get_brothers(self):
return self.get_siblings('M')
def get_sisters(self):
return self.get_siblings('F')
def children(self, gender='both'):
children = []
for person in people:
if ((gender == 'both' or
gender == person.gender) and
((self == person.mother) or
(self == person.father))):
children.append(person)
return children
def successor(self):
for child in self.children():
yield child
for grandchild in child.successor():
yield grandchild
def is_direct_successor(self, other):
younger, older = ((self, other)
if self.birth_year > other.birth_year
else (other, self))
for person in older.successor():
if person == younger:
return True
return False

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

....FFFF...
======================================================================
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-asno2u/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 differing element 0:
Adam, M, born in 0
Kain, M, born in 20

First list contains 9 additional elements.
First extra element 1:
Kain, M, born in 20

- [<solution.Person object at 0xb786da8c>,
-  <solution.Person object at 0xb786db2c>,
-  <solution.Person object at 0xb786dd0c>,
-  <solution.Person object at 0xb786deec>,
-  <solution.Person object at 0xb786df6c>,
-  <solution.Person object at 0xb786dfac>,
-  <solution.Person object at 0xb78a014c>,
-  <solution.Person object at 0xb78a024c>,
-  <solution.Person object at 0xb78a034c>,
-  <solution.Person object at 0xb78a044c>]
? ^

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


======================================================================
FAIL: test_has_no_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-asno2u/test.py", line 38, in test_has_no_brother
    self.assertEqual(self.first_son.get_brothers(), [])
AssertionError: Lists differ: [<solution.Person object at 0x... != []

First list contains 11 additional elements.
First extra element 0:
Adam, M, born in 0

+ []
- [<solution.Person object at 0xb786da8c>,
-  <solution.Person object at 0xb786db2c>,
-  <solution.Person object at 0xb786dd0c>,
-  <solution.Person object at 0xb786deec>,
-  <solution.Person object at 0xb786df6c>,
-  <solution.Person object at 0xb786dfac>,
-  <solution.Person object at 0xb78a014c>,
-  <solution.Person object at 0xb78a024c>,
-  <solution.Person object at 0xb78a034c>,
-  <solution.Person object at 0xb78a044c>,
-  <solution.Person object at 0xb786d22c>]

======================================================================
FAIL: test_has_no_sister (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-asno2u/test.py", line 35, in test_has_no_sister
    self.assertEqual(self.first_daughter.get_sisters(), [])
AssertionError: Lists differ: [<solution.Person object at 0x... != []

First list contains 13 additional elements.
First extra element 0:
Eva, F, born in 0

+ []
- [<solution.Person object at 0xb786daec>,
-  <solution.Person object at 0xb786db6c>,
-  <solution.Person object at 0xb786ddcc>,
-  <solution.Person object at 0xb786df0c>,
-  <solution.Person object at 0xb786dfec>,
-  <solution.Person object at 0xb78a006c>,
-  <solution.Person object at 0xb78a020c>,
-  <solution.Person object at 0xb78a026c>,
-  <solution.Person object at 0xb78a040c>,
-  <solution.Person object at 0xb78a046c>,
-  <solution.Person object at 0xb786d2cc>,
-  <solution.Person object at 0xb786d16c>,
-  <solution.Person object at 0xb78a0e2c>]

======================================================================
FAIL: test_has_sister (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-asno2u/test.py", line 29, in test_has_sister
    self.assertEqual(self.first_son.get_sisters(), [self.first_daughter])
AssertionError: Lists differ: [<solution.Person object at 0x... != [<solution.Person object at 0x...

First differing element 0:
Eva, F, born in 0
Pepa, F, born in 22

First list contains 15 additional elements.
First extra element 1:
Pepa, F, born in 22

Diff is 741 characters long. Set self.maxDiff to None to see it.

----------------------------------------------------------------------
Ran 11 tests in 0.015s

FAILED (failures=4)

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

Цанислава обнови решението на 27.03.2013 00:32 (преди около 11 години)

+people = []
+
+
+class Person:
+ def __init__(self, name, birth_year, gender):
+ self.name = name
+ self.birth_year = birth_year
+ self.gender = gender
+ self.mother = None
+ self.father = None
+ people.append(self)
+
+ def __setattr__(self, name, value):
+ if name in ["mother", "father"] and value is not None and value.birth_year > self.birth_year - 18:
+ raise AttributeError("{} is too young to have a child that age!".format(name))
+ super().__setattr__(name, value)
+
+ def get_siblings(self, gender=all):
+ siblings = []
+ for person in people:
+ if (person is not self) and (gender == all or gender is person.gender) and ((person.mother is self.mother) or (person.father == person.father)):
+ siblings.append(person)
+ return siblings
+
+ def get_brothers(self):
+ return self.get_siblings('M')
+
+ def get_sisters(self):
+ return self.get_siblings('F')
+
+ def children(self, gender=all):
+ children = []
+ for person in people:
+ if (gender == all or gender == person.gender) and ((self is person.mother) or (self is person.father)):
+ children.append(person)
+ return children
+
+ def successor(self):
+ for child in self.children():
+ yield child
+ for grandchild in child.successor():
+ yield grandchild
+
+
+ def is_direct_successor(self, other):
+ younger, older = (self, other) if self.birth_year > other.birth_year else (other, self)
+ for person in older.successor():
+ if person is younger:
+ return True
+ return False

Цанислава обнови решението на 30.03.2013 12:15 (преди около 11 години)

people = []
class Person:
- def __init__(self, name, birth_year, gender):
+ def __init__(self, name, gender, birth_year, father=None,
+ mother=None):
self.name = name
- self.birth_year = birth_year
self.gender = gender
- self.mother = None
- self.father = None
+ self.birth_year = birth_year
+ self.father = father
+ self.mother = mother
people.append(self)
def __setattr__(self, name, value):
- if name in ["mother", "father"] and value is not None and value.birth_year > self.birth_year - 18:
- raise AttributeError("{} is too young to have a child that age!".format(name))
+ if (name in ["mother", "father"] and value != None and
+ value.birth_year > self.birth_year - 18):
+ raise AttributeError("{} is too young!".format(name))
super().__setattr__(name, value)
- def get_siblings(self, gender=all):
+ def __str__(self):
+ return "{}, {}, born in {}".format(self.name,
+ self.gender,
+ self.birth_year)
+
+ def get_siblings(self, gender='both'):
siblings = []
for person in people:
- if (person is not self) and (gender == all or gender is person.gender) and ((person.mother is self.mother) or (person.father == person.father)):
+ if ((person != self) and
+ (gender == 'both' or
+ gender == person.gender) and
+ ((person.mother == self.mother) or
+ (person.father == person.father))):
siblings.append(person)
return siblings
def get_brothers(self):
return self.get_siblings('M')
def get_sisters(self):
return self.get_siblings('F')
- def children(self, gender=all):
+ def children(self, gender='both'):
children = []
for person in people:
- if (gender == all or gender == person.gender) and ((self is person.mother) or (self is person.father)):
+ if ((gender == 'both' or
+ gender == person.gender) and
+ ((self == person.mother) or
+ (self == person.father))):
children.append(person)
return children
def successor(self):
for child in self.children():
yield child
for grandchild in child.successor():
yield grandchild
-
def is_direct_successor(self, other):
- younger, older = (self, other) if self.birth_year > other.birth_year else (other, self)
+ younger, older = ((self, other)
+ if self.birth_year > other.birth_year
+ else (other, self))
for person in older.successor():
- if person is younger:
+ if person == younger:
return True
return False