Go语言之旅练习#18:切片

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

Tour of Go exercise #18: Slices

问题

我正在尝试完成Go Tour中的“切片练习”。然而,我不太理解要我做什么。

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

我有以下代码:

package main

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

func Pic(dx, dy int) [][]uint8 {
    a := make([][]uint8, dy)
    return a
}

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

我已经创建了长度为dy的切片。然而,我不理解下一步。我需要创建一个循环,将我的切片的每个元素赋值为dx范围内的值吗?我不是要求代码,而是要求解释/澄清。

英文:

I am trying to complete the Exercise: Slices from the Go Tour.
However I don't really understand what I'm being asked to do.

> 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.

I have the following code

package main

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

func Pic(dx, dy int) [][]uint8 {
    a := make([][]uint8, dy)
    return a
}

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

I have created the slice of length dy so far. However I don't understand the next step. Do I need to create a for loop in which I assign every element of my slice to a value in the range of dx? I don't ask for code but rather for an explanation/clarification

答案1

得分: 25

我需要在一个循环中为每个切片元素分配一个dx范围内的值吗?

是的:

  • 外部循环用于将a 分配为大小为dx的[]uint8切片,
  • 内部循环 'y' 用于为每个元素a (即[]uint8)分配所需的值(例如,其中一个“有趣的函数”如 x^y(x+y)/2x*y)。
    xy 是在切片(a,然后a )上的范围内的索引:参见“For语句”。

我喜欢 (x ^ y) * (x ^ y)

Go语言之旅练习#18:切片

正如rwilson04在下面的评论中所说:

x*x + y*y 也是一个不错的选择

Go语言之旅练习#18:切片

Waqas Ilyas评论中建议使用 y * 10000 / (x + 1)

Go语言之旅练习#18:切片

英文:

> Do I need to create a for loop in which I assign every element of my slice to a value in the range of dx?

Yes:

  • an outer loop to assign a with a []uint8 slice of dx size,
  • with an inner loop 'y' for each element a[x] (which is a []uint8) in order to assign a[x][y] with the asked value (ie, one of the "interesting functions" like x^y, (x+y)/2, and x*y).
    x and y are indexes in a range over a slice (a, then a[x]): see "For statements".

I like (x ^ y) * (x ^ y):

Go语言之旅练习#18:切片

As rwilson04 comments below:

> x*x + y*y is another good one

Go语言之旅练习#18:切片

Waqas Ilyas suggests in the comments: y * 10000 / (x + 1)

Go语言之旅练习#18:切片

答案2

得分: 10

请在这里找到两种方法的建议。

package main

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

func Pic(dx, dy int) [][]uint8 {
    p := make([][]uint8, dy)
    for y := range p {
        p[y] = make([]uint8, dx)
        for x := range p[y] {
            p[y][x] = uint8(x^y)
            // p[y][x] = uint8(x*y)
            // p[y][x] = uint8((x+y)/2)
        }
    }
    return p
}

func _Pic(dx, dy int) [][]uint8 {
    p := make([][]uint8, dy)
    for i := 0; i < dy; i++ {
        p[i] = make([]uint8, dx)
    }
    for y := range p {
        for x := range p[y] {
            p[y][x] = uint8(x^y)
            // p[y][x] = uint8(x*y)
            // p[y][x] = uint8((x+y)/2)
        }
    }
    return p
}

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

这是一个使用Go语言编写的程序,其中定义了两个函数Pic_Pic,用于生成一个二维切片,表示一个图像。这个图像的大小由输入参数dxdy决定。在这个程序中,使用了三种不同的方法来生成图像的像素值:

  1. p[y][x] = uint8(x^y):使用位运算异或操作生成像素值。
  2. p[y][x] = uint8(x*y):使用乘法操作生成像素值。
  3. p[y][x] = uint8((x+y)/2):使用加法和除法操作生成像素值。

main函数中,调用pic.Show函数来显示生成的图像。

英文:

Please find here a suggestion with two approaches.

package main

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

func Pic(dx, dy int) [][]uint8 {
    p := make([][]uint8, dy)
    for y := range p {
        p[y] = make([]uint8, dx)
        for x := range p[y] {
		p[y][x] = uint8(x^y)
		// p[y][x] = uint8(x*y)
		// p[y][x] = uint8((x+y)/2)
        }
    }
    return p
}

func _Pic(dx, dy int) [][]uint8 {
	p := make([][]uint8, dy)
	for i := 0; i &lt; dy; i++ {
		p[i] = make([]uint8, dx)
	}
	for y := range p {
		for x := range p[y] {
			p[y][x] = uint8(x^y)
			// p[y][x] = uint8(x*y)
			// p[y][x] = uint8((x+y)/2)
		}
	}
	return p
}

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

huangapple
  • 本文由 发表于 2014年8月23日 15:20:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/25459474.html
匿名

发表评论

匿名网友

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

确定