Last active
February 7, 2018 08:13
-
-
Save xavierskip/f10b8f023c1b5a08e344b9e57dd2e1df to your computer and use it in GitHub Desktop.
golang tour 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
package main | |
import "golang.org/x/tour/tree" | |
import "fmt" | |
// Walk 步进 tree t 将所有的值从 tree 发送到 channel ch。 | |
func Walk(t *tree.Tree, ch chan int) { | |
if t == nil { | |
return | |
} | |
Walk(t.Left, ch) | |
ch <- t.Value | |
Walk(t.Right, ch) | |
//close(ch) | |
} | |
// Same 检测树 t1 和 t2 是否含有相同的值。 | |
func Same(t1, t2 *tree.Tree) bool { | |
ch1, ch2 := make(chan int), make(chan int) | |
go Walk(t1, ch1) | |
go Walk(t2, ch2) | |
for { | |
v1 := <-ch1 | |
v2 := <-ch2 | |
if v1 == v2 { | |
return true | |
} else { | |
return false | |
} | |
} | |
} | |
func main() { | |
t1 := tree.New(1) | |
t2 := tree.New(2) | |
//fmt.Println(t1) | |
if Same(t1, t2) { | |
fmt.Println("PASS!") | |
} else { | |
fmt.Println("FAIL!") | |
} | |
} |
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 | |
import ( | |
"fmt" | |
"math" | |
) | |
type ErrNegativeSqrt float64 | |
func (e ErrNegativeSqrt) Error() string { | |
return fmt.Sprintf("cannot Sqrt negative number: %v",float64(e)) | |
} | |
func Sqrt(x float64) (float64, error) { | |
if x < 0 { | |
return 0, ErrNegativeSqrt(x) | |
} | |
var z float64 = x/2 | |
var tmp float64 | |
for math.Abs(tmp-z) > 0.000000000001 { | |
tmp = z | |
z -= (z*z-x)/(2*z) | |
//fmt.Printf("tmp:%f\n",tmp) | |
} | |
return z,nil | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
fmt.Println(Sqrt(-2)) | |
} |
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 | |
import "fmt" | |
// fibonacci 函数会返回一个返回 int 的函数。 | |
func fibonacci() func() int { | |
a, b := -1, 1 | |
return func() int { | |
a, b = b, a+b | |
return b | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 15; i++ { | |
fmt.Println(f()) | |
} | |
} |
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 | |
import ( | |
"image" | |
"image/color" | |
"golang.org/x/tour/pic" | |
) | |
type Image struct{ | |
Height, Width int | |
} | |
func (m Image) ColorModel() color.Model { | |
return color.RGBAModel | |
} | |
func (m Image) Bounds() image.Rectangle { | |
return image.Rect(0, 0, m.Height, m.Width) | |
} | |
func (m Image) At(x, y int) color.Color { | |
v := uint8(x ^ y) | |
return color.RGBA{v, v, 255, 255} | |
} | |
func main() { | |
m := Image{256, 256} | |
pic.ShowImage(m) | |
} |
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 | |
import ( | |
"fmt" | |
"math" | |
) | |
func Sqrt(x float64) float64 { | |
var z float64 = x/2 | |
var tmp float64 | |
for math.Abs(tmp-z) > 0.000000000001 { | |
tmp = z | |
z -= (z*z-x)/(2*z) | |
fmt.Printf("tmp:%f\n",tmp) | |
} | |
return z | |
} | |
func main() { | |
fmt.Println(Sqrt(65535)) | |
} |
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 | |
import ( | |
"strings" | |
"golang.org/x/tour/wc" | |
) | |
func WordCount(s string) map[string]int { | |
counter := make(map[string]int) | |
arr := strings.Fields(s) | |
for _, item := range arr { | |
counter[item]++ | |
} | |
return counter | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
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 | |
import "golang.org/x/tour/reader" | |
type MyReader struct{} | |
// TODO: Add a Read([]byte) (int, error) method to MyReader. | |
func (r MyReader) Read(b []byte) (int,error) { | |
for _,i := range b { | |
b[i] = 'A' | |
} | |
return 1, nil | |
} | |
func main() { | |
reader.Validate(MyReader{}) | |
} |
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 | |
import ( | |
"io" | |
"os" | |
"strings" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func (rot rot13Reader) Read(b []byte) (int, error) { | |
n, err := rot.r.Read(b) | |
for i := 0; i < len(b); i++ { | |
if (b[i] >= 'a' && b[i] < 'n') || (b[i] >= 'A' && b[i] < 'N') { | |
b[i] += 13 | |
} else if (b[i] >= 'm' && b[i] <= 'z') || (b[i] >= 'M' && b[i] <= 'Z') { | |
b[i] -= 13 | |
} | |
} | |
return n, err | |
} | |
func main() { | |
s := strings.NewReader("Lbh penpxrq gur pbqr!") | |
r := rot13Reader{s} | |
io.Copy(os.Stdout, &r) | |
} |
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 | |
import "golang.org/x/tour/pic" | |
func Pic(dx, dy int) [][]uint8 { | |
s := make([][]uint8, dy) | |
for y := 0; y < dy; y++ { | |
s[y] = make([]uint8, dx) | |
for x := 0; x < dx; x++ { | |
//s[y][x] = uint8((x + y) / 2) | |
//s[y][x] = uint8(x*y) | |
s[y][x] = uint8(x^y) | |
} | |
} | |
return s | |
} | |
func main() { | |
pic.Show(Pic) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment