From 7ee04a186489287d2f69cda59b207e1edea94b6e Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sun, 9 Feb 2020 10:44:41 -0600 Subject: [PATCH] more exercises --- exercise-errors.go | 28 ++++++++++++++++++++++++++++ exercise-reader.go | 19 +++++++++++++++++++ exercise-rot-reader.go | 42 ++++++++++++++++++++++++++++++++++++++++++ reader.go | 21 +++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 exercise-errors.go create mode 100644 exercise-reader.go create mode 100644 exercise-rot-reader.go create mode 100644 reader.go diff --git a/exercise-errors.go b/exercise-errors.go new file mode 100644 index 0000000..7067654 --- /dev/null +++ b/exercise-errors.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "math" +) + +type ErrNegativeSqrt float64 + +func (e ErrNegativeSqrt) Error() string { + return fmt.Sprintf("cannot Sqrt negative number: %f", float64(e)) +} + +func Sqrt(x float64) (float64, error) { + if x < 0 { + return 0, ErrNegativeSqrt(x) + } + var p float64 + for z := 1.0; z != p && math.Abs(z - p) > 0.000000000000001; z -= (z * z - x) / (2 * z) { + p = z + } + return p, nil +} + +func main() { + fmt.Println(Sqrt(4)) + fmt.Println(Sqrt(-2)) +} diff --git a/exercise-reader.go b/exercise-reader.go new file mode 100644 index 0000000..0799a59 --- /dev/null +++ b/exercise-reader.go @@ -0,0 +1,19 @@ +package main + +import "golang.org/x/tour/reader" + +type MyReader struct{} + +// TODO: Add a Read([]byte) (int, error) method to MyReader. + +func (MyReader) Read(b []byte) (int, error) { + c := "A" + for i := range b { + b[i] = byte(c[0]) + } + return len(b), nil +} + +func main() { + reader.Validate(MyReader{}) +} diff --git a/exercise-rot-reader.go b/exercise-rot-reader.go new file mode 100644 index 0000000..8171aaf --- /dev/null +++ b/exercise-rot-reader.go @@ -0,0 +1,42 @@ +package main + +import ( + "io" + "os" + "strings" +) + +type rot13Reader struct { + r io.Reader +} + +var rot13Input []byte = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") +var rot13Output []byte = []byte("NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm") + +func indexOf(s []byte, b byte) int { + for i, v := range s { + if v == b { + return i + } + } + return -1 +} + +func (r rot13Reader) Read(b []byte) (n int, err error) { + n, err = r.r.Read(b) + if err == nil { + for i := 0; i < n; i++ { + c := b[i] + if j := indexOf(rot13Input, c); j > -1 { + b[i] = rot13Output[j] + } + } + } + return n, err +} + +func main() { + s := strings.NewReader("Lbh penpxrq gur pbqr!") + r := rot13Reader{s} + io.Copy(os.Stdout, &r) +} diff --git a/reader.go b/reader.go new file mode 100644 index 0000000..1408057 --- /dev/null +++ b/reader.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "io" + "strings" +) + +func main() { + r := strings.NewReader("Hello, Reader!") + + b := make([]byte, 8) + for { + n, err := r.Read(b) + fmt.Printf("n = %v err = %v b = %v\n", n, err, b) + fmt.Printf("b[:n] = %q\n", b[:n]) + if err == io.EOF { + break + } + } +}