I need to compare 1D array's and 2D array's values and to return the result array which has similar values using Go:
func absolutePermutation(n int32, k int32) []int32 {
var position []int32
var i, j int32
var result []int32
for i = 1; i <= n; i++ {
position[i-1] = i
}
x := getPermutations(position)
resVal := permNum(n)
fmt.Println(x)
for i = 0; i < resVal; i++ {
for j = 0; j < 4; j++ {
fmt.Println(x[i][j])
**if int32(math.Abs(float64(position[(j*resVal)+i])-float64(x[i][j]))) == k** {
result[i] = x[i][j]
} else {
continue
}
}
}
return result
}
func getPermutations(elements []int32) [][]int32 {
permutations := [][]int32{}
if len(elements) == 1 {
permutations = [][]int32{elements}
return permutations
}
for i := range elements {
el := make([]int32, len(elements))
copy(el, elements)
for _, perm := range getPermutations(append(el[0:i], el[i+1:]...)) {
permutations = append(permutations, append([]int32{elements[i]}, perm...))
}
}
return permutations
}
func permNum (n int32) int32 {
if n == 0 {
return 1
}
return n * permNum(n-1)
}
I tried to create position (1D array into 2D array here.) but it didn't make sense. As a newbie to GoLang, I get this error in this code which is written to get the permutation of a given number:
runtime error: index out of range [0] with length 0