int input def gcd while and if else return letters sorted list QWERTYU

 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
t = int(input())
def gcd(u, v):
while u > 0 and v > 0:
if u > v:
u %= v
else:
v %= u
return u + v
letters = sorted(list('QWERTYUIOPASDFGHJKLZXCVBNM'))
for test_num in range(1, t + 1):
line1 = input()
d = list(map(int, input().split(' ')))
n = len(d)
r = [None] * (n + 1)
for i in range(n):
if d[i] != d[i + 1]:
c = gcd(d[i], d[i + 1])
r[i+1] = c
for j in range(i, -1, -1):
r[j] = d[j] // r[j+1]
for j in range(i+1, n+1):
r[j] = d[j-1] // r[j-1]
break
nums = sorted(list(set(r)))
code = {k: v for k, v in zip(nums, letters)}
out = [code[v] for v in r]
result = ''.join(out)
print("Case #{}: {}".format(test_num, result))