/
24_permute_string.go
129 lines (115 loc) · 2.81 KB
/
24_permute_string.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
package main
import (
"bytes"
"fmt"
"sort"
"strings"
)
func main() {
func() {
slice := []string{"D", "C", "A", "E", "X"}
rs := permuteStrings(slice)
// var buf bytes.Buffer
buf := new(bytes.Buffer)
for _, elem1 := range rs {
for _, elem2 := range elem1 {
buf.WriteString(elem2)
}
}
if len(buf.String()) != 600 {
fmt.Errorf("Length should be 600 but %d", len(buf.String()))
}
}()
func() {
slice := []string{"E", "A", "A"}
rs := permuteStrings(slice)
// var buf bytes.Buffer
buf := new(bytes.Buffer)
for _, elem1 := range rs {
for _, elem2 := range elem1 {
buf.WriteString(elem2)
}
}
if len(buf.String()) != 9 || len(rs) != 3 {
fmt.Errorf("Length should be 9 but %d", len(buf.String()))
}
}()
func() {
slice := []string{"1", "2", "3"}
rs := permuteStrings(slice)
// var buf bytes.Buffer
buf := new(bytes.Buffer)
for _, elem1 := range rs {
for _, elem2 := range elem1 {
buf.WriteString(elem2)
}
buf.WriteString("---")
}
rStr := buf.String()
if len(rStr) != 36 {
fmt.Errorf("Length should be 600 but %s", rStr)
}
if !strings.Contains(rStr, "123") ||
!strings.Contains(rStr, "132") ||
!strings.Contains(rStr, "213") ||
!strings.Contains(rStr, "231") ||
!strings.Contains(rStr, "312") ||
!strings.Contains(rStr, "321") {
fmt.Errorf("Missing a permutation %s", rStr)
}
}()
}
func first(data sort.Interface) {
sort.Sort(data)
}
// next returns false when it cannot permute any more
// http://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
func next(data sort.Interface) bool {
var k, l int
for k = data.Len() - 2; ; k-- {
if k < 0 {
return false
}
if data.Less(k, k+1) {
break
}
}
for l = data.Len() - 1; !data.Less(k, l); l-- {
}
data.Swap(k, l)
for i, j := k+1, data.Len()-1; i < j; i++ {
data.Swap(i, j)
j--
}
return true
}
// permuteStrings returns all possible permutations of string slice.
func permuteStrings(slice []string) [][]string {
first(sort.StringSlice(slice))
copied1 := make([]string, len(slice)) // we need to make a copy!
copy(copied1, slice)
result := [][]string{copied1}
for {
isDone := next(sort.StringSlice(slice))
if !isDone {
break
}
// https://groups.google.com/d/msg/golang-nuts/ApXxTALc4vk/z1-2g1AH9jQJ
// Lesson from Dave Cheney:
// A slice is just a pointer to the underlying back array, your storing multiple
// copies of the slice header, but they all point to the same backing array.
// NOT
// result = append(result, slice)
copied2 := make([]string, len(slice))
copy(copied2, slice)
result = append(result, copied2)
}
combNum := 1
for i := 0; i < len(slice); i++ {
combNum *= i + 1
}
if len(result) != combNum {
fmt.Printf("Expected %d combinations but %+v because of duplicate elements", combNum, result)
}
return result
}