FAQ
It's very simple function that just keeps sorting and add the sorted to
another two dimensional slice defined before for-loop.

But the final result slice is of only one single element. I was expecting
to see all possible permutation of input string slice.

What am I missing? I would greatly appreciate your help!

http://play.golang.org/p/rDU0GFlAe6

package main

import (
"fmt"
"log"
"sort"
)

func first(data sort.Interface) {
sort.Sort(data)
}

func next(data sort.Interface) bool {
// false when it cannot permute any more
//
http://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
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
}

// String returns all possible permutations of string slice.
func String(slice []string) [][]string {

first(sort.StringSlice(slice))
result := [][]string{slice}
cn := 0
for {
cn++
isDone := next(sort.StringSlice(slice))
if !isDone {
break
}

fmt.Println("Elem:", result)
result = append(result, slice)
}
fmt.Println(cn)
possibleNum := 1
for i := 0; i < len(slice); i++ {
possibleNum *= i + 1
}
if len(result) != possibleNum {
log.Fatalf("Should be %d combinations but %+v", possibleNum, result)
}

return result
}

func main() {
slice := []string{"D", "C", "A", "E", "X"}
fmt.Println(String(slice))
}

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Dave Cheney at Dec 16, 2014 at 10:21 pm
    A slice is just a pointer to the underlying back array, your storing muktiple copies of the slice header, but they all point to the same backing array.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Gyu-Ho Lee at Dec 16, 2014 at 10:30 pm
    Thanks Dave, I am still confused. When I
    ran http://play.golang.org/p/sRtiv0Nseb


    Elem: [E C A D X]
    Elem: [E C A X D]
    Elem: [E C D A X]
    Elem: [E C D X A]
    Elem: [E C X A D]
    Elem: [E C X D A]

    ...


    So the printed slice is all different and what I want to append.

    Then I am doing wrong in this line? `result = append(result, slice)`

    How would you get all permutations out of this code? I worked it out just
    strings.Join and append to 1-dimension slice, instead of using
    two-dimensional slice.

    But I still do not understand what I am doing wrong. Should I make a
    shallow copy of the slice to append to two-dimensional slice?

    Thanks!
    On Tuesday, December 16, 2014 2:21:20 PM UTC-8, Dave Cheney wrote:

    A slice is just a pointer to the underlying back array, your storing
    muktiple copies of the slice header, but they all point to the same backing
    array.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dave Cheney at Dec 16, 2014 at 10:59 pm
    A slicd is always a "shallow copy" of its back array.

    Have a read of http://blog.golang.org/slices for some background on the differences between slices and arrays

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Gyu-Ho Lee at Dec 17, 2014 at 2:34 am
    Thanks Dave!
    On Tuesday, December 16, 2014 2:59:00 PM UTC-8, Dave Cheney wrote:

    A slicd is always a "shallow copy" of its back array.

    Have a read of http://blog.golang.org/slices for some background on the
    differences between slices and arrays
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • John Clover at Dec 16, 2014 at 10:26 pm
    You need to copy the slice before appending it.

    http://play.golang.org/p/moc9IfU6tU

    see line 50
    On Dec 16, 2014, at 2:16 PM, Gyu-Ho Lee wrote:

    It's very simple function that just keeps sorting and add the sorted to another two dimensional slice defined before for-loop.

    But the final result slice is of only one single element. I was expecting to see all possible permutation of input string slice.

    What am I missing? I would greatly appreciate your help!

    http://play.golang.org/p/rDU0GFlAe6

    package main

    import (
    "fmt"
    "log"
    "sort"
    )

    func first(data sort.Interface) {
    sort.Sort(data)
    }

    func next(data sort.Interface) bool {
    // false when it cannot permute any more
    // http://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
    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
    }

    // String returns all possible permutations of string slice.
    func String(slice []string) [][]string {

    first(sort.StringSlice(slice))
    result := [][]string{slice}
    cn := 0
    for {
    cn++
    isDone := next(sort.StringSlice(slice))
    if !isDone {
    break
    }

    fmt.Println("Elem:", result)
    result = append(result, slice)
    }
    fmt.Println(cn)
    possibleNum := 1
    for i := 0; i < len(slice); i++ {
    possibleNum *= i + 1
    }
    if len(result) != possibleNum {
    log.Fatalf("Should be %d combinations but %+v", possibleNum, result)
    }

    return result
    }

    func main() {
    slice := []string{"D", "C", "A", "E", "X"}
    fmt.Println(String(slice))
    }


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Gyu-Ho Lee at Dec 16, 2014 at 10:31 pm
    I see. Thank you so much. It was very tricky for me.
    On Tuesday, December 16, 2014 2:26:21 PM UTC-8, John Clover wrote:

    You need to copy the slice before appending it.

    http://play.golang.org/p/moc9IfU6tU

    see line 50
    On Dec 16, 2014, at 2:16 PM, Gyu-Ho Lee <gyuh...@gmail.com <javascript:>> wrote:
    It's very simple function that just keeps sorting and add the sorted to
    another two dimensional slice defined before for-loop.
    But the final result slice is of only one single element. I was
    expecting to see all possible permutation of input string slice.
    What am I missing? I would greatly appreciate your help!

    http://play.golang.org/p/rDU0GFlAe6

    package main

    import (
    "fmt"
    "log"
    "sort"
    )

    func first(data sort.Interface) {
    sort.Sort(data)
    }

    func next(data sort.Interface) bool {
    // false when it cannot permute any more
    //
    http://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
    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
    }

    // String returns all possible permutations of string slice.
    func String(slice []string) [][]string {

    first(sort.StringSlice(slice))
    result := [][]string{slice}
    cn := 0
    for {
    cn++
    isDone := next(sort.StringSlice(slice))
    if !isDone {
    break
    }

    fmt.Println("Elem:", result)
    result = append(result, slice)
    }
    fmt.Println(cn)
    possibleNum := 1
    for i := 0; i < len(slice); i++ {
    possibleNum *= i + 1
    }
    if len(result) != possibleNum {
    log.Fatalf("Should be %d combinations but %+v",
    possibleNum, result)
    }

    return result
    }

    func main() {
    slice := []string{"D", "C", "A", "E", "X"}
    fmt.Println(String(slice))
    }


    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 16, '14 at 10:16p
activeDec 17, '14 at 2:34a
posts7
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase