File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ))
Original file line number Diff line number Diff line change @@ -37,3 +37,4 @@ def fibonacci2(n):
3737print (fibonacci2 (12 )) # Output -> 144
3838print (fibonacci2 (- 11 )) # Output -> 0
3939print (fibonacci2 (6 )) # Output -> 8
40+
You can’t perform that action at this time.
0 commit comments