-
Notifications
You must be signed in to change notification settings - Fork 0
/
p10152.py
56 lines (47 loc) · 1.41 KB
/
p10152.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
56
import collections
# A turtle:
# rank: int
# name: str
# position: int
def climb_up(tower, index):
turtle = tower.pop(index)
tower.insert(0, turtle)
def shell_sort(tower: list):
""" Tower: list of Turtles"""
is_sorted = False
climb_history = []
while not is_sorted:
previous: dict = tower[0]
turtle_to_move = None
index = None
for i, t in enumerate(tower[1:], start=1):
if t["rank"] < previous["rank"]:
if not turtle_to_move or t["rank"] > turtle_to_move["rank"]:
turtle_to_move = t
index = i
else:
previous = t
if not turtle_to_move:
is_sorted = True # all elements in order
else:
climb_up(tower, index)
climb_history.append(tower[0]["name"])
return climb_history
test_cases = int(input())
for i in range(test_cases):
turtle_count = int(input())
turtle_original_order = []
name_lookup = {}
for x in range(turtle_count):
name = input()
turtle = {"name": name}
turtle_original_order.append(turtle)
name_lookup[name] = x
for rank in range(turtle_count):
name = input()
current_pos = name_lookup[name]
turtle_original_order[current_pos]["rank"] = rank
order = shell_sort(turtle_original_order)
for name in order:
print(name)
print()