Last active
December 26, 2015 01:39
-
-
Save rpkamp/7072408 to your computer and use it in GitHub Desktop.
My Solution to a few of the #golang Tour exercises
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
/////// | |
// Sqrt | |
func Sqrt(x float64) (z float64) { | |
z = 8.0 | |
last, delta := z, 0.0000000000000005 | |
for i := 0; i < 1000000000; i++ { | |
top := math.Pow(z, 2.0) - x | |
bottom := 2*z | |
z = z - top / bottom | |
if z < last * (1+delta) && z > last * (1-delta) { | |
fmt.Println(z / last) | |
fmt.Println("Small change. Done", i) | |
return | |
} | |
last = z | |
} | |
return | |
} | |
//////////// | |
// Fibonacci | |
package main | |
import "fmt" | |
func fibonacci() func() int { | |
p3, p2, p1 := 1, 1, 0 | |
return func() int { | |
ret := p1 | |
p1, p2 = p2, p3 | |
p3 = p1 + p2 | |
return ret | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} | |
///////// | |
// Errors | |
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) | |
} | |
z := 8.0 | |
last, delta := z, 0.0000000000000005 | |
for i := 0; i < 1000000000; i++ { | |
top := math.Pow(z, 2.0) - x | |
bottom := 2*z | |
z = z - top / bottom | |
if math.Abs(z-last) < delta { | |
return z, nil | |
} | |
last = z | |
} | |
return z, nil | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
fmt.Println(Sqrt(-2)) | |
} | |
///////// | |
// Images | |
package main | |
import ( | |
"code.google.com/p/go-tour/pic" | |
"image" | |
"math" | |
"image/color" | |
) | |
type Image struct { | |
Width,Height int | |
} | |
func (i *Image) ColorModel() color.Model { | |
return color.RGBAModel | |
} | |
func (i *Image) Bounds() image.Rectangle { | |
return image.Rect(0, 0, i.Width, i.Height) | |
} | |
func (i *Image) At(x,y int) color.Color { | |
r := uint8(math.Sin(float64(x))*255.0) | |
g := uint8(math.Cos(float64(y))*255.0) | |
b := 1/g | |
return color.RGBA{r, g, b, 100} | |
} | |
func main() { | |
m := Image{1000,1000} | |
pic.ShowImage(&m) | |
} | |
////// | |
//Tree | |
package main | |
import ( | |
"code.google.com/p/go-tour/tree" | |
"fmt" | |
) | |
// Walk walks the tree t sending all values | |
// from the tree to the 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) | |
} | |
func Walker(t *tree.Tree) <-chan int { | |
ch := make(chan int) | |
go func() { | |
Walk(t, ch) | |
close(ch) | |
}() | |
return ch | |
} | |
// Same determines whether the trees | |
// t1 and t2 contain the same values. | |
func Same(t1, t2 *tree.Tree) bool { | |
ch1 := Walker(t1) | |
ch2 := Walker(t2) | |
for { | |
v1, ok1 := <- ch1 | |
v2, ok2 := <- ch2 | |
if !ok1 || !ok2 { | |
return ok1 == ok2 | |
} | |
if v1 != v2 { | |
break | |
} | |
} | |
return false | |
} | |
func main() { | |
for i := 1; i < 10; i++ { | |
for j := 1; j < 10; j++ { | |
if x := Same(tree.New(i), tree.New(j)); x == true && j != i { | |
fmt.Println("OH NO! ", i, " ", j) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment