I want to convert a variable (I took an int for this example) to a byte using this code that I have found:
func IntToByteArray(num int64) []byte {
size := int(unsafe.Sizeof(num))
arr := make([]byte, size)
for i := 0 ; i < size ; i++ {
byt := *(*uint32)(unsafe.Pointer(uintptr(unsafe.Pointer(&num)) + uintptr(i)))
arr[i] = byte(byt)
}
return arr
}
func main(){
println(IntToByteArray(1456))
}
But the output that it gives me is this one : [8/8]0xc00001a0d0
Can some one explain me why do I have this has a result?
And what is exactly a byte array?
package main
import "fmt"
func IntToByteArray(num int64) []byte {
r := make([]byte, 8)
for i := 0; i < len(r); i++ {
r[i] = byte(num >> (i * 8) & 255)
}
return r
}
func main() {
fmt.Println(IntToByteArray(65280))
}
This assumes little-endianness.
As others have suggested, the included packages are more flexible and tested.
I have encountered the following problem that I am attempting to solve:
In a N x N grid representing a field of cherries, each cell is one of three possible integers.
0 means the cell is empty, so you can pass through;
1 means the cell contains a cherry, that you can pick up and pass through;
-1 means the cell contains a thorn that blocks your way.
Your task is to collect maximum number of cherries possible by following the rules below:
Starting at the position (0, 0) and reaching (N-1, N-1) by moving right or down through valid path cells (cells with > value 0 or 1);
After reaching (N-1, N-1), returning to (0, 0) by moving left or up through valid path cells;
When passing through a path cell containing a cherry, you pick it up and the cell becomes an empty cell (0);
If there is no valid path between (0, 0) and (N-1, N-1), then no cherries can be collected.
Example 1:
Input: grid =
[[0, 1, -1],
[1, 0, -1],
[1, 1, 1]]
Output: 5
Explanation:
The player started at (0, 0) and went down, down, right right to reach (2, 2).
4 cherries were picked up during this single trip, and the matrix becomes [[0,1,-1],[0,0,-1],[0,0,0]].
Then, the player went left, up, up, left to return home, picking up one more cherry.
The total number of cherries picked up is 5, and this is the maximum possible.
Note:
grid is an N by N 2D array, with 1 <= N <= 50.
Each grid[i][j] is an integer in the set {-1, 0, 1}.
It is guaranteed that grid[0][0] and grid[N-1][N-1] are not -1.
So I need to write a function cherryPickup that takes a grid and returns the maximum score.
My first suboptimal attempt (written in Go) is the following, which supposedly would try to walk every possible path, storing the score in a slice upon round-trip path completion, and then return the greatest score present in the slice:
func cherryPickup(grid [][]int) int {
values := []int{}
pVals := &values
finalPoints := 0
// Begin top-down path
traverseAndCollectTopDown(grid, 0, 0, 0, pVals)
// Find max value in slice
for i, pathPoints := range values {
if i == 0 || pathPoints > finalPoints {
finalPoints = pathPoints
}
}
return finalPoints
}
func isTraversable(grid [][]int, x, y int) bool {
return (grid[x][y] != -1)
}
func isOnBounds(grid [][]int, x, y int) bool {
return (x < len(grid) && y < len(grid[0]) && x >= 0 && y >= 0)
}
func traverseAndCollectTopDown(grid [][]int, x, y, points int, vals *[]int) {
// Collect point before continuing
if grid[x][y] == 1 {
grid[x][y] = 0
points++
}
// If reached bottom, begin bottom-up path
if (x == len(grid)-1) && (y == len(grid[0])-1) {
traverseAndCollectBottomUp(grid, x, y, points, vals)
return
}
// Go Down
if isOnBounds(grid, x+1, y) && isTraversable(grid, x+1, y) {
traverseAndCollectTopDown(grid, x+1, y, points, vals)
}
// Go Right
if isOnBounds(grid, x, y+1) && isTraversable(grid, x, y+1) {
traverseAndCollectTopDown(grid, x, y+1, points, vals)
}
}
func traverseAndCollectBottomUp(grid [][]int, x, y, points int, vals *[]int) {
if grid[x][y] == 1 {
grid[x][y] = 0
points++
}
if x == 0 && y == 0 {
*vals = append(*vals, points)
return
}
// Go Up
if isOnBounds(grid, x-1, y) && isTraversable(grid, x-1, y) {
traverseAndCollectBottomUp(grid, x-1, y, points, vals)
}
// Go Left
if isOnBounds(grid, x, y-1) && isTraversable(grid, x, y-1) {
traverseAndCollectBottomUp(grid, x, y-1, points, vals)
}
}
Currently it passes a bunch of tests, but this one is failing and I don't know why.
Input: [[1,1,1,1,0,0,0],[0,0,0,1,0,0,0],[0,0,0,1,0,0,1],[1,0,0,1,0,0,0],[0,0,0,1,0,0,0],[0,0,0,1,0,0,0],[0,0,0,1,1,1,1]]
Output: 10
Expected: 15
I get the way this grid must score the 15 points, however, why does my code fail to travel the winning path and only scores 10? Also, do you recommend any terminal utilities, programs or strategies to help visualize better what is happening on every run?
Thanks in advance!
The parameter you are using to traverseAndCollectTopDown / traverseAndCollectBottomUp is a grid [][]int. You are modifying that and then passing it directly into other functions (recursively). In go a slice is effectively passed by reference meaning that when one of your routines edits the slice this impacts the slice held by all of the other routines too (so as soon as one path finds a '1' it gets removed and another path going through the same cell will find a '0' there).
To fix this take a copy of grid before making a recursive call e.g. call grid = copyGrid(grid) in traverseAndCollectTopDown / traverseAndCollectBottomUp before modifying grid.
func copyGrid(in [][]int) [][]int {
duplicate := make([][]int, len(in))
for i := range in {
duplicate[i] = make([]int, len(in[i]))
copy(duplicate[i], in[i])
}
return duplicate
}
I try to implement a function taking (any) structure, returning an array of those structures. ReturnArrayOfStory show the idea with a fixed type struct type.
trying to do the same for any type with function ReturnArrayOfX and reflection fails at compile time.
package main
import (
"fmt"
"reflect"
)
type story_t struct {
LANGUAGE string
SPECIES string
}
func ReturnArrayOfStory(x story_t) []story_t {
x1 := x
var a1 []story_t
a1 = append(a1, x1)
a1 = append(a1, x1)
a1 = append(a1, x1)
return a1
}
func ReturnArrayOfX(x interface{}) []interface{} {
x1 := x
v1 := reflect.ValueOf(&x1).Elem()
a1 := []reflect.TypeOf(&x1)
// var a1 []x
a1 = append(a1, x1)
a1 = append(a1, x1)
a1 = append(a1, x1)
//return a1
return a1
}
func main() {
var as1 []story_t
s1 := story_t{"EN", "Prince of Persia"}
as1 = ReturnArrayOfStory(s1)
//as1 := ReturnArrayOfX(s1)
for i := 0; i < len(as1); i++ {
fmt.Printf("%02d %+v\n", i, as1[i])
}
as2 := ReturnArrayOfX(s1)
//as1 := ReturnArrayOfX(s1)
for i := 0; i < len(as2); i++ {
fmt.Printf("%02d %+v\n", i, as2[i])
}
}
a1 := []reflect.TypeOf(&x1)
main.go:25:8: reflect.TypeOf is not a type
This is a simplified scenario. In reality, I like to read a multitude of struct types from an external data source like a database.
How can I came to my goal with ReturnArrayOfX?
List item Is this possible? If not,why?
There are two solutions to your problem:
First: if you want to return a slice of a type using reflection:
// You cannot return []interface{}, because this function will return [](type of x), and that is not []interface{}
func ReturnArrayOfX(x interface{}) interface{} {
x1 := x
a1 :=
// this creates *[](typeOf x)
reflect.New(reflect.SliceOf(reflect.TypeOf(x)))
// Append the first element to *[](typeof x)
// after this, a1 now points to a slice, not to a slice *
a1 = reflect.Append(a1.Elem(), reflect.ValueOf(x1))
a1 = reflect.Append(a1, reflect.ValueOf(x1))
a1 = reflect.Append(a1, reflect.ValueOf(x1))
//return [](typeof x)
return a1.Interface()
}
You can use this as:
as2 := ReturnArrayOfX(s1)
arr:=as2.([]story_t)
for i := 0; i < len(arr); i++ {
fmt.Printf("%02d %+v\n", i, arr[i])
}
Second: you can return []interface{} without reflection:
func ReturnArrayOfX(x interface{}) []interface{} {
ret:=make([]interface{},0)
ret=append(ret,x)
ret=append(ret,x)
ret=append(ret,x)
}
Then you need to deal with each element of the array:
as2 := ReturnArrayOfX(s1)
for i := 0; i < len(as2); i++ {
fmt.Printf("%02d %+v\n", i, as2[i])
data:=as2[i].(story_t)
}
Here's a generic slice conversion function:
// convertSlice copies the slice in src to the slice pointed to by pdst.
// The concrete values in src must be assignable to the dst elements.
func convertSlice(pdst interface{}, src interface{}) {
dstv := reflect.ValueOf(pdst).Elem()
srcv := reflect.ValueOf(src)
dstv.Set(reflect.MakeSlice(dstv.Type(), srcv.Len(), srcv.Len()))
for i := 0; i < srcv.Len(); i++ {
dstv.Index(i).Set(reflect.ValueOf(srcv.Index(i).Interface()))
}
}
Use it like this:
// Convert []story_t to []interface{}
s0 := []story_t{{"EN", "Prince of Persia"}, {"EN", "Karateka"}}
var s1 []interface{}
convertSlice(&s1, s0)
// Convert []interface{} containing story_t to []story_t
var s2 []story_t
convertSlice(&s2, s1)
Run it on the playground.
So my friend gave me this task where the sum of squares of positive numbers must be calculated using recursion.
Conditions - The input will be a string with space separated numbers
This is what I've come so far but this shows a runtime error.
Here is the full error https://ideone.com/53oOjN
package main
import(
'fmt',
'strings',
'strconv'
)
var n int = 4
var sum_of_squares int = 0
func sumOfSquares(strArray []string, iterate int) int{
number, _ := strconv.Atoi(strArray[iterate])
if number > 0 {
sum_of_squares += number*number
}
if iterate == n {
return 0 // just to end the recursion
}
return sumOfSquares(strArray, iterate+1)
}
func main() {
str := "1 2 3 4"
strArray := strings.Fields(str)
result := sumOfSquares(strArray, 0)
fmt.Println(sum_of_squares, result)
}
The rule of thumb in recursion is termination condition. It should exist and it should exist in the right place.
func sumOfSquares(strArray []string, iterate int) int{
if iterate >= len(strArray) {
return sum_of_squares
}
number, _ := strconv.Atoi(strArray[iterate]) //TODO: handle err here
sum_of_squares += number*number
return sumOfSquares(strArray, iterate+1)
}
Just for you information: canonical recursion should not save it's state into global fields. I would suggest using following function signature.
func sumOfSquares(strArray []string, iterate, currentSum int) int{
//...
return sumOfSquares(strArray, iterate+1, sum_of_squares)
}
So that you don't need to store sum_of_squares somewhere. You will just pass it to next function invocation.
package main
import (
"fmt"
"strconv"
"strings"
)
var n int
func sumOfSquares(strArray []string, iterate int) int {
number, _ := strconv.Atoi(strArray[iterate])
if iterate == n {
return number * number
}
return ((number * number) + sumOfSquares(strArray, iterate+1))
}
func main() {
str := "1 2 3 4"
strArray := strings.Fields(str)
n = len(strArray) - 1
result := sumOfSquares(strArray, 0)
fmt.Println(result)
}
Indexing starts from 0, so decrease the length by one.
As #peterSO have pointed out, if strings contain unusual characters, it doesn't work, I didn't post the right answer for getting input because you seem to be beginner, but you can read the input, like this instead.
var inp []byte
var loc int
inp, _ = ioutil.ReadFile(fileName)
//add \n so that we don't end up running out of bounds,
//if last byte is integer.
inp = append(inp, '\n')
func scanInt() (res int) {
if loc < len(inp) {
for ; inp[loc] < 48 || inp[loc] > 57; loc++ {
}
for ; inp[loc] > 47 && inp[loc] < 58; loc++ {
res = res<<3 + res<<1 + (int(inp[loc]) - 48)
}
}
return
}
This is faster and scans integers only, and skips all other unusual characters.
I like to keep it simple. I have some few if conditions as well, but hope you like it.
func sumOfSquares(numArr []string) int {
i, err := strconv.Atoi(numArr[0])
rest := numArr[1:]
//Error checking
if err != nil {
fmt.Println(err)
os.Exit(1)
return 0
}
square := i * i
// negative & last number
if i < 0 && len(rest) == 0 {
return square
}
// negative & not last number
if i < 0 && len(rest) > 0 {
return sumOfSquares(rest)
}
// last man standing
if i >= 0 && len(rest) == 0 {
return square
}
return square + sumOfSquares(rest)
}
DEMO : https://play.golang.org/p/WWYxKbvzanJ
I was doing the exercise of "A Tour of Go", the page I was on is https://tour.golang.org/moretypes/15
And following is my code:
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
var ret [][]uint8;
var row []uint8;
for i:=uint8(0);i<uint8(dy);i++ {
row = []uint8 {}
for j:=uint8(0);j<uint8(dx);j++ {
row = append(row, i+j)
}
ret = append(ret, row)
}
return ret
}
func main() {
pic.Show(Pic)
}
When I run these codes, the console throws an error:
panic: runtime error: index out of range
goroutine 1 [running]:
panic(0x18b820, 0x1040a010)
/usr/local/go/src/runtime/panic.go:464 +0x700
golang.org/x/tour/pic.Show(0x1d7948, 0x104000e0)
/go/src/golang.org/x/tour/pic/pic.go:24 +0x540
main.main()
/tmp/sandbox969725880/main.go:19 +0x20
Does anyone have any ideas why does the error occur for this int->uint8 type conversion?Thanks!
uint8 has a max value of 255 (only 8 bits, max 2^8) but dx, dy passed into Pic can have values greater than that (since they're int's, likely 64 bits). Values greater than 255 might get cast to 0 during conversion to uint8. If dy is 256 and it's cast to 0 as i, the outer for loop doesn't execute at all and no items get pushed into the array. Subsequently, when whatever mechanism in "golang.org/x/tour/pic" tries to access the values in the matrix after it's returned, it looks like it's generating an 'index out of range' error because there are literally no indexes in the matrix to access.
Working code:
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
var ret [][]uint8
for i := 0; i < dy; i++ {
row := []uint8{}
for j := 0; j < dx; j++ {
row = append(row, uint8(i+j))
}
ret = append(ret, row)
}
return ret
}
func main() {
pic.Show(Pic)
}