GolangNote

Golang笔记

goLang 实现排列组合的代码

Permalink

几种比较好的 goLang 排列组合方法推荐

Go: Port of some Python's itertools
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Port of some Python's itertools
// ================================
// A.K.A. my first piece of Go code
//
// Written by Nuno Antunes, 2012-08-08
// GitHub: https://github.com/ntns
//
// Python docs: http://docs.python.org/library/itertools.html
// Python source: http://svn.python.org/view/python/tags/r271/Modules/itertoolsmodule.c?view=markup

package main

import "fmt"

func combinations(iterable []int, r int) {
    pool := iterable
    n := len(pool)

    if r > n {
        return
    }

    indices := make([]int, r)
    for i := range indices {
        indices[i] = i
    }

    result := make([]int, r)
    for i, el := range indices {
        result[i] = pool[el]
    }

    fmt.Println(result)

    for {
        i := r - 1
        for ; i >= 0 && indices[i] == i+n-r; i -= 1 {
        }

        if i < 0 {
            return
        }

        indices[i] += 1
        for j := i + 1; j < r; j += 1 {
            indices[j] = indices[j-1] + 1
        }

        for ; i < len(indices); i += 1 {
            result[i] = pool[indices[i]]
        }
        fmt.Println(result)

    }

}

func permutations(iterable []int, r int) {
    pool := iterable
    n := len(pool)

    if r > n {
        return
    }

    indices := make([]int, n)
    for i := range indices {
        indices[i] = i
    }

    cycles := make([]int, r)
    for i := range cycles {
        cycles[i] = n - i
    }

    result := make([]int, r)
    for i, el := range indices[:r] {
        result[i] = pool[el]
    }

    fmt.Println(result)

    for n > 0 {
        i := r - 1
        for ; i >= 0; i -= 1 {
            cycles[i] -= 1
            if cycles[i] == 0 {
                index := indices[i]
                for j := i; j < n-1; j += 1 {
                    indices[j] = indices[j+1]
                }
                indices[n-1] = index
                cycles[i] = n - i
            } else {
                j := cycles[i]
                indices[i], indices[n-j] = indices[n-j], indices[i]

                for k := i; k < r; k += 1 {
                    result[k] = pool[indices[k]]
                }

                fmt.Println(result)

                break
            }
        }

        if i < 0 {
            return
        }

    }

}

func product(argsA, argsB []int) {

    pools := [][]int{argsA, argsB}
    npools := len(pools)
    indices := make([]int, npools)

    result := make([]int, npools)
    for i := range result {
        result[i] = pools[i][0]
    }

    fmt.Println(result)

    for {
        i := npools - 1
        for ; i >= 0; i -= 1 {
            pool := pools[i]
            indices[i] += 1

            if indices[i] == len(pool) {
                indices[i] = 0
                result[i] = pool[0]
            } else {
                result[i] = pool[indices[i]]
                break
            }

        }

        if i < 0 {
            return
        }

        fmt.Println(result)
    }
}

func main() {
    fmt.Println("Itertools combinations in Go:")
    // combinations('ABCD', 2) --> AB AC AD BC BD CD
    // combinations(range(4), 3) --> 012 013 023 123
    fmt.Printf("iterable = %s, r = %d", "[]int{1, 2, 3, 4, 5, 6}", 3)
    fmt.Println()
    combinations([]int{1, 2, 3, 4, 5, 6}, 3)

    fmt.Println("Itertools permutations in Go:")
    // permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
    // permutations(range(3)) --> 012 021 102 120 201 210
    fmt.Printf("iterable = %s, r = %d", "[]int{1, 2, 3, 4}", 3)
    fmt.Println()
    permutations([]int{1, 2, 3, 4}, 3)

    fmt.Println("Itertools product in Go:")
    // product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    // product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    fmt.Printf("iterables = %s, %s", "[]int{1, 2, 3}", "[]int{10, 20, 30}")
    fmt.Println()
    product([]int{1, 2, 3}, []int{10, 20, 30})

}

运行输出:

plaintext: 排列组合输出
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
Itertools combinations in Go:
iterable = []int{1, 2, 3, 4, 5, 6}, r = 3
[1 2 3]
[1 2 4]
[1 2 5]
[1 2 6]
[1 3 4]
[1 3 5]
[1 3 6]
[1 4 5]
[1 4 6]
[1 5 6]
[2 3 4]
[2 3 5]
[2 3 6]
[2 4 5]
[2 4 6]
[2 5 6]
[3 4 5]
[3 4 6]
[3 5 6]
[4 5 6]
Itertools permutations in Go:
iterable = []int{1, 2, 3, 4}, r = 3
[1 2 3]
[1 2 4]
[1 3 2]
[1 3 4]
[1 4 2]
[1 4 3]
[2 1 3]
[2 1 4]
[2 3 1]
[2 3 4]
[2 4 1]
[2 4 3]
[3 1 2]
[3 1 4]
[3 2 1]
[3 2 4]
[3 4 1]
[3 4 2]
[4 1 2]
[4 1 3]
[4 2 1]
[4 2 3]
[4 3 1]
[4 3 2]
Itertools product in Go:
iterables = []int{1, 2, 3}, []int{10, 20, 30}
[1 10]
[1 20]
[1 30]
[2 10]
[2 20]
[2 30]
[3 10]
[3 20]
[3 30]

这里有另外一个实现: https://github.com/kokardy/listing

使用方法:

Go: 排列组合 listing
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
package main

import (
    "fmt"
    "github.com/kokardy/listing"
)

func main() {

    ss := listing.StringReplacer([]string{
        "A", "B", "C", "D",
    })
    //You can use any types that implement listing.Replacer interface (Len(), Replace([]int))


    select_num := 3
    repeatable := false
    buf := 5

    fmt.Println("Permutations")
    for perm := range listing.Permutations(ss, select_num, repeatable, buf) {
        fmt.Println(perm)
    }

    fmt.Println("Combinations")
    for comb := range listing.Combinations(ss, select_num, repeatable, buf) {
        fmt.Println(comb)
    }

}

输出:

plaintext: listing 排列组合输出
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
Permutations
[A B C]
[A C B]
[B A C]
[B C A]
[C A B]
[C B A]
[A B D]
[A D B]
[B A D]
[B D A]
[D A B]
[D B A]
[A C D]
[A D C]
[C A D]
[C D A]
[D A C]
[D C A]
[B C D]
[B D C]
[C B D]
[C D B]
[D B C]
[D C B]
Combinations
[A B C]
[A B D]
[A C D]
[B C D]

更简单的实现:

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
package main
 
import (
    "fmt"
)
 
func main() {
    comb(5, 3, func(c []int) {
        fmt.Println(c)
    })
}
 
func comb(n, m int, emit func([]int)) {
    s := make([]int, m)
    last := m - 1
    var rc func(int, int)
    rc = func(i, next int) {
        for j := next; j < n; j++ {
            s[i] = j
            if i == last {
                emit(s)
            } else {
                rc(i+1, j+1)
            }
        }
        return
    }
    rc(0, 0)
}

输出:

plaintext: 排列组合输出
1
2
3
4
5
6
7
8
9
10
[0 1 2]
[0 1 3]
[0 1 4]
[0 2 3]
[0 2 4]
[0 3 4]
[1 2 3]
[1 2 4]
[1 3 4]
[2 3 4]

本文网址: https://golangnote.com/topic/85.html 转摘请注明来源

Related articles

Golang实现简单的Socks5代理

Socks5 代理较 `http/https` 代理有较好的性能,下面是借鉴某个著名开源软件的 local 实现的简单代理。...

Golang 实现 10 进制转 N 进制

给定一个不没有重复字符的字符串,如 `[0-9,a-z]`,把一个 10 进制数字转为,该字符集的字符串。应用场合如汽车牌、顺序计数。...

golang 实现的基于web的文件管理-filebrowser

FileBrowser 在指定目录中提供了一个文件管理界面,可用于上传,删除,预览,重命名和编辑文件。它允许创建多个用户,每个用户都可以有自己的目录。它可以用作独立的应用程序。...

Golang phantomjs 动态代理实现

phantomjs 是个很优秀的软件,虽然现在被chrome headless 抢了风头,但在某些特定场合,使用phantomjs 还是很方便,这里是介绍使用Go 实现动态代理。...

Write a Comment to "goLang 实现排列组合的代码"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 15ms