I'm trying to create a block matrix which contains 4 blocks (n*n submatrices).
I tried many things but I can't get it to work.
func newBlocMatrix(A Matrix, B Matrix, C Matrix, D Matrix) (M Matrix) {
var M Matrix
// Something here
// Filled with A, B, C, and D
return M, nil
}
Any suggestions to fill the matrix M with matrices A, B, C and D?
For simplicity I'll assume Matrix is a square (n*n) [][]int:
package main
import "fmt"
type Matrix [][]int
func Block(a, b, c, d Matrix) Matrix {
l := len(a)
s := l * 2
m := make([][]int, s)
for i := 0; i < s; i++ {
m[i] = make([]int, s)
}
copy(a, m, 0, 0)
copy(b, m, 0, l)
copy(c, m, l, 0)
copy(d, m, l, l)
return m
}
func copy(a, m Matrix, x, y int) {
for i := 0; i < len(a); i++ {
for j := 0; j < len(a[i]); j++ {
m[i+x][j+y] = a[i][j]
}
}
}
func main() {
a := Matrix{{1, 2}, {3, 4}}
b := Matrix{{5, 6}, {7, 8}}
c := Matrix{{9, 10}, {11, 12}}
d := Matrix{{13, 14}, {15, 16}}
m := Block(a, b, c, d)
fmt.Printf("a: %+v\n", a)
fmt.Printf("b: %+v\n", b)
fmt.Printf("c: %+v\n", c)
fmt.Printf("d: %+v\n", d)
fmt.Printf("m: %+v\n", m)
}
Running that prints:
a: [[1 2] [3 4]]
b: [[5 6] [7 8]]
c: [[9 10] [11 12]]
d: [[13 14] [15 16]]
m: [[1 2 5 6] [3 4 7 8] [9 10 13 14] [11 12 15 16]]
Which I believe is what you want.
Related
While I was trying to solve a problem "Subset II" from LC, I came across a strange problem. The code generates a power set from a given set.
However, when I run the code it failed because one of the set wasn't correct.
The set [0,3,5,7] replaced by [0,3,5,9] (hence gets appended twice).
I have a print statement (highlighted in code) right before a set gets appended to res, and it prints the correct power set.
The only issue I could think is the use of pointers to append values into a slice, however since it's does not run concurrently I don't see why there would be a race condition.
Appreciate if someone can point out my mistake.
package main
import (
"fmt"
"sort"
)
func ValueCount( nums []int) map[int]int{
hm := make(map[int]int)
for _,v := range(nums){
if c, ok := hm[v]; ok {
hm[v] = c + 1
}else{
hm[v] = 1
}
}
return hm
}
func subsetsWithDup(nums []int) [][]int {
var res [][]int
res = append(res,[]int{})
sort.Ints(nums)
hashMap := ValueCount(nums)
var t []int
printTest(nums, t, &res, hashMap)
return res
}
func printTest(nums []int, t []int, res *[][]int, hm map[int]int) {
if len(nums) == 0 {
return
}
for i:= 0; i < len(nums); {
v := nums[i]
x := nums[i:]
for k:= 0; k< hm[v]; k++ {
var a,b []int
for z:= 0; z<k+1; z++ {
a = append(t,x[z])
}
fmt.Println(a) // <--------- Prints the values that gets appended to res
*res = append(*res, a)
b = a
printTest(nums[i+hm[v]:], b, res, hm)
}
i += hm[v]
}
}
func main(){
n := []int{9,0,3,5,7}
fmt.Println("Find the power set of:", n)
fmt.Println(subsetsWithDup(n))
}
// [0,3,5,7] changes to
// [0,3,5,9] in the output
The bug occurs on line 40:
a = append(t, x[z])
A quick fix would be to change this for loop:
for k := 0; k < hm[v]; k++ {
var a, b []int
for z := 0; z < k+1; z++ {
a = append(t, x[z])
}
fmt.Println(a) // <--------- Prints the values that gets appended to res
*res = append(*res, a)
b = a
printTest(nums[i+hm[v]:], b, res, hm)
}
To this:
for k := 0; k < hm[v]; k++ {
var a, b []int
a = make([]int, len(t))
copy(a, t)
for z := 0; z < k+1; z++ {
a = append(a, x[z])
}
fmt.Println(a) // <--------- Prints the values that gets appended to res
*res = append(*res, a)
b = a
printTest(nums[i+hm[v]:], b, res, hm)
}
It has to do with how Go uses slices as a data structure. When the first argument to the built-in append function was a slice argument, it copied some of the slice's internal data that wasn't intuitive to the programmer. It then modified the argument slice, t, and the newly created slice, a.
I'd recommend reading up on slice internals if you're interested in learning more.
Full program edited:
package main
import (
"fmt"
"sort"
)
func ValueCount(nums []int) map[int]int {
hm := make(map[int]int)
for _, v := range nums {
if c, ok := hm[v]; ok {
hm[v] = c + 1
} else {
hm[v] = 1
}
}
return hm
}
func subsetsWithDup(nums []int) [][]int {
var res [][]int
res = append(res, []int{})
sort.Ints(nums)
hashMap := ValueCount(nums)
var t []int
printTest(nums, t, &res, hashMap)
return res
}
func printTest(nums []int, t []int, res *[][]int, hm map[int]int) {
if len(nums) == 0 {
return
}
for i := 0; i < len(nums); {
v := nums[i]
x := nums[i:]
for k := 0; k < hm[v]; k++ {
var a, b []int
a = make([]int, len(t))
copy(a, t)
for z := 0; z < k+1; z++ {
a = append(a, x[z])
}
fmt.Println(a) // <--------- Prints the values that gets appended to res
*res = append(*res, a)
b = a
printTest(nums[i+hm[v]:], b, res, hm)
}
i += hm[v]
}
}
func main() {
n := []int{9, 0, 3, 5, 7}
fmt.Println("Find the power set of:", n)
fmt.Println(subsetsWithDup(n))
}
New output:
Find the power set of: [9 0 3 5 7]
[0]
[0 3]
[0 3 5]
[0 3 5 7]
[0 3 5 7 9]
[0 3 5 9]
[0 3 7]
[0 3 7 9]
[0 3 9]
[0 5]
[0 5 7]
[0 5 7 9]
[0 5 9]
[0 7]
[0 7 9]
[0 9]
[3]
[3 5]
[3 5 7]
[3 5 7 9]
[3 5 9]
[3 7]
[3 7 9]
[3 9]
[5]
[5 7]
[5 7 9]
[5 9]
[7]
[7 9]
[9]
[[] [0] [0 3] [0 3 5] [0 3 5 7] [0 3 5 7 9] [0 3 5 9] [0 3 7] [0 3 7 9] [0 3 9] [0 5] [0 5 7] [0 5 7 9] [0 5 9] [0 7] [0 7 9] [0 9] [3] [3 5] [3 5 7] [3 5 7 9] [3 5 9] [3 7] [3 7 9] [3 9] [5] [5 7] [5 7 9] [5 9] [7] [7 9] [9]]
Be very careful using (and reusing) slice results - especially when altering those slice values later. Since slices have backing arrays, the referenced data can change in very unexpected ways!
A quick fix to your problem is to copy slice results to a new slice. This ensures changes to the original slice do not introduce bugs (especially in a recursive algorithm).
To copy a slice:
func copyIntSlice(a []int) []int {
c := make([]int, len(a))
copy(c, a) // `a` can now grow/shrink/change without affecting `c`
return c
}
and just call this from your main code:
aCopy := copyIntSlice(a)
*res = append(*res, aCopy)
printTest(nums[i+hm[v]:], aCopy, res, hm)
https://play.golang.org/p/1p8Z4sV9foQ
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
}
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
Is it possible to swap elements like in python?
a,b = b,a
or do we have to use:
temp = a
a = b
b = temp
Yes, it is possible. Assuming a and b have the same type, the example provided will work just fine. For example:
a, b := "second", "first"
fmt.Println(a, b) // Prints "second first"
b, a = a, b
fmt.Println(a, b) // Prints "first second"
Run sample on the playground
This is both legal and idiomatic, so there's no need to use an intermediary buffer.
Yes it is possible to swap elements using multi-value assignments:
i := []int{1, 2, 3, 4}
fmt.Println(i)
i[0], i[1] = i[1], i[0]
fmt.Println(i)
a, b := 1, 2
fmt.Println(a, b)
a, b = b, a // note the lack of ':' since no new variables are being created
fmt.Println(a, b)
Output:
[1 2 3 4]
[2 1 3 4]
1 2
2 1
Example: https://play.golang.org/p/sopFxCqwM1
More details here: https://golang.org/ref/spec#Assignments
Yes you can swap by using
a, b = b, a
So if a = 1 and b= 2,
then after executing
a , b = b, a
you get a = 2 and b = 1
Also, if you write
a, b, a = b, a, b
then it results b = 1 and a = 2
There is a function called Swapper which takes a slice and returns a swap function. This swap function takes 2 indexes and swap the index values in the slice.
package main
import (
"fmt"
"reflect"
)
func main() {
s := []int{1, 2, 3}
fmt.Printf("Before swap: %v\n", s)
swapF := reflect.Swapper(s)
swapF(0, 1)
fmt.Printf("After swap: %v\n", s)
}
Try it
Output
Before swap: [1 2 3]
After swap: [2 1 3]
Yes, you can swap values like python.
a, b := 0, 1
fmt.Printf("Before swap a = %v, b = %v\n", a, b)
b, a = a, b
fmt.Printf("After swap a = %v, b = %v\n", a, b)
Output
Before swap a = 0, b = 1
After swap a = 1, b = 0
you can use ^ option like this...
func swap(nums []int, i, j int) {
nums[i] ^= nums[j]
nums[j] ^= nums[i]
nums[i] ^= nums[j]
}
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
}