Created
January 6, 2014 23:25
-
-
Save dnorton/8291832 to your computer and use it in GitHub Desktop.
simple solution to Go Tour Slice Exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/** | |
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, (x+y)/2, and x*y. | |
(You need to use a loop to allocate each []uint8 inside the [][]uint8.) | |
(Use uint8(intValue) to convert between types.) | |
http://tour.golang.org/#38 | |
**/ | |
import "code.google.com/p/go-tour/pic" | |
func Pic(dx, dy int) [][]uint8 { | |
pic_array := make([][]uint8, dy) | |
for i := 0; i < len(pic_array); i++ { | |
pic_array[i] = make([]uint8, dx) | |
for j := range pic_array[i] { | |
pic_array[i][j] = uint8(i ^ j) | |
} | |
} | |
return pic_array | |
} | |
func main() { | |
pic.Show(Pic) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TIL: because of proxy issues, I couldn't download go-tour/pic, so I downloaded the zip and unpacked into
$GOPATH/src/code.google.com/p/go-tour/