Skip to content

Commit 33992a6

Browse files
committed
convert to title
LeetCode
1 parent fc5f1ba commit 33992a6

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

excel_convert.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Learn Python together
2+
"""Given an integer columnNumber, return its corresponding
3+
column title as it appears in an Excel sheet."""
4+
# Solution
5+
def convert_to_title(colNum):
6+
result = ""
7+
while colNum > 0:
8+
# getting a zero-based index
9+
colNum -= 1
10+
# using divmod function to get quotient and remainder
11+
quotient, remainder = divmod(colNum, 26)
12+
# converting the remainder to a character using the
13+
# ASCII code and addin the result
14+
result = chr(remainder + 65) + result
15+
# setting index of the next wrap to colNum
16+
colNum = quotient
17+
return result
18+
19+
# check
20+
print(convert_to_title(2))
21+
print(convert_to_title(26))
22+
print(convert_to_title(-1))
23+
print(convert_to_title(101))
24+
print(convert_to_title(450))

fib_numbers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ def fibonacci2(n):
3737
print(fibonacci2(12)) # Output -> 144
3838
print(fibonacci2(-11)) # Output -> 0
3939
print(fibonacci2(6)) # Output -> 8
40+

0 commit comments

Comments
 (0)