From e6adb03ab5d6658e538c9274d235c2c3f0471f5b Mon Sep 17 00:00:00 2001 From: openset Date: Sun, 1 Dec 2019 10:11:41 +0800 Subject: [PATCH] Add: Hexspeak --- problems/hexspeak/hexspeak.go | 19 +++++++++++++++++++ problems/hexspeak/hexspeak_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 problems/hexspeak/hexspeak.go create mode 100644 problems/hexspeak/hexspeak_test.go diff --git a/problems/hexspeak/hexspeak.go b/problems/hexspeak/hexspeak.go new file mode 100644 index 000000000..9f81f7d0d --- /dev/null +++ b/problems/hexspeak/hexspeak.go @@ -0,0 +1,19 @@ +package problem1271 + +import ( + "fmt" + "strconv" + "strings" +) + +func toHexspeak(num string) string { + n, _ := strconv.Atoi(num) + hex := fmt.Sprintf("%X", n) + hex = strings.NewReplacer("0", "O", "1", "I").Replace(hex) + for _, v := range hex { + if v < 'A' { + return "ERROR" + } + } + return hex +} diff --git a/problems/hexspeak/hexspeak_test.go b/problems/hexspeak/hexspeak_test.go new file mode 100644 index 000000000..07bf6f44c --- /dev/null +++ b/problems/hexspeak/hexspeak_test.go @@ -0,0 +1,27 @@ +package problem1271 + +import "testing" + +type testType struct { + in string + want string +} + +func TestToHexspeak(t *testing.T) { + tests := [...]testType{ + { + in: "257", + want: "IOI", + }, + { + in: "3", + want: "ERROR", + }, + } + for _, tt := range tests { + got := toHexspeak(tt.in) + if got != tt.want { + t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want) + } + } +}