【问题标题】:Get all permutations of a slice获取切片的所有排列
【发布时间】:2013-04-25 19:17:25
【问题描述】:

我想知道是否有办法在 Go 中找到一个充满字符的切片的所有排列?

在 Python 中,您可以将itertools.product 与列表、字符或整数一起使用,并且可以获得所有可能的排列。

我查看了那里是否有包裹,但我似乎找不到。欢迎任何帮助。

【问题讨论】:

  • itertools.product 给你一些集合的笛卡尔积。它确实给你排列。尽管您可以使用笛卡尔积来计算排列,但这将是非常低效的。 docs.python.org/2/library/itertools.html#itertools.product
  • 我是个白痴。我总是把这些弄混,我找到了笛卡尔产品的包装。谢谢
  • @Colum 你搞错了;这不会让你成为白痴。
  • 根据问题的大小(和细节),看看 Knuth, TAoCP vol. 4,第7.2.1.2章(对不起,这里没有直接的Go代码。)
  • itertools.permutations,但是,确实在 Python 中为您提供排列。

标签: go permutation


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    这是我编写的置换函数的实现...

    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
    }
    

    它将 2D 切片作为输入并返回 3D 切片,但您可以轻松更改代码,以便该函数将简单切片作为输入并返回具有所有排列的 2D 切片

    【讨论】:

      【解决方案3】:

      不确定这是否能回答您的问题,但这是一个简单的递归实现,可以找到下面的输出。

      package main
      
      import "fmt"
      
      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

      上面sn-p的输出:

      [[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]]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-03
        • 2012-04-30
        • 1970-01-01
        • 2015-05-22
        • 2017-02-27
        相关资源
        最近更新 更多