Skip to content

Instantly share code, notes, and snippets.

@paulperegud
Last active August 28, 2015 17:12
Show Gist options
  • Save paulperegud/15606ca9d72d9f4bb17c to your computer and use it in GitHub Desktop.
Save paulperegud/15606ca9d72d9f4bb17c to your computer and use it in GitHub Desktop.
tour.golang exercise solutions
/******************************************************************************************************/
/* Exercise: Stringers */
package main
import "fmt"
type IPAddr [4]byte
// TODO: Add a "String() string" method to IPAddr.
func (v IPAddr) String() string {
return fmt.Sprintf("%d.%d.%d.%d", v[0], v[1], v[2], v[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"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negative number: %f", float64(e))
}
func Sqrt(x float64) (z float64, err error) {
if x < 0 {
return 0, ErrNegativeSqrt(x)
}
z = float64(2.)
for i:=0; i < 20; i++ {
z = z - (z*z - x)/(2*z)
}
return z, nil
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(Sqrt(-2))
}
/******************************************************************************************************/
/* Exercise: Readers */
package main
import (
"golang.org/x/tour/reader"
"fmt"
)
type MyReader struct{}
func (v MyReader) Read(stream []byte) (int, error) {
i := 0
//printSlice("a", stream)
for i = 0; i < cap(stream); i++ {
stream[i] = uint8('A')
}
return i, nil
}
func printSlice(s string, x []byte) {
fmt.Printf("%s len=%d cap=%d %v\n",
s, len(x), cap(x), x)
}
func main() {
reader.Validate(MyReader{})
}
/******************************************************************************************************/
/* Exercise: Rot13 Reader #59: 'You cracked the code!' */
package main
import (
"io"
"os"
"strings"
"math"
)
type rot13Reader struct {
r io.Reader
}
e
func (ret *rot13Reader) Read(p []byte) (n int, err error) {
n, err = ret.r.Read(p)
for i := 0; i < len(p); i++ {
var o byte = 0
if p[i] >= byte('A') && p[i] <= byte('Z') {
o = byte('A')-1
}
if p[i] >= byte('a') && p[i] <= byte('z') {
o = byte('a')-1
}
if o > 0 {
f := float64((p[i] - o) + 13)
p[i] = byte(math.Mod(f, 26)) + o
}
}
return
}
func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}
/******************************************************************************************************/
/* Exercise: Equivalent Binary Trees */
package main
import (
"golang.org/x/tour/tree"
"fmt"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.
// Depth-first walking, send after backing
// from left and before going into right
func Walk(t *tree.Tree, ch chan int) {
if t.Left != nil {
Walk(t.Left, ch)
}
ch <- t.Value
if t.Right != nil {
Walk(t.Right, ch)
}
}
func RWalk(t *tree.Tree, ch chan int) {
Walk(t, ch)
close(ch)
}
// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
ch1 := make(chan int)
ch2 := make(chan int)
go RWalk(t1, ch1)
go RWalk(t2, ch2)
for i := range ch1 {
if i != <- ch2 {
return false
}
}
return true
}
func main() {
ch := make(chan int)
go RWalk(tree.New(10), ch)
for i := range(ch) {
fmt.Println(i)
}
fmt.Println(Same(tree.New(10), tree.New(10)))
fmt.Println(Same(tree.New(10), tree.New(11)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment