2013-04-25 30 views
5

Go karakterli bir dilimin tüm permütasyonlarını bulmanın bir yolu olup olmadığını merak ediyordum?Dilimin tüm permütasyonlarını alın

Python'da bir liste veya karakterler veya tam sayılarla 'u kullanabilirsiniz ve olası tüm izinleri alabilirsiniz.

Orada bir paket olup olmadığını görmek için baktım ve bir tane bulamıyorum. Herhangi bir yardım memnuniyetle karşılanacaktır. şey

+2

'itertools.product' bazı kümenin kartezyen ürün verir. Bu size * izin verme * vermez. Kartezyen ürünleri kullanabilmenize rağmen, permütasyonları anlayabilmek için bu çok verimsiz olacaktır. http://docs.python.org/2/library/itertools.html#itertools.product – scvalex

+0

Ben bir aptalım. Bunları her zaman karıştırıyorum ve Kartezyen ürünler için bir paket buldum. Teşekkür – Colum

+1

@Colum Bir hata yaptınız; Bu seni aptal yapmaz. –

cevap

0

Permutation{First,Next} i yazdım bir permütasyon fonksiyonunun uygulamasıdır ...

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 
} 

o girdi olarak 2D dilim alır ve bir 3B dilimi döndürür, ancak kodu kolayca değiştirebilirsiniz, böylece işlev giriş olarak basit bir dilim alır ve tüm permütasyonlarla bir 2D dilimi döndürür

0

Sorunun yanıtı bu değil, ancak bu çıktıyı bulmak için basit bir özyinelemeli uygulama olduğundan emin değilsiniz. Yukarıdaki snippet'in

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

Çıktı:

[[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]] 
İlgili konular