mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 18:34:42 +01:00
24: pandigital products
This commit is contained in:
@@ -10,7 +10,7 @@ var nums = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
func main() {
|
||||
var facs []int
|
||||
for _, i := range nums {
|
||||
n, _ := findPermutation(i, i)
|
||||
n := findFacs(i)
|
||||
facs = append(facs, n)
|
||||
}
|
||||
fmt.Println("facs:", facs)
|
||||
@@ -30,13 +30,10 @@ func findNPermutation(n int, src []int, facs []int) string {
|
||||
return ret
|
||||
}
|
||||
|
||||
func findPermutation(n, m int) (int, error) {
|
||||
if m > n {
|
||||
return 0, fmt.Errorf("m=%d cannot be larger than n=%d", m, n)
|
||||
}
|
||||
func findFacs(n int) int {
|
||||
p := 1
|
||||
for i := n - m + 1; i <= n; i++ {
|
||||
for i := 1; i <= n; i++ {
|
||||
p *= i
|
||||
}
|
||||
return p, nil
|
||||
return p
|
||||
}
|
||||
|
||||
@@ -1,67 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_findPermutation(t *testing.T) {
|
||||
type args struct {
|
||||
n int
|
||||
m int
|
||||
}
|
||||
tt := []struct {
|
||||
name string
|
||||
args args
|
||||
want int
|
||||
wantErr error
|
||||
name string
|
||||
args args
|
||||
want int
|
||||
}{
|
||||
{
|
||||
"1-2",
|
||||
args{1, 2},
|
||||
0,
|
||||
errors.New("m=2 cannot be larger than n=1"),
|
||||
},
|
||||
{
|
||||
"1-1",
|
||||
args{1, 1},
|
||||
"1",
|
||||
args{1},
|
||||
1,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"3-3",
|
||||
args{3, 3},
|
||||
"2",
|
||||
args{1},
|
||||
2,
|
||||
},
|
||||
{
|
||||
"3",
|
||||
args{3},
|
||||
6,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"4-4",
|
||||
args{4, 4},
|
||||
"4",
|
||||
args{4},
|
||||
24,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"3-2",
|
||||
args{3, 2},
|
||||
6,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"4-2",
|
||||
args{4, 2},
|
||||
12,
|
||||
nil,
|
||||
},
|
||||
}
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, err := findPermutation(tc.args.n, tc.args.m)
|
||||
if !reflect.DeepEqual(err, tc.wantErr) {
|
||||
t.Errorf("findPermutation() = %v, want err %v", err, tc.wantErr)
|
||||
}
|
||||
got := findFacs(tc.args.n)
|
||||
if got != tc.want {
|
||||
t.Errorf("findPermutation() = %v, want %v", got, tc.want)
|
||||
t.Errorf("findFacs() = %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
40
32_pandigital_products/main.go
Normal file
40
32_pandigital_products/main.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
sum := 0
|
||||
for i := 0; i < 10000; i++ {
|
||||
if hasPandigitalProduct(i) {
|
||||
sum += i
|
||||
}
|
||||
}
|
||||
fmt.Println("sum:", sum)
|
||||
}
|
||||
|
||||
func hasPandigitalProduct(n int) bool {
|
||||
for i := 1; i <= n; i++ {
|
||||
for n%i == 0 && isPandigital(strconv.Itoa(n)+strconv.Itoa(i)+strconv.Itoa(n/i)) {
|
||||
fmt.Printf("Pandigital:%d = %d * %d\n", n, i, n/i)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
63
32_pandigital_products/main_test.go
Normal file
63
32_pandigital_products/main_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_hasPandigitalProduct(t *testing.T) {
|
||||
type args struct {
|
||||
n int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
"7254",
|
||||
args{7254},
|
||||
true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := hasPandigitalProduct(tt.args.n); got != tt.want {
|
||||
t.Errorf("hasPandigitalProduct() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user