mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-30 16:48:39 +01:00
38: pandigital multiples
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
num := 0
|
||||||
|
for x := 9487; x >= 9234; x-- {
|
||||||
|
res := 100002 * x
|
||||||
|
if isPandigital(strconv.Itoa(res)) {
|
||||||
|
num = res
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println("num:", num)
|
||||||
|
}
|
||||||
|
|
||||||
|
func isPandigital(s string) bool {
|
||||||
|
var ss []string
|
||||||
|
for _, c := range s {
|
||||||
|
ss = append(ss, string(c))
|
||||||
|
}
|
||||||
|
sort.Strings(ss)
|
||||||
|
s = ""
|
||||||
|
for _, c := range ss {
|
||||||
|
s += c
|
||||||
|
}
|
||||||
|
return s == "123456789"
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Test_isPandigital(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"912345678",
|
||||||
|
args{"912345678"},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"12345678",
|
||||||
|
args{"12345678"},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"812349756",
|
||||||
|
args{"812349756"},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := isPandigital(tt.args.s); got != tt.want {
|
||||||
|
t.Errorf("isPandigital() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user