-
-
Save ludovicdeluna/a4ce25219fcd8719a6c7 to your computer and use it in GitHub Desktop.
tour.golang exercise solutions
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 #43 */ | |
| package main | |
| import ( | |
| "fmt" | |
| "math" | |
| ) | |
| func Sqrt(x float64) float64 { | |
| z := float64(2.) | |
| s := float64(0) | |
| for { | |
| z = z - (z*z - x)/(2*z) | |
| if math.Abs(s-z) < 1e-15 { | |
| break | |
| } | |
| s = z | |
| } | |
| return s | |
| } | |
| func main() { | |
| fmt.Println(Sqrt(2)) | |
| fmt.Println(math.Sqrt(2)) | |
| } | |
| /******************************************************************************************************/ | |
| /* Exercise: Maps #44 */ | |
| package main | |
| import ( | |
| "tour/wc" | |
| "strings" | |
| ) | |
| func WordCount(s string) map[string]int { | |
| ss := strings.Fields(s) | |
| num := len(ss) | |
| ret := make(map[string]int) | |
| for i := 0; i < num; i++ { | |
| (ret[ss[i]])++ | |
| } | |
| return ret | |
| } | |
| func main() { | |
| wc.Test(WordCount) | |
| } | |
| /******************************************************************************************************/ | |
| /* Exercise: Slices #45 */ | |
| package main | |
| import "tour/pic" | |
| func Pic(dx, dy int) [][]uint8 { | |
| ret := make([][]uint8, dy) | |
| for i := 0; i < dy; i++ { | |
| ret[i] = make([]uint8, dx) | |
| for j := 0; j < dx; j++ { | |
| ret[i][j] = uint8(i^j+(i+j)/2) | |
| } | |
| } | |
| return ret | |
| } | |
| func main() { | |
| pic.Show(Pic) | |
| } | |
| /******************************************************************************************************/ | |
| /* Exercise: Fibonacci closure #46 */ | |
| package main | |
| import "fmt" | |
| // fibonacci is a function that returns | |
| // a function that returns an int. | |
| func fibonacci() func() int { | |
| sum := 1 | |
| prev := 0 | |
| return func() int { | |
| old := sum | |
| sum += prev | |
| prev = old | |
| return sum | |
| } | |
| } | |
| func main() { | |
| f := fibonacci() | |
| for i := 0; i < 10; i++ { | |
| fmt.Println(f()) | |
| } | |
| } | |
| /******************************************************************************************************/ | |
| /* Advanced Exercise: Complex cube roots #47 */ | |
| package main | |
| import ( | |
| "fmt" | |
| "math/cmplx" | |
| ) | |
| func Cbrt(x complex128) complex128 { | |
| z := complex128(2) | |
| s := complex128(0) | |
| for { | |
| z = z - (cmplx.Pow(z,3) - x)/(3 * (z * z)) | |
| if cmplx.Abs(s-z) < 1e-17 { | |
| break | |
| } | |
| s = z | |
| } | |
| return z | |
| } | |
| func main() { | |
| fmt.Println(Cbrt(2)) | |
| } | |
| /******************************************************************************************************/ | |
| /* Exercise: Errors #57 */ | |
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| type ErrNegativeSqrt float64 | |
| func (e ErrNegativeSqrt) Error() string { | |
| return fmt.Sprintf("cannot Sqrt negativ number: %g", float64(e)) | |
| } | |
| func Sqrt(f float64) (float64, error) { | |
| if f < 0 { | |
| return 0, ErrNegativeSqrt(f) | |
| } | |
| return 0, nil | |
| } | |
| func main() { | |
| fmt.Println(Sqrt(2)) | |
| fmt.Println(Sqrt(-2)) | |
| } | |
| /******************************************************************************************************/ | |
| /* Exercise: Images #58 */ | |
| package main | |
| import ( | |
| "image" | |
| "tour/pic" | |
| "image/color" | |
| ) | |
| type Image struct{ | |
| Width, Height int | |
| colr uint8 | |
| } | |
| func (r *Image) Bounds() image.Rectangle { | |
| return image.Rect(0, 0, r.Width, r.Height) | |
| } | |
| func (r *Image) ColorModel() color.Model { | |
| return color.RGBAModel | |
| } | |
| func (r *Image) At(x, y int) color.Color { | |
| return color.RGBA{r.colr, r.colr, 255, 255} | |
| } | |
| func main() { | |
| m := Image{100, 100, 128} | |
| pic.ShowImage(&m) | |
| } | |
| /******************************************************************************************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment