Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d35c46b

Browse files
committed
Add: Excel Sheet Column Title
1 parent a581d27 commit d35c46b

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 7 additions & 0 deletions
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+
}
Lines changed: 54 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /