-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path171_ExcelSheetColumnNumber.py
More file actions
105 lines (78 loc) · 2.61 KB
/
171_ExcelSheetColumnNumber.py
File metadata and controls
105 lines (78 loc) · 2.61 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
# By Will Shin
#
#-------------------------------------------------------------------------------
# LeetCode prompt
#-------------------------------------------------------------------------------
"""
Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Example 1:
Input: "A"
Output: 1
Example 2:
Input: "AB"
Output: 28
Example 3:
Input: "ZY"
Output: 701
"""
#-------------------------------------------------------------------------------
# Approach
#-------------------------------------------------------------------------------
"""
The first we would need is some sort of way to get the number from the letter.
That function is ord(), which gives the ASCII code for a letter, so that ord(A) is '65'
You can therefore get the index of the letter by doing
ord(letter) - ord('A') + 1
How do we add the second letter?
* we can keep adding +26 to the index every time we get a new letter?
* we also want to reverse the string so that we can go from right to left :
"""
#-------------------------------------------------------------------------------
# Soluton
#-------------------------------------------------------------------------------
def my_titleToNumber(string):
index = 0
string = string[::-1]
for exponent, letter in enumerate(string):
index += (ord(letter) - ord('A') + 1) * (26 ** exponent)
return index
#-------------------------------------------------------------------------------
# Main Leetcode Input Driver
#-------------------------------------------------------------------------------
class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
return my_titleToNumber(s)
#-------------------------------------------------------------------------------
# Unit Test
#-------------------------------------------------------------------------------
import unittest
class TestSolution(unittest.TestCase):
def test_A(self):
string = "A"
ans = 1
self.assertEqual(Solution().titleToNumber(string), ans)
def test_AB(self):
string = "AB"
ans = 28
self.assertEqual(Solution().titleToNumber(string), ans)
def test_ZY(self):
string = "ZY"
ans = 701
self.assertEqual(Solution().titleToNumber(string), ans)
unittest.main()