-
Notifications
You must be signed in to change notification settings - Fork 85
/
212.py
32 lines (23 loc) · 652 Bytes
/
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
32
"""
Problem:
Spreadsheets often use this alphabetical encoding for its columns: "A", "B", "C", ...,
"AA", "AB", ..., "ZZ", "AAA", "AAB", ....
Given a column number, return its alphabetical column id. For example, given 1, return
"A". Given 27, return "AA".
"""
def get_column_name(num: int) -> str:
result = ""
while num > 0:
result = chr(64 + (num % 26)) + result
num = num // 26
return result
if __name__ == "__main__":
print(get_column_name(1))
print(get_column_name(27))
print(get_column_name(30))
print(get_column_name(53))
"""
SPECS:
TIME COMPLEXITY: O(log(n))
SPACE COMPLEXITY: O(log(n))
"""