forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_212.py
31 lines (25 loc) · 804 Bytes
/
problem_212.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
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def get_alpha_encoding(num):
num_chars = 1
min_range, max_range = 1, 26
while num > max_range:
num_chars += 1
min_range = max_range
max_range += len(alphabet) ** num_chars
chars = list()
for _ in range(num_chars):
interval = ((max_range - min_range + 1) // len(alphabet))
char_pos = 0
prev, curr = min_range, min_range + interval
while num >= curr:
char_pos += 1
prev = curr
curr = prev + interval
chars.append(alphabet[char_pos])
num -= prev
min_range, max_range = prev, curr
return "".join(chars)
# Tests
assert get_alpha_encoding(1) == "A"
assert get_alpha_encoding(20) == "T"
assert get_alpha_encoding(27) == "AA"