Combination using goroutine - go

I'm trying to get a code to work that pretty much involves channels in goroutine (in a yield-like behavior in C#)
The code involves getting an iterable matrix from a slice like so:
elements := []float64{1, 2, 3, 4}
expected := [][]float64{
{1},
{2},
{3},
{4},
{1, 2},
{1, 3},
{2, 3},
{1, 4},
{2, 4},
{3, 4},
{1, 2, 3},
{1, 2, 4},
{1, 3, 4},
{2, 3, 4},
{1, 2, 3, 4},
}
I've tried applying Knuth's by doing:
func Combinadic(values []float64) <-chan []float64 {
ch := make(chan []float64)
go func() {
for i := 0; i < len(values); i++ {
for value := range CombinadicK(values, i+1) {
ch <- value
}
}
close(ch)
}()
return ch
}
func CombinadicK(values []float64, k int) <-chan []float64 {
chnl := make(chan []float64)
go func() {
n := len(values)
t := k
c := make([]int, t+3)
current := make([]float64, t)
x := 0
j := 0
for j = 1; j <= t; j++ {
c[j] = j - 1
}
c[t+1] = n
c[t+2] = 0
j = t
for {
for i := 0; i < len(current); i++ {
current[i] = values[c[i+1]]
}
chnl <- current
if j > 0 {
x = j
} else {
if c[1]+1 < c[2] {
c[1]++
continue
} else {
j = 2
}
}
for {
c[j-1] = j - 2
x = c[j] + 1
if x == c[j+1] {
j++
} else {
break
}
}
c[j] = x
j--
if j >= t {
break
}
}
close(chnl)
}()
return chnl
}
It appears to give out random number per row, but the structure of the expected (the count of items per row) appears to be ok.
The code in Go Playground

You have a data race. Your results are undefined.
$ go run -race racer.go
==================
WARNING: DATA RACE
Read at 0x00c00009c010 by main goroutine:
reflect.typedmemmove()
/home/peter/go/src/runtime/mbarrier.go:177 +0x0
reflect.packEface()
/home/peter/go/src/reflect/value.go:119 +0x103
reflect.valueInterface()
/home/peter/go/src/reflect/value.go:1027 +0x16f
fmt.(*pp).printValue()
/home/peter/go/src/reflect/value.go:997 +0x38f7
fmt.(*pp).printValue()
/home/peter/go/src/fmt/print.go:868 +0xec7
fmt.(*pp).printArg()
/home/peter/go/src/fmt/print.go:715 +0x2ee
fmt.(*pp).doPrintln()
/home/peter/go/src/fmt/print.go:1172 +0xad
fmt.Fprintln()
/home/peter/go/src/fmt/print.go:263 +0x65
main.main()
/home/peter/go/src/fmt/print.go:273 +0x14b
Previous write at 0x00c00009c010 by goroutine 8:
main.CombinadicK.func1()
/home/peter/racer.go:48 +0x1e6
Goroutine 8 (running) created at:
main.CombinadicK()
/home/peter/racer.go:26 +0x96
main.Combinadic.func1()
/home/peter/racer.go:12 +0xda
==================
[3]
[3]
[4]
[4]
[2 3]
[2 4]
[1 4]
[3 4]
[3 4]
[3 4]
[1 3 4]
[1 3 4]
[1 3 4]
[2 3 4]
[1 2 3 4]
Found 1 data race(s)
exit status 66
$
racer.go:
package main
import (
"fmt"
)
func Combinadic(values []float64) <-chan []float64 {
ch := make(chan []float64)
go func() {
for i := 0; i < len(values); i++ {
for value := range CombinadicK(values, i+1) {
ch <- value
}
}
close(ch)
}()
return ch
}
func CombinadicK(values []float64, k int) <-chan []float64 {
chnl := make(chan []float64)
go func() {
n := len(values)
t := k
c := make([]int, t+3)
current := make([]float64, t)
x := 0
j := 0
for j = 1; j <= t; j++ {
c[j] = j - 1
}
c[t+1] = n
c[t+2] = 0
j = t
for {
for i := 0; i < len(current); i++ {
current[i] = values[c[i+1]]
}
chnl <- current
if j > 0 {
x = j
} else {
if c[1]+1 < c[2] {
c[1]++
continue
} else {
j = 2
}
}
for {
c[j-1] = j - 2
x = c[j] + 1
if x == c[j+1] {
j++
} else {
break
}
}
c[j] = x
j--
if j >= t {
break
}
}
close(chnl)
}()
return chnl
}
func main() {
elements := []float64{1, 2, 3, 4}
for v := range Combinadic(elements) {
fmt.Println(v)
}
}
Playground: https://play.golang.org/p/hhQgVdqe6l1
Go: Data Race Detector

Related

Go: sort.Slice using foreign slice to sort

I'm coding on leetcode 973.
func kClosest(points [][]int, k int) [][]int {
sort.Slice(points, func(i, j int) bool {
if hypot(points[i]) > hypot(points[j]){
return false
}else{
return true
}
})
return points[:k]
}
func hypot(point []int) int {
ans := 0
for _, n := range point{
ans+=n*n
}
return ans
}
this is one of the answer. But this resolution involves redundant computation of distance, so i want to create a slice to restore the distance. and i'm trying to sort slice points using this foreign data like
func kClosest(points [][]int, k int) [][]int {
dist := make([]int, len(points))
for i:=0; i< len(points); i++{
dist[i] = hypot(points[i])
}
sort.Slice(points, func(i, j int) bool {
if dist[i] > dist[j]{
dist[i], dist[j] = dist[j], dist[i]
return false
}else{
return true
}
})
return points[:k]
}
func hypot(point []int) int {
ans := 0
for _, n := range point{
ans+=n*n
}
return ans
}
But in this case only slice dist is sorted while the slice points doesn't change.
I'm trying to refer to the source code but they seem to be wrapped.
Thanks if anyone could explain it to me or tell me where i can find the answer.
The purpose and responsibility of less() function you pass to sort.Slice() is to tell the is-less relation of 2 elements of the sortable slice. It should not perform changes on the slice, and it should be idempotent. Your less function is not idempotent: if you call it twice with the same i and j, it may return different result even if the slice is not modified between the calls.
Genesis: your original solution
Your original, improved solution is this:
func kClosest(points [][]int, k int) [][]int {
sort.Slice(points, func(i, j int) bool {
return hypot(points[i]) < hypot(points[j])
})
return points[:k]
}
Using an indices slice
One way of sorting a slice based on another is to create a slice holding the indices, sort this indices slice (based on the reference slice), and once we have the final index order, assemble the result.
Here's how it could look like:
func kClosest2(points [][]int, k int) [][]int {
dist := make([]int, len(points))
indices := make([]int, len(points))
for i := range dist {
indices[i] = i
dist[i] = hypot(points[i])
}
sort.Slice(indices, func(i, j int) bool {
return dist[indices[i]] <= dist[indices[j]]
})
result := make([][]int, k)
for i := range result {
result[i] = points[indices[i]]
}
return result
}
Creating sortable pairs
Another approach is to create pairs from the points and their distances, and sort the pairs.
Here's how it could be done:
func kClosest3(points [][]int, k int) [][]int {
type pair struct {
dist int
point []int
}
pairs := make([]pair, len(points))
for i := range pairs {
pairs[i].dist = hypot(points[i])
pairs[i].point = points[i]
}
sort.Slice(pairs, func(i, j int) bool {
return pairs[i].dist <= pairs[j].dist
})
result := make([][]int, k)
for i := range result {
result[i] = pairs[i].point
}
return result
}
Implementing sort.Interface and using a swap() method that swaps in both slices
Title says it all. We implement sort.Interface ourselves whose less() method will report based on the distance slice, but the swap() function will perform swapping on both slices:
type mySorter struct {
dist []int
points [][]int
}
func (m mySorter) Len() int { return len(m.dist) }
func (m mySorter) Swap(i, j int) {
m.dist[i], m.dist[j] = m.dist[j], m.dist[i]
m.points[i], m.points[j] = m.points[j], m.points[i]
}
func (m mySorter) Less(i, j int) bool { return m.dist[i] < m.dist[j] }
func kClosest4(points [][]int, k int) [][]int {
dist := make([]int, len(points))
for i := range dist {
dist[i] = hypot(points[i])
}
ms := mySorter{dist, points}
sort.Sort(ms)
return points[:k]
}
Testing the solutions
Here's a test code for all the above solutions:
solutions := []func(points [][]int, k int) [][]int{
kClosest, kClosest2, kClosest3, kClosest4,
}
for _, solution := range solutions {
points := [][]int{
{3, 5},
{0, 5},
{0, 0},
{1, 2},
{6, 0},
}
fmt.Println(solution(points, len(points)))
}
Which will output (try it on the Go Playground)
[[0 0] [1 2] [0 5] [3 5] [6 0]]
[[0 0] [1 2] [0 5] [3 5] [6 0]]
[[0 0] [1 2] [0 5] [3 5] [6 0]]
[[0 0] [1 2] [0 5] [3 5] [6 0]]

Is my golang func run time analysis correct?

enter image description here
This code snippet is for give two slice of binary number a1 and a2 to return sum slice r1, and I want to figure out how long spend with this code snippet figure out the result.
and I figure out the factorial result.
Is my analysis right?
my analysis for time complexity is:
cn + (n*n!) + c
the Code is:
func BinaryPlus(a1 []int, a2 []int) []int {
var r1 = make([]int, len(a1), 2*(len(a1)))
for i := 0; i < len(a1); i++ {
r1[i] = a1[i] + a2[i]
}
// 二分反转
ReverseSlice(r1)
r1 = append(r1, 0)
final := 0
for i := 0; final != 1; i++ {
isOver := 1
for j := 0; j < len(r1); j++ {
if r1[j] > 1 {
r1[j] = r1[j] % 2
r1[j+1] += 1
if r1[j+1] > 1 {
isOver = 0
}
}
}
if isOver == 1 {
final = 1
}
}
// 二分反转
ReverseSlice(r1)
return r1
}
func ReverseSlice(s interface{}) {
n := reflect.ValueOf(s).Len()
swap := reflect.Swapper(s)
for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
swap(i, j)
}
}
It is not entirely clear that your code, as written, is correct. The size cap for your result array could be too small. Consider the case that len(a2) > 2*len(a1): the r1 := make(...) will not reserve enough in this case. Further, the initial for loop will miss adding in the more significant bits of a2.
Binary addition should have no more than O(n) complexity. You can do it with a single for loop. n = 1+max(len(a1),len(a2)):
package main
import (
"fmt"
"reflect"
)
func BinaryPlus(a1 []int, a2 []int) []int {
reserve := len(a1) + 1
if x := len(a2) + 1; x > reserve {
reserve = x
}
hold := 0
maxBit := 1
ans := make([]int, reserve)
for i := 1; i <= reserve; i++ {
hold = hold / 2
if i <= len(a1) {
hold += a1[len(a1)-i]
}
if i <= len(a2) {
hold += a2[len(a2)-i]
}
ans[reserve-i] = hold & 1
if hold != 0 && i > maxBit {
maxBit = i
}
}
return ans[reserve-maxBit:]
}
func main() {
tests := []struct {
a, b, want []int
}{
{
a: []int{1},
b: []int{0},
want: []int{1},
},
{
a: []int{1, 0},
b: []int{0, 0, 1},
want: []int{1, 1},
},
{
a: []int{1, 0, 0, 1},
b: []int{1, 1, 1, 1, 0},
want: []int{1, 0, 0, 1, 1, 1},
},
{
a: []int{0, 0},
b: []int{0, 0, 0, 0, 0},
want: []int{0},
},
}
bad := false
for i := 0; i < len(tests); i++ {
t := tests[i]
c := BinaryPlus(t.a, t.b)
if !reflect.DeepEqual(c, t.want) {
fmt.Println(t.a, "+", t.b, "=", c, "; wanted:", t.want)
bad = true
}
}
if bad {
fmt.Println("FAILED")
} else {
fmt.Println("PASSED")
}
}

Strange Behavior of append in two dimension slice

I am a bit new to Go Language, and trying to build a function which will subdivide a slice into a number of slices with almost equal sizes. In case the size of the main slice does not fit into the number of sub-slices, I plan to redistribute the remaining elements to the sub-slices in order.
I have built the following code:
package main
import (
"fmt"
stc "strconv"
"strings"
)
func main() {
myslice := make([]int, 12)
myslice = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
fmt.Println("Original Slice = ", myslice)
newdiv := subslices(myslice, 4)
fmt.Println(newdiv)
}
func subslices(sl []int, dividnet int) [][]int {
var res [][]int
minsize := len(sl) / dividnet
for i := 0; i < dividnet; i++ {
res = append(res, sl[i*minsize:i*minsize+minsize])
}
for i := 0; i < dividnet; i++ {
fmt.Printf("res[%d] = %v\n", i, res[i])
}
fmt.Println(res)
if rem := len(sl) % dividnet; rem != 0 {
fmt.Println("remaining elements = ", rem)
for j := 0; j < rem; j++ {
tobeadd := sl[minsize*dividnet+j]
fmt.Println("element to be added = ", tobeadd)
fmt.Printf("res[%d] before append = %v\n", j, res[j])
res[j] = append(res[j], tobeadd)
fmt.Printf("res[%d] after append = %v\n", j, res[j])
}
}
return res
}
func gentwodim(x, y int) [][]int {
res := make([][]int, x)
for z := range res {
res[z] = make([]int, y)
}
for i := 0; i < x; i++ {
for j := 0; j < y; j++ {
res[i][j] = i + j
}
}
return res
}
A Sample in Go Play
sample of the above code
The output of the code is as follows:
res[0] = [1 2]
res[1] = [3 4]
res[2] = [5 6]
res[3] = [7 8]
[[1 2] [3 4] [5 6] [7 8]]
remaining elements = 2
element to be added = 9
res[0] before append = [1 2]
res[0] after append = [1 2 9] // up to this step the code works fine
element to be added = 10
res[1] before append = [9 4] // I did not get why res[1] is changed by replacing 3 with 9
res[1] after append = [9 4 10]
[[1 2 9] [9 4 10] [10 6] [7 8]]
However, after appending the first remaining element which shown in res[0] after append = [1 2 9], the 2nd sub-slice is changed as shown from res[1] = [3 4] to res[1] before append = [9 4]
I have tried to debug and understand what I have missed or coded wrong here, but could not.
I would appreciate your support.
Avoid the problem by distributing the "extra" values as you slice things up:
func subslices(sl []int, dividnet int) [][]int {
var res [][]int
var i int
for len(sl) > 0 {
i = len(sl) / (dividnet - len(res))
res = append(res, sl[:i])
sl = sl[i:]
}
return res
}

Change slice content and capacity inside a function in-place

I am trying to learn Go, so here is my very simple function for removing adjacent duplicates from slice for exercise from the book by Donovan & Kernighan.
Here is the code: https://play.golang.org/p/avHc1ixfck
package main
import "fmt"
func main() {
a := []int{0, 1, 1, 3, 3, 3}
removeDup(a)
fmt.Println(a)
}
func removeDup(s []int) {
n := len(s)
tmp := make([]int, 0, n)
tmp = append(tmp, s[0])
j := 1
for i := 1; i < n; i++ {
if s[i] != s[i-1] {
tmp = append(tmp, s[i])
j++
}
}
s = s[:len(tmp)]
copy(s, tmp)
}
It should print out [0 1 3] - and I checked, actually tmp at the end of the function it has desired form. However, the result is [0 1 3 3 3 3]. I guess there is something with copy function.
Can I somehow replace input slice s with the temp or trim it to desired length?
Option 1
Return a new slice as suggested by #zerkms.
https://play.golang.org/p/uGJiD3WApS
package main
import "fmt"
func main() {
a := []int{0, 1, 1, 3, 3, 3}
a = removeDup(a)
fmt.Println(a)
}
func removeDup(s []int) []int {
n := len(s)
tmp := make([]int, 0, n)
tmp = append(tmp, s[0])
for i := 1; i < n; i++ {
if s[i] != s[i-1] {
tmp = append(tmp, s[i])
}
}
return tmp
}
Option 2
Use pointers for pass-by-reference.
The same thing in effect as that of option1.
https://play.golang.org/p/80bE5Qkuuj
package main
import "fmt"
func main() {
a := []int{0, 1, 1, 3, 3, 3}
removeDup(&a)
fmt.Println(a)
}
func removeDup(sp *[]int) {
s := *sp
n := len(s)
tmp := make([]int, 0, n)
tmp = append(tmp, s[0])
for i := 1; i < n; i++ {
if s[i] != s[i-1] {
tmp = append(tmp, s[i])
}
}
*sp = tmp
}
Also, refer to following SO thread:
Does Go have no real way to shrink a slice? Is that an issue?
Here's two more slightly different ways to achieve what you want using sets and named types. The cool thing about named types is that you can create interfaces around them and can help with the readability of lots of code.
package main
import "fmt"
func main() {
// returning a list
a := []int{0, 1, 1, 3, 3, 3}
clean := removeDup(a)
fmt.Println(clean)
// creating and using a named type
nA := &newArrType{0, 1, 1, 3, 3, 3}
nA.removeDup2()
fmt.Println(nA)
// or... casting your orginal array to the named type
nB := newArrType(a)
nB.removeDup2()
fmt.Println(nB)
}
// using a set
// order is not kept, but a set is returned
func removeDup(s []int) (newArr []int) {
set := make(map[int]struct{})
for _, n := range s {
set[n] = struct{}{}
}
newArr = make([]int, 0, len(set))
for k := range set {
newArr = append(newArr, k)
}
return
}
// using named a typed
type newArrType []int
func (a *newArrType) removeDup2() {
x := *a
for i := range x {
f := i + 1
if f < len(x) {
if x[i] == x[f] {
x = x[:f+copy(x[f:], x[f+1:])]
}
}
}
// check the last 2 indexes
if x[len(x)-2] == x[len(x)-1] {
x = x[:len(x)-1+copy(x[len(x)-1:], x[len(x)-1+1:])]
}
*a = x
}

Generate all permutations in go

I am looking for a way to generate all possible permutations of a list of elements. Something similar to python's itertools.permutations(arr)
permutations ([])
[]
permutations ([1])
[1]
permutations ([1,2])
[1, 2]
[2, 1]
permutations ([1,2,3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
With the difference that I do not care whether permutations would be generated on demand (like a generator in python) or all together. I also do not care whether they will be lexicographically sorted. All I need is to somehow get these n! permutations.
There are a lot of the algorithms that generate permutations. One of the easiest I found is Heap's algorithm:
It generates each permutation from the previous one by choosing a pair
of elements to interchange.
The idea and a pseudocode that prints the permutations one after another is outlined in the above link. Here is my implementation of the algorithm which returns all permutations
func permutations(arr []int)[][]int{
var helper func([]int, int)
res := [][]int{}
helper = func(arr []int, n int){
if n == 1{
tmp := make([]int, len(arr))
copy(tmp, arr)
res = append(res, tmp)
} else {
for i := 0; i < n; i++{
helper(arr, n - 1)
if n % 2 == 1{
tmp := arr[i]
arr[i] = arr[n - 1]
arr[n - 1] = tmp
} else {
tmp := arr[0]
arr[0] = arr[n - 1]
arr[n - 1] = tmp
}
}
}
}
helper(arr, len(arr))
return res
}
and here is an example of how to use it (Go playground):
arr := []int{1, 2, 3}
fmt.Println(permutations(arr))
[[1 2 3] [2 1 3] [3 2 1] [2 3 1] [3 1 2] [1 3 2]]
One thing to notice that the permutations are not sorted lexicographically (as you have seen in itertools.permutations). If for some reason you need it to be sorted, one way I have found it is to generate them from a factorial number system (it is described in permutation section and allows to quickly find n-th lexicographical permutation).
P.S. you can also take a look at others people code here and here
Here's code that iterates over all permutations without generating them all first. The slice p keeps the intermediate state as offsets in a Fisher-Yates shuffle algorithm. This has the nice property that the zero value for p describes the identity permutation.
package main
import "fmt"
func nextPerm(p []int) {
for i := len(p) - 1; i >= 0; i-- {
if i == 0 || p[i] < len(p)-i-1 {
p[i]++
return
}
p[i] = 0
}
}
func getPerm(orig, p []int) []int {
result := append([]int{}, orig...)
for i, v := range p {
result[i], result[i+v] = result[i+v], result[i]
}
return result
}
func main() {
orig := []int{11, 22, 33}
for p := make([]int, len(orig)); p[0] < len(p); nextPerm(p) {
fmt.Println(getPerm(orig, p))
}
}
var res [][]int
func permute(nums []int) [][]int {
res=make([][]int,0)
n:=len(nums)
var backTrack func(int)
backTrack=func(first int){
if first == n{
temp:=make([]int, n)
copy(temp,nums)
res = append(res, temp)
}
for i:=first;i<n;i++{
nums[first],nums[i] = nums[i],nums[first]
backTrack(first+1)
nums[first],nums[i] = nums[i],nums[first]
}
}
backTrack(0)
return res
}
In my case I had a reference to an array, then I've did a few changes in your example:
func generateIntPermutations(array []int, n int, result *[][]int) {
if n == 1 {
dst := make([]int, len(array))
copy(dst, array[:])
*result = append(*result, dst)
} else {
for i := 0; i < n; i++ {
generateIntPermutations(array, n-1, result)
if n%2 == 0 {
// Golang allow us to do multiple assignments
array[0], array[n-1] = array[n-1], array[0]
} else {
array[i], array[n-1] = array[n-1], array[i]
}
}
}
}
numbers := []int{0, 1, 2}
var result [][]int
generateIntPermutations(numbers, len(numbers), &result)
// result -> [[0 1 2] [1 0 2] [2 1 0] [1 2 0] [2 0 1] [0 2 1]]
Another Working code
package permutations
import "fmt"
func AllPermutation(a []int) {
var res [][]int
calPermutation(a, &res, 0)
fmt.Println(res)
}
func calPermutation(arr []int, res *[][]int, k int) {
for i := k; i < len(arr); i++ {
swap(arr, i, k)
calPermutation(arr, res, k+1)
swap(arr, k, i)
}
if k == len(arr)-1 {
r := make([]int, len(arr))
copy(r, arr)
*res = append(*res, r)
return
}
}
func swap(arr []int, i, k int) {
arr[i], arr[k] = arr[k], arr[i]
}
//result [[1 2 3] [1 3 2] [2 1 3] [2 3 1] [3 2 1] [3 1 2]]
Here is another variation:
// heap algorithm
func permutations(arr []int, l int, p [][]int) [][]int {
if l == 1 { p = append(p, append([]int{}, arr...)) }
for i := 0 ; i < l ; i++ {
p = permutations(arr, l-1, p)
if l % 2 == 1 {
arr[0], arr[l-1] = arr[l-1], arr[0]
} else {
arr[i], arr[l-1] = arr[l-1], arr[i]
}
}
return p
}

Resources