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 0602be0

Browse files
author
Shuo
authored
Merge pull request #468 from openset/develop
Add: Base 7
2 parents b3132ff + 8fb4089 commit 0602be0

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

‎problems/base-7/base_7.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
11
package base_7
2+
3+
func convertToBase7(num int) string {
4+
ans := ""
5+
if num < 0 {
6+
ans, num = "-", -num
7+
}
8+
if num >= 7 {
9+
ans += convertToBase7(num / 7)
10+
}
11+
return ans + string('0'+num%7)
12+
}

‎problems/base-7/base_7_test.go‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
11
package base_7
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input int
7+
expected string
8+
}
9+
10+
func TestConvertToBase7(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: 100,
14+
expected: "202",
15+
},
16+
{
17+
input: -7,
18+
expected: "-10",
19+
},
20+
{
21+
input: -1,
22+
expected: "-1",
23+
},
24+
{
25+
input: 0,
26+
expected: "0",
27+
},
28+
}
29+
for _, tc := range tests {
30+
output := convertToBase7(tc.input)
31+
if output != tc.expected {
32+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
33+
}
34+
}
35+
}

0 commit comments

Comments
(0)

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