Skip to content

Commit a581d27

Browse files
committed
Add: Excel Sheet Column Number
1 parent f87fc5f commit a581d27

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
package excel_sheet_column_number
2+
3+
func titleToNumber(s string) int {
4+
ans := 0
5+
for _, c := range s {
6+
ans *= 26
7+
ans += int(c) - 'A' + 1
8+
}
9+
return ans
10+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
11
package excel_sheet_column_number
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input string
7+
expected int
8+
}
9+
10+
func TestTitleToNumber(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: "A",
14+
expected: 1,
15+
},
16+
{
17+
input: "C",
18+
expected: 3,
19+
},
20+
{
21+
input: "Z",
22+
expected: 26,
23+
},
24+
{
25+
input: "AA",
26+
expected: 27,
27+
},
28+
{
29+
input: "AB",
30+
expected: 28,
31+
},
32+
{
33+
input: "ZY",
34+
expected: 701,
35+
}, {
36+
input: "AAA",
37+
expected: 703,
38+
},
39+
{
40+
input: "XYZ",
41+
expected: 16900,
42+
},
43+
}
44+
for _, tc := range tests {
45+
output := titleToNumber(tc.input)
46+
if output != tc.expected {
47+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)