获取切片的所有排列组合

huangapple go评论86阅读模式
英文:

Get all permutations of a slice

问题

我想知道在Go语言中是否有一种方法可以找到一个填满字符的切片的所有排列方式?

在Python中,你可以使用itertools.product来对一个字符或整数的列表进行排列,从而得到所有可能的排列方式。

我已经查找了是否有相关的包,但似乎找不到。任何帮助将不胜感激。

英文:

I was wondering if there was a way to find all the permutations of an slice filled with characters in Go?

In Python you can use itertools.product with a list or characters or integers, and you can get all the possible permutations.

I have looked to see if there is a package out there, and I cannot seem to find one. Any help would be welcomed.

答案1

得分: 11

sort.Interface实现的任何东西的排列:Permutation{First,Next}

英文:

Permutations of anything implementing sort.Interface: Permutation{First,Next}

答案2

得分: 0

这是我写的一个排列函数的实现...

https://github.com/itcraftsman/GoPermutation

func permutate(slice [][]int) (permutations [][][]int){
	f := fac(len(slice))
	for i := 0; i < len(slice); i++ {
		elem, s := splice(slice, i)
		pos := 0
		for count := 0; count < (f / len(slice)); count++{
			if pos == (len(s) -1) {
				pos = 0
			}
			s = swap(s, pos, pos +1)
			permutation := make([][]int, len(slice))
			permutation = s
			permutation = append(permutation, elem)
			permutations = append(permutations, permutation)
			pos++
		} 
	} 
	return
}

它接受一个二维切片作为输入,并返回一个三维切片,但你可以很容易地修改代码,使函数接受一个简单的切片作为输入,并返回一个包含所有排列的二维切片。

英文:

Here is a implementation of a permutation function i've written...

https://github.com/itcraftsman/GoPermutation

func permutate(slice [][]int) (permutations [][][]int){
	f := fac(len(slice))
	for i := 0; i &lt; len(slice); i++ {
		elem, s := splice(slice, i)
		pos := 0
		for count := 0; count &lt; (f / len(slice)); count++{
			if pos == (len(s) -1) {
				pos = 0
			}
			s = swap(s, pos, pos +1)
			permutation := make([][]int, len(slice))
			permutation = s
			permutation = append(permutation, elem)
			permutations = append(permutations, permutation)
			pos++
		} 
	} 
	return
}

it takes a 2D slice as input and returns a 3D slice, but you can easily change the code so that the function will take a simple slice as input and return a 2D slice with all permutations

答案3

得分: 0

这是一个简单的递归实现,用于找到下面的输出。

输出如下:

[[1 4 7] [1 4 8] [1 4 9] [1 5 7] [1 5 8] [1 5 9] [1 6 7] [1 6 8] [1 6 9] [2 4 7] [2 4 8] [2 4 9] [2 5 7] [2 5 8] [2 5 9] [2 6 7] [2 6 8] [2 6 9] [3 4 7] [3 4 8] [3 4 9] [3 5 7] [3 5 8] [3 5 9] [3 6 7] [3 6 8] [3 6 9]]

英文:

Not really sure if this answers your question but this is a simple recursive implementation to find the output below.

package main

import &quot;fmt&quot;

func main() {
	values := [][]int{}

	// These are the first two rows.
	row1 := []int{1, 2, 3}
	row2 := []int{4, 5, 6}
	row3 := []int{7, 8, 9}

	// Append each row to the two-dimensional slice.
	values = append(values, row1)
	values = append(values, row2)
	values = append(values, row3)

	
	fmt.Println(getPermutation(values))
}

func getPermutation(vids [][]int) [][]int {
	toRet := [][]int{}

    if len(vids) == 0 {
      return toRet
    }

	if len(vids) == 1 {
		for _, vid := range vids[0] {
			toRet = append(toRet, []int{vid})
		}
		return toRet
	}

	t := getPermutation(vids[1:])
	for _, vid := range vids[0] {
		for _, perm := range t {
			toRetAdd := append([]int{vid}, perm...)
			toRet = append(toRet, toRetAdd)
		}
	}

	return toRet
}

https://play.golang.org/p/f8wktrxkU0

Output of above snippet:

[[1 4 7] [1 4 8] [1 4 9] [1 5 7] [1 5 8] [1 5 9] [1 6 7] [1 6 8] [1 6 9] [2 4 7] [2 4 8] [2 4 9] [2 5 7] [2 5 8] [2 5 9] [2 6 7] [2 6 8] [2 6 9] [3 4 7] [3 4 8] [3 4 9] [3 5 7] [3 5 8] [3 5 9] [3 6 7] [3 6 8] [3 6 9]]

huangapple
  • 本文由 发表于 2013年4月26日 03:17:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/16222979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定