forked from JaredLGillespie/CodinGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
order-of-succession.py
55 lines (38 loc) · 1.31 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# https://www.codingame.com/training/easy/order-of-succession
class Person:
def __init__(self, name, parent, birth, death, religion, gender):
self.name = name
self.parent = parent
self.birth = birth
self.death = death
self.religion = religion
self.gender = gender
@classmethod
def new(cls, text):
name, parent, birth, death, religion, gender = text.split()
if parent == '-': parent = None
if death == '-': death = None
return cls(name, parent, birth, death, religion, gender)
def person_sort_key(person):
return (
0 if person.gender == 'M' else 1, # Males come first
person.birth
)
def traverse(person, relations):
if person.death is None and person.religion != 'Catholic':
print(person.name)
if person.name not in relations: return
for p in sorted(relations[person.name], key=person_sort_key):
traverse(p, relations)
def solution():
num_people = int(input())
relations = {}
queen = None
for i in range(num_people):
person = Person.new(input())
if i == 0: queen = person
if person.parent not in relations:
relations[person.parent] = []
relations[person.parent].append(person)
traverse(queen, relations)
solution()