dsoihsnz85757 2014-08-23 07:20
浏览 57
已采纳

巡回演习练习逻辑

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条回答 默认 最新

  • duanmu5039 2014-08-23 07:26
    关注

    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[x] 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):

    res

    As rwilson04 comments below:

    x*x + y*y is another good one

    x square plus y square

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)