为什么我的解决方案在Go切片练习中是错误的?

huangapple go评论75阅读模式
英文:

Why is my solution incorrect for tour of go slices exercise?

问题

我正在学习Go语言,并尝试完成Go之旅。我在切片的练习上遇到了困难。我将问题和我的解决方案复制粘贴在这里。有人可以批评一下并告诉我我在这里做错了什么吗?

问题:

实现Pic函数。它应该返回一个长度为dy的切片,其中每个元素都是一个长度为dx的8位无符号整数切片。当你运行程序时,它将显示你的图片,将整数解释为灰度(或蓝度)值。

图片的选择由你决定。有趣的函数包括(x+y)/2、x*y和x^y。

(你需要使用循环来分配[][]uint8中的每个[]uint8。)

(使用uint8(intValue)来在类型之间进行转换。)

我的解决方案:

package main

import "golang.org/x/tour/pic"

func Pic(dx, dy int) [][]uint8 {
	ans := make([][]uint8, dy)
	for i:=0; i<dy; i++ {
		slice := make([]uint8, dx)
		for j := 0; j<dx;j++{
			slice = append(slice, uint8((i+j)/2))
		}
		ans = append(ans,slice)
	}
	return ans
}

func main() {
	pic.Show(Pic)
}

运行时我得到了错误:

panic: runtime error: index out of range [0] with length 0

我不确定我在这里做错了什么。还有,为什么在练习中传递一个函数?这是有意的吗?

英文:

I am learning golang and trying to finish the tour of go. I am stuck on the exercise for slices. Copy pasting the question and my solution here. Can someone critique it and tell me what I am doing incorrectly here?

Question:

Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit 
unsigned integers. When you run the program, it will display your picture,
interpreting the integers as grayscale (well, bluescale) values.

The choice of image is up to you. Interesting functions include (x+y)/2, x*y, and x^y.

(You need to use a loop to allocate each []uint8 inside the [][]uint8.)

(Use uint8(intValue) to convert between types.)

My Solution:

package main

import &quot;golang.org/x/tour/pic&quot;

func Pic(dx, dy int) [][]uint8 {
	ans := make([][]uint8, dy)
	for i:=0; i&lt; dy; i++ {
		slice := make([]uint8, dx)
		for j := 0; j&lt;dx;j++{
			slice = append(slice, uint8((i+j)/2))
		}
		ans = append(ans,slice)
	}
	return ans
}

func main() {
	pic.Show(Pic)
}

Upon running I get the error:

> panic: runtime error: index out of range [0] with length 0

I am not sure what I am doing wrong here. Also, why is there a function being passed in the exercise? Is this intended?

答案1

得分: 2

好的,以下是翻译好的内容:

好的,我明白了。正如我在评论中所说的,你应该将你的append调用替换为slice[j] = uint((i+j)/2)ans[i] = slice

这个练习调用了一个大小为256x256的函数。你创建了一个长度为256的切片,然后追加了256次其他切片,结果得到一个长度为512的切片ans。前256个条目是空的,因为append将slice追加到末尾。因此,当pic库迭代你的数据时,它尝试访问一个空的切片。

更新:
修复算法的另一种方法是用长度为0初始化切片。因此,编辑

ans := make([][]uint8, 0)
slice := make([]uint8, 0)

也应该得到正确的结果。

英文:

Ok I got it. As I said in my comment, you should replace your append calls by slice[j] = uint((i+j)/2) and ans[i] = slice.

The exercise calls your function with 256x256. You create a slice that is 256 long and then append other slices 256 times, resulting in a 512 long slice ans. The first 256 entries are empty, since append appends slice at the end. Therefore when the pic library iterates your data, it tries to access an empty slice.

Update:
Another way to fix the algorithm is to initialize the slices with length of 0. So editing

ans := make([][]uint8, 0) and
slice := make([]uint8, 0)

should also give the correct results.

huangapple
  • 本文由 发表于 2021年10月31日 06:37:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/69782655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定