Forked from wuxiao356/Solutions for A Tour of Go Exercises
Created
January 9, 2017 03:57
-
-
Save xujiajun/6442afef7622f201060b188750ce8d2f to your computer and use it in GitHub Desktop.
A Tour of Go: Exercises Solution
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
| //Exercise: Loops and Functions | |
| package main | |
| import ( | |
| "fmt" | |
| "math" | |
| ) | |
| const delta = 0.000000000001 | |
| func Sqrt(x float64) (value float64, iteration int) { | |
| z := 1.0 | |
| i := 0 | |
| for math.Abs(z*z -x) > delta{ | |
| z = z - (z*z - x)/(z*2) | |
| i++ | |
| } | |
| return z,i | |
| } | |
| func main() { | |
| fmt.Printf("math.Sqrt(2) is %v\n", math.Sqrt(2)) | |
| value, iteration := Sqrt(2) | |
| fmt.Printf("My Sqrt(2) is %v and it takes %v iterations", value, iteration) | |
| } | |
| //Exercise: Slices | |
| package main | |
| import "code.google.com/p/go-tour/pic" | |
| func Pic(dx, dy int) [][]uint8 { | |
| image := make([][]uint8, dy) | |
| for i := range image { | |
| image[i] = make([]uint8, dx) | |
| for j := range image[i] { | |
| image[i][j] = uint8(i*i + j*j) | |
| } | |
| } | |
| return image | |
| } | |
| func main() { | |
| pic.Show(Pic) | |
| } | |
| //Exercise: Maps | |
| package main | |
| import ( | |
| "code.google.com/p/go-tour/wc" | |
| "strings" | |
| ) | |
| func WordCount(s string) map[string]int { | |
| chuncks := strings.Fields(s) | |
| result := make(map[string]int) | |
| for i:= 0; i < len(chuncks); i++{ | |
| if _, exist := result[chuncks[i]]; !exist{ | |
| result[chuncks[i]] = 1 | |
| }else{ | |
| result[chuncks[i]]++ | |
| } | |
| } | |
| return result | |
| } | |
| func main() { | |
| wc.Test(WordCount) | |
| } | |
| //Exercise: Fibonacci closure | |
| package main | |
| import "fmt" | |
| // fibonacci is a function that returns | |
| // a function that returns an int. | |
| func fibonacci() func() int { | |
| num1, num2, sum := 0, 1, 0 | |
| return func() int{ | |
| sum = num1 + num2 | |
| num1 = num2 | |
| num2 = sum | |
| return num1 | |
| } | |
| } | |
| func main() { | |
| f := fibonacci() | |
| for i := 0; i < 10; i++ { | |
| fmt.Println(f()) | |
| } | |
| } | |
| //Exercise: Stringers | |
| package main | |
| import "fmt" | |
| type IPAddr [4]byte | |
| // TODO: Add a "String() string" method to IPAddr. | |
| func (a IPAddr)String() string{ | |
| return fmt.Sprintf("%v.%v.%v.%v",a[0],a[1],a[2],a[3]) | |
| } | |
| func main() { | |
| addrs := map[string]IPAddr{ | |
| "loopback": {127, 0, 0, 1}, | |
| "googleDNS": {8, 8, 8, 8}, | |
| } | |
| for n, a := range addrs { | |
| fmt.Printf("%v: %v\n", n, a) | |
| } | |
| } | |
| //Exercise: Errors | |
| package main | |
| import ( | |
| "fmt" | |
| "math" | |
| ) | |
| type ErrNegativeSqrt float64 | |
| func (err ErrNegativeSqrt)Error() string{ | |
| return fmt.Sprintf("\"cannot Sqrt negative number: %F\"", err) | |
| } | |
| func Sqrt(x float64) (float64, error) { | |
| z := 1.0 | |
| if x >= 0 { | |
| for math.Abs(z*z - x) > 0.000000000001{ | |
| z = z - (z*z - x)/(z*2) | |
| } | |
| return z, nil | |
| }else{ | |
| return x, ErrNegativeSqrt(x) | |
| } | |
| } | |
| func main() { | |
| fmt.Println(Sqrt(2)) | |
| fmt.Println(Sqrt(-2)) | |
| } | |
| //Exercise: Readers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment