Решение на Родословно дърво от Илия Тобов

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

Към профила на Илия Тобов

Резултати

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

Код

class Person:
def __init__(self, **kwargs):
for attribute in kwargs:
setattr(self, attribute, kwargs[attribute])
self.kids = []
if 'mother' in kwargs:
self.mother.kids.append(self)
else:
self.mother = None
if 'father' in kwargs:
self.father.kids.append(self)
else:
self.father = None
def children(self, **kwargs):
if kwargs:
return [child for child in self.kids
if child.gender == kwargs['gender']]
else:
return [child for child in self.kids]
def is_direct_successor(self, successor):
if successor.mother is self:
return True
if successor.father is self:
return True
return False
def get_sisters(self):
siblings = []
if self.mother:
from_mother = self.mother.children(gender='F')
siblings += from_mother
if self.father:
from_father = self.father.children(gender='F')
siblings += from_father
return [sister for sister in set(siblings)
if sister is not self]
def get_brothers(self):
siblings = []
if self.mother:
from_mother = self.mother.children(gender='M')
siblings += from_mother
if self.father:
from_father = self.father.children(gender='M')
siblings += from_father
return [brother for brother in set(siblings)
if brother is not self]

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

...........
----------------------------------------------------------------------
Ran 11 tests in 0.006s

OK

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

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

+class Person:
+ def __init__(self, **kwargs):
+ for attribute in kwargs:
+ setattr(self, attribute, kwargs[attribute])
+
+ self.kids = []
+
+ if 'mother' in kwargs:
+ self.mother.kids.append(self)
+
+ if 'father' in kwargs:
+ self.father.kids.append(self)
+
+ def children(self, **kwargs):
+ if kwargs:
+ return [child for child in self.kids
+ if child.gender == kwargs['gender']]
+ else:
+ return [child for child in self.kids]
+
+ def is_direct_successor(self, successor):
+ return successor.mother or successor.father is self
+
+ def get_sisters(self):
+ if self.mother:
+ from_mother = self.mother.children(gender='F')
+ if self.father:
+ from_father = self.father.children(gender='F')
+
+ return [sister for sister in set(from_father+from_mother)]
+
+ def get_brothers(self):
+ if self.mother:
+ from_mother = self.mother.children(gender='M')
+ if self.father:
+ from_father = self.father.children(gender='M')
+
+ return [brother for brother in set(from_father+from_mother)]

Damn, it! Открих малко проблеми, ама ще ги оправя утре : ) Само да питам, имената kwargs проблем ли са? Търсих в PEP8, дали когато имам някакъв клас вместо kwargs е по-подбре да използвам някоя по подходяща дума и не открих нищо по въпроса.

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

class Person:
def __init__(self, **kwargs):
for attribute in kwargs:
setattr(self, attribute, kwargs[attribute])
self.kids = []
if 'mother' in kwargs:
self.mother.kids.append(self)
+ else:
+ self.mother = None
if 'father' in kwargs:
self.father.kids.append(self)
+ else:
+ self.father = None
def children(self, **kwargs):
if kwargs:
return [child for child in self.kids
if child.gender == kwargs['gender']]
else:
return [child for child in self.kids]
def is_direct_successor(self, successor):
- return successor.mother or successor.father is self
+ if successor.mother is self:
+ return True
+ if successor.father is self:
+ return True
+ return False
+
def get_sisters(self):
+ siblings = []
if self.mother:
from_mother = self.mother.children(gender='F')
+ siblings += from_mother
if self.father:
from_father = self.father.children(gender='F')
+ siblings += from_father
- return [sister for sister in set(from_father+from_mother)]
+ return [sister for sister in set(siblings)
+ if sister is not self]
+
def get_brothers(self):
+ siblings = []
if self.mother:
from_mother = self.mother.children(gender='M')
+ siblings += from_mother
if self.father:
from_father = self.father.children(gender='M')
+ siblings += from_father
- return [brother for brother in set(from_father+from_mother)]
+ return [brother for brother in set(siblings)
+ if brother is not self]

Илия обнови решението на 30.03.2013 20:08 (преди над 11 години)

class Person:
def __init__(self, **kwargs):
for attribute in kwargs:
setattr(self, attribute, kwargs[attribute])
self.kids = []
if 'mother' in kwargs:
self.mother.kids.append(self)
else:
self.mother = None
if 'father' in kwargs:
self.father.kids.append(self)
else:
self.father = None
def children(self, **kwargs):
if kwargs:
return [child for child in self.kids
if child.gender == kwargs['gender']]
else:
return [child for child in self.kids]
def is_direct_successor(self, successor):
if successor.mother is self:
return True
if successor.father is self:
return True
return False
def get_sisters(self):
siblings = []
if self.mother:
from_mother = self.mother.children(gender='F')
siblings += from_mother
if self.father:
from_father = self.father.children(gender='F')
siblings += from_father
-
return [sister for sister in set(siblings)
if sister is not self]
def get_brothers(self):
siblings = []
if self.mother:
from_mother = self.mother.children(gender='M')
siblings += from_mother
if self.father:
from_father = self.father.children(gender='M')
siblings += from_father
return [brother for brother in set(siblings)
if brother is not self]