Last active
August 29, 2015 14:13
-
-
Save worace/8b61e95ecaabefccc4ea to your computer and use it in GitHub Desktop.
Golang Tour Exercises
This file contains 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 guessOutsideDelta(guess, x float64) bool { | |
delta := .00000000001 | |
return (math.Abs(guess * guess - x) > delta) | |
} | |
func Sqrt(x float64) float64 { | |
z := x / 2 | |
iterations := 1 | |
for guessOutsideDelta(z, x) { | |
z = z - (z*z - x)/ (2 * z) | |
fmt.Println("iteration #", iterations) | |
iterations ++ | |
} | |
return z | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
fmt.Println(math.Sqrt(2)) | |
fmt.Println(Sqrt(9)) | |
fmt.Println(Sqrt(16)) | |
fmt.Println(Sqrt(7)) | |
} |
This file contains 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/wc" | |
"strings" | |
) | |
func WordCount(s string) map[string]int { | |
countMap := map[string]int {} | |
for _, word := range strings.Fields(s) { | |
if count, ok := countMap[word]; ok { | |
countMap[word] = count + 1 | |
} else { | |
countMap[word] = 1 | |
} | |
} | |
return countMap | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
This file contains 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/reader" | |
type MyReader struct{} | |
func (reader MyReader) Read(b []byte) (int, error) { | |
for { | |
for i:=0; i < 8; i ++ { | |
b[i] = byte('A') | |
} | |
return 8, nil | |
} | |
} | |
func main() { | |
reader.Validate(MyReader{}) | |
} |
This file contains 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 ( | |
"bytes" | |
"fmt" | |
"io" | |
"os" | |
"strings" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func alphabetRange(start int) []byte { | |
var chars []byte | |
for i := start; i < start+26; i++ { | |
chars = append(chars, byte(i)) | |
} | |
return append(chars, chars...) | |
} | |
func (reader *rot13Reader) Read(b []byte) (bytesRead int, err error) { | |
bytesRead, err = reader.r.Read(b) | |
upper := alphabetRange(65) | |
lower := alphabetRange(97) | |
for i := range b { | |
if bytes.IndexByte(lower, b[i]) > -1 { | |
b[i] = lower[b[i]+13-97] | |
} else if bytes.IndexByte(upper, b[i]) > -1 { | |
b[i] = upper[b[i]+13-65] | |
} else { | |
b[i] = b[i] | |
} | |
} | |
return | |
} | |
func main() { | |
s := strings.NewReader("Lbh penpxrq gur pbqr!") | |
r := rot13Reader{s} | |
written, err := io.Copy(os.Stdout, &r) | |
fmt.Println(err) | |
fmt.Println("printed n bytes", written) | |
} |
This file contains 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 guessOutsideDelta(guess, x float64) bool { | |
delta := .000000001 | |
return (math.Abs(guess * guess - x) > delta) | |
} | |
func Sqrt(x float64) (float64, error) { | |
if x < 0 { | |
return 0, ErrNegativeSqrt(x) | |
} else { | |
z := x / 2 | |
iterations := 1 | |
for guessOutsideDelta(z, x) { | |
z = z - (z*z - x)/ (2 * z) | |
fmt.Println("iteration #", iterations) | |
iterations ++ | |
} | |
return z, nil | |
} | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
fmt.Println(Sqrt(-2)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment