mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 19:54:43 +01:00
55: lychrel numbers
This commit is contained in:
24
55_lychrel_numbers/main.go
Normal file
24
55_lychrel_numbers/main.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/big"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func isLychrelNumber(n int) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsPalindrome(n *big.Int) bool {
|
||||||
|
str := n.String()
|
||||||
|
pb := strings.Builder{}
|
||||||
|
for i := len(str); i > 0; i-- {
|
||||||
|
pb.WriteString(str[i-1 : i])
|
||||||
|
}
|
||||||
|
pstr := pb.String()
|
||||||
|
return str == pstr
|
||||||
|
}
|
||||||
66
55_lychrel_numbers/main_test.go
Normal file
66
55_lychrel_numbers/main_test.go
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/big"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIsPalindrome(t *testing.T) {
|
||||||
|
fn, _ := (&big.Int{}).SetString("10203040506070809", 0)
|
||||||
|
fn2, _ := (&big.Int{}).SetString("1234567890987654321", 0)
|
||||||
|
type args struct {
|
||||||
|
n *big.Int
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"test1",
|
||||||
|
args{fn},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test2",
|
||||||
|
args{fn2},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := IsPalindrome(tt.args.n); got != tt.want {
|
||||||
|
t.Errorf("IsPalindrome() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_isLychrelNumber(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
n int
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"test1",
|
||||||
|
args{47},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test1",
|
||||||
|
args{4994},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := isLychrelNumber(tt.args.n); got != tt.want {
|
||||||
|
t.Errorf("isLychrelNumber() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user