From ab8249dbc6c46bf85ad20b4ad5eb603482c06ecd Mon Sep 17 00:00:00 2001 From: Erocs Date: Fri, 20 Jan 2017 13:10:58 -0800 Subject: [PATCH] [Go] Challenge 13 (Unreviewed) --- challenge_13/go/erocs/README.md | 12 +++++ challenge_13/go/erocs/src/c13/c13.go | 24 ++++++++++ challenge_13/go/erocs/src/c13/c13_test.go | 55 +++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 challenge_13/go/erocs/README.md create mode 100644 challenge_13/go/erocs/src/c13/c13.go create mode 100644 challenge_13/go/erocs/src/c13/c13_test.go diff --git a/challenge_13/go/erocs/README.md b/challenge_13/go/erocs/README.md new file mode 100644 index 000000000..25f54b71e --- /dev/null +++ b/challenge_13/go/erocs/README.md @@ -0,0 +1,12 @@ +# Challenge 13: Integer Palindrome + +## 1. Solution Description + +Divide the given value by 10 until it is 0 to extract all digits and store those into a slice. Then just check that each element of the first half of the slice is mirrored on the other half. + +## 2. Running Tests + +In bash in this directory: + + export GOPATH=`pwd` + go test c13 diff --git a/challenge_13/go/erocs/src/c13/c13.go b/challenge_13/go/erocs/src/c13/c13.go new file mode 100644 index 000000000..5a353ff81 --- /dev/null +++ b/challenge_13/go/erocs/src/c13/c13.go @@ -0,0 +1,24 @@ +package c13 + +func splitDigits(n uint64, base uint64) []byte { + out := make([]byte, 0, 22) // 22 is max length base-8 represented 64-bit value + for n > 0 { + r := n % base + n = n / base + out = append(out, byte(r)) + } + return out +} + +func IsPalindrome64(n uint64) bool { + ds := splitDigits(n, 10) + ll := len(ds) + hl := ll / 2 + ll-- + for i := 0; i < hl; i++ { + if ds[i] != ds[ll-i] { + return false + } + } + return true +} diff --git a/challenge_13/go/erocs/src/c13/c13_test.go b/challenge_13/go/erocs/src/c13/c13_test.go new file mode 100644 index 000000000..64d9b8c27 --- /dev/null +++ b/challenge_13/go/erocs/src/c13/c13_test.go @@ -0,0 +1,55 @@ +package c13 + +import ( + "testing" +) + +func CheckGood(t *testing.T, n uint64) { + if !IsPalindrome64(n) { + t.Fail() + } +} + +func CheckBad(t *testing.T, n uint64) { + if IsPalindrome64(n) { + t.Fail() + } +} + +func TestGL1(t *testing.T) { + CheckGood(t, 1) +} + +func TestGL2(t *testing.T) { + CheckGood(t, 11) +} + +func TestGL3(t *testing.T) { + CheckGood(t, 121) +} + +func TestGL4(t *testing.T) { + CheckGood(t, 1221) +} + +func TestBL2(t *testing.T) { + CheckBad(t, 12) +} + +func TestBL3(t *testing.T) { + CheckBad(t, 112) +} + +func TestBL4(t *testing.T) { + CheckBad(t, 1112) + CheckBad(t, 1121) +} + +func TestGMax(t *testing.T) { + CheckGood(t, 11111111111111111111) +} + +func TestBMax(t *testing.T) { + CheckBad(t, 11111111111111111112) + CheckBad(t, 11111111112111111111) +}