Skip to content

Commit a087f61

Browse files
author
Shuo
authored
Merge pull request #727 from openset/develop
Add: Hexspeak
2 parents 5d6edbc + e6adb03 commit a087f61

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

problems/hexspeak/hexspeak.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package problem1271
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
func toHexspeak(num string) string {
10+
n, _ := strconv.Atoi(num)
11+
hex := fmt.Sprintf("%X", n)
12+
hex = strings.NewReplacer("0", "O", "1", "I").Replace(hex)
13+
for _, v := range hex {
14+
if v < 'A' {
15+
return "ERROR"
16+
}
17+
}
18+
return hex
19+
}

problems/hexspeak/hexspeak_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package problem1271
2+
3+
import "testing"
4+
5+
type testType struct {
6+
in string
7+
want string
8+
}
9+
10+
func TestToHexspeak(t *testing.T) {
11+
tests := [...]testType{
12+
{
13+
in: "257",
14+
want: "IOI",
15+
},
16+
{
17+
in: "3",
18+
want: "ERROR",
19+
},
20+
}
21+
for _, tt := range tests {
22+
got := toHexspeak(tt.in)
23+
if got != tt.want {
24+
t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want)
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)