I am trying to write a simple function that transposes a square matrix (i.e. swaps the columns and rows). Here is the function in question:
func Transpose(a [][]bool) {
for i := 0; i < len(a); i++ {
for j := 0; j < len(a[i]); j++ {
a[i][j], a[j][i] = a[j][i], a[i][j]
}
}
}
It doesn't seem to work. If I run this function
func TestTranspose(t *testing.T) {
a := make([][]bool, 3)
a[0] = []bool{true, true, true}
a[1] = []bool{false, true, false}
a[2] = []bool{true, true, true}
fmt.Println(BoolArrayViz(a))
Transpose(a)
fmt.Println(BoolArrayViz(a))
}
It gives the following output (BoolArrayViz simply prints the bool as '*' if true ' ' if false):
***
*
***
***
*
***
I believe it has something to do with pointers and the fact that I'm passing around slices. Any help is appreciated!