Last active
December 16, 2015 08:19
-
-
Save broox/5405046 to your computer and use it in GitHub Desktop.
Here are some of my solutions to the exercises at tour.golang.org. This felt like I was in college again...
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" | |
) | |
func Sqrt(x float64) (float64, int) { | |
var i, z = 1, 1.0 | |
for ; i < 11; i++ { | |
new_z := z - (((z * z) - x) / (2 * z)) | |
delta := new_z - z | |
if delta == 0 { | |
return z, i | |
} else { | |
z = new_z | |
} | |
} | |
return z, i | |
} | |
func main() { | |
for i:= 1; i <= 100; i++ { | |
var sqrt, iterations = Sqrt(float64(i)) | |
fmt.Println("Found that the sqrt of", i, "is", sqrt, "after", iterations,"iterations") | |
} | |
} |
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 "code.google.com/p/go-tour/pic" | |
func Pic(dx, dy int) [][]uint8 { | |
pic := make([][]uint8, dy) | |
for y := 0; y < dy; y++ { | |
uints := make([]uint8, dx) | |
for x := 0; x < dx; x++ { | |
uints[x] = uint8(x^y) | |
} | |
pic[y] = uints | |
} | |
return pic | |
} | |
func main() { | |
pic.Show(Pic) | |
} |
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" | |
import "strings" | |
import ( | |
"code.google.com/p/go-tour/wc" | |
) | |
func WordCount(s string) map[string]int { | |
words := strings.Fields(s) | |
fmt.Printf("\n===\n%s is %d words long\n\n",words, len(words)) | |
m := make(map[string]int) | |
for _, word := range words { | |
v := m[word] | |
m[word] = v + 1 | |
} | |
return m | |
} | |
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 "fmt" | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
x, y := 0, 1 | |
return func() int { | |
x, y = y, x+y | |
return y - x | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; 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 "fmt" | |
import "math/cmplx" | |
func Cbrt(x complex128) (complex128, int) { | |
i, z := 1, x | |
for ; i <= 100; i++ { | |
new_z := z - ((cmplx.Pow(z, 3) - x)/(3 * cmplx.Pow(z,2))) | |
delta := new_z - z | |
if delta == 0 { | |
return z, i | |
} | |
z = new_z | |
} | |
return z, i | |
} | |
func main() { | |
cbrt, iterations := Cbrt(2) | |
fmt.Println("Found that the cbrt of 2 is", cbrt, "after", iterations,"iterations") | |
} |
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" | |
type ErrNegativeSqrt float64 | |
func (e ErrNegativeSqrt) Error() string { | |
return fmt.Sprintf("Can't Sqrt negative number: %g", float64(e)) | |
} | |
func Sqrt(x float64) (float64, int, error) { | |
if x < 0 { | |
return 0, 0, ErrNegativeSqrt(x) | |
} | |
if x == 0 { | |
return 0, 0, nil | |
} | |
var i, z = 1, 1.0 | |
for ; i <= 100; i++ { | |
new_z := z - (((z * z) - x) / (2 * z)) | |
delta := new_z - z | |
if delta == 0 { | |
return z, i, nil | |
} else { | |
z = new_z | |
} | |
} | |
return z, i, nil | |
} | |
func main() { | |
for i:= -5; i <= 100; i++ { | |
var sqrt, iterations, error = Sqrt(float64(i)) | |
if error != nil { | |
fmt.Println(error) | |
continue | |
} | |
fmt.Println("Found that the sqrt of", i, "is", sqrt, "after", iterations,"iterations") | |
} | |
} |
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" | |
"net/http" | |
) | |
type Phrase string | |
type Sentence struct { | |
Greeting string | |
Punct string | |
Who string | |
} | |
func (p Phrase) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, p) | |
} | |
func (s Sentence) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, s.Greeting, s.Punct, s.Who) | |
} | |
func main() { | |
http.Handle("/phrase", Phrase("This is a phrase.")) | |
http.Handle("/sentence", &Sentence{"Hey", ":", "Gophers!"}) | |
http.ListenAndServe("localhost:4000", nil) | |
} |
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 ( | |
"code.google.com/p/go-tour/pic" | |
"image" | |
"image/color" | |
) | |
type Image struct{ | |
Width, Height int | |
color 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.color+uint8(x^y), r.color+uint8(y), 255, 255} | |
} | |
func main() { | |
m := Image{128, 128, 128} | |
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 ( | |
"io" | |
"os" | |
"strings" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func (rot *rot13Reader) Read(s []byte) (n int, err error) { | |
n, err = rot.r.Read(s) | |
for i := 0; i < len(s); i++ { | |
if (s[i] >= 'A' && s[i] < 'N') || (s[i] >='a' && s[i] < 'n') { | |
s[i] += 13 | |
} else if (s[i] > 'M' && s[i] <= 'Z') || (s[i] > 'm' && s[i] <= 'z'){ | |
s[i] -= 13 | |
} | |
} | |
return n, err | |
} | |
func main() { | |
phrases := [...]string{"Lbh penpxrq gur pbqr!", | |
"Broox rules?", | |
"Oebbk ehyrf!"} | |
for _, phrase := range phrases { | |
s := strings.NewReader(phrase) | |
r := rot13Reader{s} | |
io.Copy(os.Stdout, &r) | |
io.WriteString(os.Stdout, "\n") | |
} | |
} |
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 "code.google.com/p/go-tour/tree" | |
import "fmt" | |
// Walk walks the tree t sending all values | |
// from the tree to the channel ch. | |
func Walk(t *tree.Tree, ch chan int) { | |
_walk(t, ch) | |
close(ch) | |
} | |
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) | |
} | |
} | |
// 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 Walk(t1, ch1) | |
go Walk(t2, ch2) | |
var leaves []int | |
for i := range ch1 { | |
leaf := <-ch1 | |
leaves = CheckLeaf(leaves, leaf) | |
fmt.Printf("ch1[%d]: %d %v\n", i, leaf, leaves) | |
} | |
for j := range ch2 { | |
leaf := <-ch2 | |
leaves = CheckLeaf(leaves, leaf) | |
fmt.Printf("ch2[%d]: %d %v\n", j, leaf, leaves) | |
} | |
fmt.Println(leaves) | |
return len(leaves) == 0 | |
} | |
// Check for leaves by appending new leaves | |
// and deleting existing leaves from a slice | |
func CheckLeaf(slice []int, ch int) []int { | |
for i, v := range slice { | |
if v == ch { | |
slice = append(slice[:i], slice[i+1:]...) | |
return slice | |
} | |
} | |
return append(slice, ch) | |
} | |
func main() { | |
fmt.Println("Same?",Same(tree.New(1), tree.New(1))) | |
fmt.Println("Same?",Same(tree.New(1), tree.New(2))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment