Skip to content

[Go] Challenge 13 (Unreviewed) #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions challenge_13/go/erocs/README.md
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions challenge_13/go/erocs/src/c13/c13.go
Original file line number Diff line number Diff line change
@@ -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
}
55 changes: 55 additions & 0 deletions challenge_13/go/erocs/src/c13/c13_test.go
Original file line number Diff line number Diff line change
@@ -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)
}