Skip to content

Commit df85dd0

Browse files
author
Shuo
authored
Merge pull request #398 from openset/develop
Add: Excel Sheet Column Title
2 parents 6e23a3f + d35c46b commit df85dd0

File tree

2 files changed

+61
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)