@qiezhian
2014-11-26T16:14:21.000000Z
字数 3029
阅读 1391
Go
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.)
package main
import "code.google.com/p/go-tour/pic"
func Pic(dx, dy int) [][]uint8 {
result := make([][] uint8,dy)
for i := range result {
result[i] = make([] uint8, dx)
}
for i := 0; i<dy; i++ {
for j:=0; j<dx; j++ {
result[i][j] = uint8(i+j)
}
}
return result
}
func main() {
pic.Show(Pic)
}
Implement WordCount. It should return a map of the counts of each “word” in the string s. The wc.Test function runs a test suite against the provided function and prints success or failure.
package main
import (
"code.google.com/p/go-tour/wc"
)
func WordCount(s string) map[string]int {
m := make( map[string]int )
length := len(s)
var word string
for i,j := 0,0; j<length; j++ {
if s[j] == ' ' {
word = s[i:j]
i = j+1
m[word]++
} else if j==length-1 {
word = s[i:j+1]
m[word]++
}
}
return m
//return map[string]int{"x": 1}
}
func main() {
wc.Test(WordCount)
}
Let's have some fun with functions.
Implement a fibonacci function that returns a function (a closure) that returns successive fibonacci numbers.
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
a := 0
b := 1
return func() int {
tmp := a + b
a = b
b = tmp
return a
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
Let's explore Go's built-in support for complex numbers via the complex64 and complex128 types. For cube roots, Newton's method amounts to repeating:
Find the cube root of 2, just to make sure the algorithm works. There is a Pow function in the math/cmplx package.
package main
import ( "fmt";"math/cmplx" )
func Cbrt(x complex128) complex128 {
z := x
y := z - ( cmplx.Pow( z, 3 ) - x)/(3*cmplx.Pow( z, 2 ) )
for i := 0; i<100; i++ {
z = y
y = z - ( cmplx.Pow( z, 3 ) - x)/(3*cmplx.Pow( z, 2 ) )
}
return y
}
func main() {
fmt.Println(Cbrt(2))
}
Copy your Sqrt function from the earlier exercises and modify it to return an error value.
Sqrt should return a non-nil error value when given a negative number, as it doesn't support complex numbers.
Create a new type
type ErrNegativeSqrt float64
and make it an error by giving it a
func (e ErrNegativeSqrt) Error() string
method such that ErrNegativeSqrt(-2).Error() returns "cannot Sqrt negative number: -2".
Note: a call to fmt.Print(e) inside the Error method will send the program into an infinite loop. You can avoid this by converting e first: fmt.Print(float64(e)). Why?
Change your Sqrt function to return an ErrNegativeSqrt value when given a negative number.
package main
import (
"fmt"
"math"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt ) Error() string {
return fmt.Sprintf( "cannot Sqrt negative number: %f",
float64(e) )
}
func Sqrt(f float64) (float64, error) {
var y float64 = 0
var e error = nil
if f < 0 {
e = ErrNegativeSqrt( f )
} else {
z := f
y = z - ( math.Pow( z, 2 ) - f)/(2*z)
for i := 0; i<100; i++ {
z = y
y = z - ( math.Pow( z, 2 ) - f)/(2*z)
}
}
return y, e
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(Sqrt(-2))
}