mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 18:34:42 +01:00
24: lexicographic permutations
This commit is contained in:
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var src = "73167176531330624919225119674426574742355349194934" +
|
||||
@@ -36,8 +37,10 @@ func main() {
|
||||
nextSrc = append(nextSrc, i)
|
||||
src = src[1:]
|
||||
}
|
||||
t := time.Now()
|
||||
var next []int
|
||||
var out int
|
||||
var outS []int
|
||||
for {
|
||||
next, nextSrc = FetchNextAdjacent(13, nextSrc)
|
||||
if len(next) == 0 {
|
||||
@@ -50,10 +53,12 @@ func main() {
|
||||
}
|
||||
if product > out {
|
||||
out = product
|
||||
outS = next
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("the largest product is", out)
|
||||
fmt.Println("time:", time.Since(t))
|
||||
fmt.Println("the largest product is", out, outS)
|
||||
}
|
||||
|
||||
func FetchNextAdjacent(n int, src []int) ([]int, []int) {
|
||||
|
||||
42
24_lexicographic_permutations/main.go
Normal file
42
24_lexicographic_permutations/main.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
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)
|
||||
facs = append(facs, n)
|
||||
}
|
||||
fmt.Println("facs:", facs)
|
||||
|
||||
fmt.Println("the millionth is:", findNPermutation(1e6, nums, facs))
|
||||
}
|
||||
|
||||
func findNPermutation(n int, src []int, facs []int) string {
|
||||
n--
|
||||
var ret string
|
||||
for i := len(src) - 1; i >= 0; i-- {
|
||||
t := n / facs[i]
|
||||
n %= facs[i]
|
||||
ret += strconv.Itoa(src[t])
|
||||
src = append(src[:t], src[t+1:]...)
|
||||
}
|
||||
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)
|
||||
}
|
||||
p := 1
|
||||
for i := n - m + 1; i <= n; i++ {
|
||||
p *= i
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
109
24_lexicographic_permutations/main_test.go
Normal file
109
24_lexicographic_permutations/main_test.go
Normal file
@@ -0,0 +1,109 @@
|
||||
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
|
||||
}{
|
||||
{
|
||||
"1-2",
|
||||
args{1, 2},
|
||||
0,
|
||||
errors.New("m=2 cannot be larger than n=1"),
|
||||
},
|
||||
{
|
||||
"1-1",
|
||||
args{1, 1},
|
||||
1,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"3-3",
|
||||
args{3, 3},
|
||||
6,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"4-4",
|
||||
args{4, 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)
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Errorf("findPermutation() = %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_findNPermutation(t *testing.T) {
|
||||
type args struct {
|
||||
n int
|
||||
src []int
|
||||
facs []int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
"012-1",
|
||||
args{1, []int{0, 1, 2}, []int{1, 1, 2}},
|
||||
"012",
|
||||
},
|
||||
{
|
||||
"012-2",
|
||||
args{2, []int{0, 1, 2}, []int{1, 1, 2}},
|
||||
"021",
|
||||
},
|
||||
{
|
||||
"012-6",
|
||||
args{6, []int{0, 1, 2}, []int{1, 1, 2}},
|
||||
"210",
|
||||
},
|
||||
{
|
||||
"0123456-1",
|
||||
args{1, []int{0, 1, 2, 3, 4, 5, 6}, []int{1, 1, 2, 6, 24, 120, 720}},
|
||||
"0123456",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := findNPermutation(tt.args.n, tt.args.src, tt.args.facs); got != tt.want {
|
||||
t.Errorf("findNPermutation() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user