-
Notifications
You must be signed in to change notification settings - Fork 13
/
order-of-succession.py
31 lines (26 loc) · 993 Bytes
/
order-of-succession.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Sol:
def __init__(self, name, parent, birth, death, religion, gender):
self.name = name
self.parent = parent
self.birth = int(birth)
self.death = int(death) if death != '-' else None
self.religion = religion
self.isfemale = gender == 'F'
self.children = []
def addChild(self, child):
if child.parent == self.name: self.children += [child]
else:
for i in self.children: i.addChild(child)
def traverse(self):
succession = []
if not self.death and self.religion == 'Anglican': succession += [self]
sortedChildren = sorted(self.children, key=lambda child: (child.isfemale, child.birth))
for child in sortedChildren: succession += child.traverse()
return succession
n = int(input())
succ = Sol(*input().split())
for i in range(n - 1):
i = Sol(*input().split())
succ.addChild(i)
for i in succ.traverse():
print(i.name)