-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (123 loc) · 3.49 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"sort"
"strings"
"time"
)
func removeElement[T any](slice []T, index int) []T {
result := make([]T, 0)
for i, element := range slice {
if i != index {
result = append(result, element)
}
}
return result
}
func sliceDifference[T comparable](slice1, slice2 []T) []T {
difference := []T{}
lookup := make(map[T]bool)
for _, item := range slice2 {
lookup[item] = true
}
for _, item := range slice1 {
if !lookup[item] {
difference = append(difference, item)
}
}
return difference
}
func factorial(x int) int {
result := 1
for i := range x {
result *= i + 1
}
return result
}
func FindNthLexicographicPermutations(alphabet []string, n int) string {
permuations, remainedN := findNthLexicographicPermutations(alphabet, n)
remainedAlphabet := sliceDifference(alphabet, permuations)
if len(remainedAlphabet) == 1 {
permuations = append(permuations, remainedAlphabet[0])
return strings.Join(permuations, "")
}
remainedPermutations := Permute(remainedAlphabet)
remainedPermutationsStringSlice := make([]string, 0)
for _, permutation := range remainedPermutations {
remainedPermutationsStringSlice = append(remainedPermutationsStringSlice, strings.Join(permutation, ""))
}
sort.Sort(sort.StringSlice(remainedPermutationsStringSlice))
remainedPerm := remainedPermutationsStringSlice[remainedN-1]
for _, letter := range remainedPerm {
permuations = append(permuations, string(letter))
}
return strings.Join(permuations, "")
}
func findNthLexicographicPermutations(alphabet []string, n int) ([]string, int) {
permutation := make([]string, 0)
sort.Sort(sort.StringSlice(alphabet))
outerLoop:
for len(alphabet) > 0 {
selectedIndex := -1
states := factorial(len(alphabet) - 1)
for i := range alphabet {
if ((i + 1) * states) < n {
selectedIndex = i
} else if selectedIndex != -1 { //if nothing is selected and n is smaller
break
} else { // if sth is selected and n is smaller
break outerLoop
}
}
n -= (selectedIndex + 1) * states
permutation = append(permutation, alphabet[selectedIndex+1])
alphabet = removeElement(alphabet, selectedIndex+1)
}
return permutation, n
}
func Permute[T any](alphabet []T) [][]T {
permutations := make([][]T, 0)
permute(alphabet, []T{}, &permutations)
return permutations
}
func permute[T any](alphabet, current []T, result *[][]T) {
if len(alphabet) == 0 {
*result = append(*result, current)
return
}
for i := range alphabet {
newAlphabet := make([]T, len(alphabet)-1)
copy(newAlphabet[:i], alphabet[:i])
copy(newAlphabet[i:], alphabet[i+1:])
newCurrent := append(current, alphabet[i])
permute(newAlphabet, newCurrent, result)
}
}
func main() {
alphabet := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
n := 1000000
start := time.Now()
permutations := Permute(alphabet)
permutationsStringSlice := make([]string, 0)
for _, permutation := range permutations {
permutationsStringSlice = append(permutationsStringSlice, strings.Join(permutation, ""))
}
sort.Sort(sort.StringSlice(permutationsStringSlice))
result := permutationsStringSlice[n-1]
elapsedBruteforce := time.Since(start)
start = time.Now()
newResult := FindNthLexicographicPermutations(alphabet, n)
elapsedNewMethod := time.Since(start)
fmt.Printf(
"brute-force method: %dth lexicographic permutations = %s (elapsed time = %v)\n",
n,
result,
elapsedBruteforce,
)
fmt.Printf(
"new method: %dth lexicographic permutations = %s (elapsed time = %v)\n",
n,
newResult,
elapsedNewMethod,
)
}