From 86fe25be6f42837904be51143b85df413638360e Mon Sep 17 00:00:00 2001 From: Vasilii Shpanskii Date: Thu, 23 Jun 2022 13:19:47 +0600 Subject: [PATCH 01/13] Added e1 Added quiz answers --- unit1/README.md | 12 ++++++------ unit1/exercises/e1/main.go | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 unit1/exercises/e1/main.go diff --git a/unit1/README.md b/unit1/README.md index 69ba6ef..7533801 100644 --- a/unit1/README.md +++ b/unit1/README.md @@ -16,14 +16,14 @@ TBA 1. `numbers.append(3)` 2. `numbers.insert(3, 3)` 3. `append(numbers, 3)` -4. `numbers = append(numbers, 3)` +4. `numbers = append(numbers, 3)` - this doing it #### Q2. From where is the variable fooVar accessible if it is declared outside of any functions in a file in package fooPackage located inside module fooModule -1. anywhere inside `fooPackage`, not the rest of `fooModule` +1. anywhere inside `fooPackage`, not the rest of `fooModule` - here 2. by any application that imports `fooModule` 3. from anywhere in `fooModule` -4. by other packages in `fooModule` as long as they import `fooPackage` +4. by other packages in `fooModule` as long as they import `fooPackage` - and here #### Q3. What should the idiomatic name be for an interface with a single method and the signature Serve() error @@ -36,7 +36,7 @@ TBA 1. `for i,r:=0,rand.Int(); i < r%10; i++ { ... }` 2. `for { ... }` -3. `{ ... } for false` +3. `{ ... } for false` -- here is a failure 4. `for _,c := range "foobar" { ... }` ## Excercises @@ -74,7 +74,7 @@ func main() { ``` Next we've asked to change loop condition to stop once the value has stopped changing (or only changes by a very small amount). We can achieve that by adding `epsilon` constant that will be compared with `delta`. -**Note that their absolute values should be compared**. One approach is to use `Abs()` function form `math` package, or we can notice that `x` argument must be positive and kae our own condition based on partial comparison of positive-only numbers: +**Note that their absolute values should be compared**. One approach is to use `Abs()` function form `math` package, or we can notice that `x` argument must be positive and care(?) our own condition based on partial comparison of positive-only numbers: ```go package main @@ -104,7 +104,7 @@ func main() { ``` If you haven't managed to solve all steps publish code for the step you succeeded and make a comment in code. -If you exercise requires to make multiple files or even pacakges, don't hesitate to create them in `unit1/exercises/eX/` folder +If you exercise requires to make multiple files or even packages, don't hesitate to create them in `unit1/exercises/eX/` folder As soon as you implemented the code and place it into `unit1/exercises/e0/main.go`, make PR to this repo. diff --git a/unit1/exercises/e1/main.go b/unit1/exercises/e1/main.go new file mode 100644 index 0000000..c30af2a --- /dev/null +++ b/unit1/exercises/e1/main.go @@ -0,0 +1,23 @@ +// a function that returns an int. +func fibonacci() func() int { + a, b := 0, 0 + return func() int { + switch a { + case 0: + a = 1 + b = 1 + return 0 + default: + current := a + a, b = b, a+b + return a - current + } + } +} + +func main() { + f := fibonacci() + for i := 0; i < 12; i++ { + fmt.Println(f()) + } +} \ No newline at end of file From 724f321c4bfd14e12642907eaea90c135dd83289 Mon Sep 17 00:00:00 2001 From: Vasilii Shpanskii Date: Thu, 23 Jun 2022 13:22:46 +0600 Subject: [PATCH 02/13] Correct quiz --- unit1/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unit1/README.md b/unit1/README.md index 7533801..08a09ee 100644 --- a/unit1/README.md +++ b/unit1/README.md @@ -23,7 +23,7 @@ TBA 1. anywhere inside `fooPackage`, not the rest of `fooModule` - here 2. by any application that imports `fooModule` 3. from anywhere in `fooModule` -4. by other packages in `fooModule` as long as they import `fooPackage` - and here +4. by other packages in `fooModule` as long as they import `fooPackage` #### Q3. What should the idiomatic name be for an interface with a single method and the signature Serve() error From 4c53c92458d1591299b5924b28b64d04a266e5d3 Mon Sep 17 00:00:00 2001 From: Vasilii Shpanskii Date: Fri, 1 Jul 2022 00:13:35 +0600 Subject: [PATCH 03/13] Add stringers Correct fibonacci --- unit1/exercises/e1/main.go | 6 +++++- unit1/exercises/e2/main.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 unit1/exercises/e2/main.go diff --git a/unit1/exercises/e1/main.go b/unit1/exercises/e1/main.go index c30af2a..a2f9d0e 100644 --- a/unit1/exercises/e1/main.go +++ b/unit1/exercises/e1/main.go @@ -1,3 +1,7 @@ +package main + +import "fmt" + // a function that returns an int. func fibonacci() func() int { a, b := 0, 0 @@ -20,4 +24,4 @@ func main() { for i := 0; i < 12; i++ { fmt.Println(f()) } -} \ No newline at end of file +} diff --git a/unit1/exercises/e2/main.go b/unit1/exercises/e2/main.go new file mode 100644 index 0000000..0566d93 --- /dev/null +++ b/unit1/exercises/e2/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "strconv" + "strings" +) + +type IPAddr [4]byte + +// TODO: Add a "String() string" method to IPAddr. + +func (ip IPAddr) String() string { + var s []string + for _, value := range ip { + s = append(s, strconv.Itoa(int(value))) + } + return strings.Join(s, ".") +} + +func main() { + hosts := map[string]IPAddr{ + "loopback": {127, 0, 0, 1}, + "googleDNS": {8, 8, 8, 8}, + } + for name, ip := range hosts { + fmt.Printf("%v: %v\n", name, ip) + } +} From 4e462bf23f8e0d6407be8eb108ec43773055a195 Mon Sep 17 00:00:00 2001 From: Vasilii Shpanskii Date: Fri, 1 Jul 2022 01:00:55 +0600 Subject: [PATCH 04/13] Added e3 --- unit1/exercises/e3/main.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 unit1/exercises/e3/main.go diff --git a/unit1/exercises/e3/main.go b/unit1/exercises/e3/main.go new file mode 100644 index 0000000..69270aa --- /dev/null +++ b/unit1/exercises/e3/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" +) + +/* func Sqrt(x float64) (float64, error) { + return 0, nil +}*/ + +type ErrNegativeSqrt float64 + +var e ErrNegativeSqrt + +func Sqrt(x float64) (float64, error) { + var z float64 + //var e error + z = x + //fmt.Println(z) + if z < 0 { + return z, e + } + for i := 1; i < 20; i++ { + z -= (z*z - x) / (2 * z) + fmt.Println(z) + } + return z, nil +} + +func (e ErrNegativeSqrt) Error() string { + return fmt.Sprintf("cannot Sqrt negative number: -2") +} + +func main() { + fmt.Println(Sqrt(2)) + fmt.Println(Sqrt(-2)) +} From 5765e01ac3781e0aea8dd310573d2a4a55ddcfe7 Mon Sep 17 00:00:00 2001 From: Vasilii Shpanskii Date: Fri, 1 Jul 2022 14:00:14 +0600 Subject: [PATCH 05/13] Drop optional output --- unit1/exercises/e3/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unit1/exercises/e3/main.go b/unit1/exercises/e3/main.go index 69270aa..67d2e9c 100644 --- a/unit1/exercises/e3/main.go +++ b/unit1/exercises/e3/main.go @@ -22,7 +22,7 @@ func Sqrt(x float64) (float64, error) { } for i := 1; i < 20; i++ { z -= (z*z - x) / (2 * z) - fmt.Println(z) + // fmt.Println(z) } return z, nil } From e3591f66374db8c0162c1d028b7b14afb512a3a2 Mon Sep 17 00:00:00 2001 From: Vasilii Shpanskii Date: Mon, 4 Jul 2022 02:23:18 +0600 Subject: [PATCH 06/13] Corrected output --- unit1/exercises/e3/main.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/unit1/exercises/e3/main.go b/unit1/exercises/e3/main.go index 67d2e9c..b2d8fff 100644 --- a/unit1/exercises/e3/main.go +++ b/unit1/exercises/e3/main.go @@ -14,15 +14,12 @@ var e ErrNegativeSqrt func Sqrt(x float64) (float64, error) { var z float64 - //var e error z = x - //fmt.Println(z) if z < 0 { - return z, e + return 0, e } for i := 1; i < 20; i++ { z -= (z*z - x) / (2 * z) - // fmt.Println(z) } return z, nil } From 8e40522c8fdf6b3cd45ce44ad3350c4cc47f0f92 Mon Sep 17 00:00:00 2001 From: Mycron Date: Thu, 20 Oct 2022 01:08:31 +0600 Subject: [PATCH 07/13] Clear the code with closure --- unit1/exercises/e1/main.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/unit1/exercises/e1/main.go b/unit1/exercises/e1/main.go index a2f9d0e..e58201b 100644 --- a/unit1/exercises/e1/main.go +++ b/unit1/exercises/e1/main.go @@ -2,26 +2,31 @@ package main import "fmt" +// fibonacci is a function that returns // a function that returns an int. func fibonacci() func() int { - a, b := 0, 0 + x := 0 + y := x + z := x return func() int { - switch a { - case 0: - a = 1 - b = 1 + if z == 0 { + z = 1 return 0 - default: - current := a - a, b = b, a+b - return a - current + } else if y == 0 { + z = z + y + y = 1 + } else { + z = x + y + x = y + y = z } + return z } } func main() { f := fibonacci() - for i := 0; i < 12; i++ { + for i := 0; i < 15; i++ { fmt.Println(f()) } } From 17c5f2dd2af3504e337991357375b52363077da6 Mon Sep 17 00:00:00 2001 From: Mycron Date: Sat, 29 Oct 2022 01:35:38 +0600 Subject: [PATCH 08/13] Down to 10 elems of fibonacci --- unit1/exercises/e1/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unit1/exercises/e1/main.go b/unit1/exercises/e1/main.go index e58201b..dff2cec 100644 --- a/unit1/exercises/e1/main.go +++ b/unit1/exercises/e1/main.go @@ -26,7 +26,7 @@ func fibonacci() func() int { func main() { f := fibonacci() - for i := 0; i < 15; i++ { + for i := 0; i < 10; i++ { fmt.Println(f()) } } From 16dff69a750635836916abe044965e4c41fd50af Mon Sep 17 00:00:00 2001 From: Mycron Date: Sun, 30 Oct 2022 17:16:04 +0600 Subject: [PATCH 09/13] Reimplement e3 --- unit1/exercises/e3/main.go | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/unit1/exercises/e3/main.go b/unit1/exercises/e3/main.go index b2d8fff..0220f40 100644 --- a/unit1/exercises/e3/main.go +++ b/unit1/exercises/e3/main.go @@ -4,30 +4,32 @@ import ( "fmt" ) -/* func Sqrt(x float64) (float64, error) { - return 0, nil -}*/ - type ErrNegativeSqrt float64 -var e ErrNegativeSqrt +func (e ErrNegativeSqrt) Error() string { + return fmt.Sprint("cannot Sqrt negative number: -2\n") +} func Sqrt(x float64) (float64, error) { - var z float64 - z = x - if z < 0 { + var e ErrNegativeSqrt + epsilon := 0.0000000000001 + z := x / 2 + i := 1 + if x < 0 { return 0, e } - for i := 1; i < 20; i++ { - z -= (z*z - x) / (2 * z) + for { + delta := (z*z - x) / (2 * z) + if delta < epsilon && delta > -epsilon { + break + } + z -= delta + //fmt.Println(i," : ", z) + i += 1 } return z, nil } -func (e ErrNegativeSqrt) Error() string { - return fmt.Sprintf("cannot Sqrt negative number: -2") -} - func main() { fmt.Println(Sqrt(2)) fmt.Println(Sqrt(-2)) From 6e7f0f6a4860e3892602ae0aed4ea80610465a3d Mon Sep 17 00:00:00 2001 From: Mycron Date: Sun, 30 Oct 2022 18:01:08 +0600 Subject: [PATCH 10/13] Add e4/main.go --- unit1/exercises/e4/main.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 unit1/exercises/e4/main.go diff --git a/unit1/exercises/e4/main.go b/unit1/exercises/e4/main.go new file mode 100644 index 0000000..ffeab7f --- /dev/null +++ b/unit1/exercises/e4/main.go @@ -0,0 +1,19 @@ +package main + +import "golang.org/x/tour/reader" + +type MyReader struct{} + +func (x MyReader) Read(b []byte) (int, error) { + var i int = 0 + var e error + for { + b = append(b, byte('A')) + i += 1 + } + return i, e +} + +func main() { + reader.Validate(MyReader{}) +} From a81c3f8b67c257ba5f15be29f65bd6df7099abb8 Mon Sep 17 00:00:00 2001 From: Mycron Date: Sun, 30 Oct 2022 19:58:23 +0600 Subject: [PATCH 11/13] Switch to correct exercise :) --- unit1/exercises/e4/main.go | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/unit1/exercises/e4/main.go b/unit1/exercises/e4/main.go index ffeab7f..eb9c1d5 100644 --- a/unit1/exercises/e4/main.go +++ b/unit1/exercises/e4/main.go @@ -1,19 +1,35 @@ package main -import "golang.org/x/tour/reader" +import ( + "io" + "os" + "strings" + // "fmt" +) -type MyReader struct{} +type rot13Reader struct { + r io.Reader +} -func (x MyReader) Read(b []byte) (int, error) { - var i int = 0 - var e error - for { - b = append(b, byte('A')) - i += 1 +func (r rot13Reader) Read(p []byte) (n int, err error) { + n, err = r.r.Read(p) + // fmt.Printf("read %v bytes\n", n) + for i := range p { + p[i] = rot13(p[i]) + } + return n, err +} +func rot13(b byte) byte { + if b >= 'A' && b <= 'Z' { + b = 'A' + (b-'A'+13)%26 + } else if b >= 'a' && b <= 'z' { + b = 'a' + (b-'a'+13)%26 } - return i, e + return b } func main() { - reader.Validate(MyReader{}) + s := strings.NewReader("Lbh penpxrq gur pbqr!") + r := rot13Reader{s} + io.Copy(os.Stdout, &r) } From da6ceb11f48332469d90b1c5dfd6149f910f01ee Mon Sep 17 00:00:00 2001 From: Mycron Date: Sun, 30 Oct 2022 20:04:18 +0600 Subject: [PATCH 12/13] Try to use Sprintln --- unit1/exercises/e3/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unit1/exercises/e3/main.go b/unit1/exercises/e3/main.go index 0220f40..f22dcd5 100644 --- a/unit1/exercises/e3/main.go +++ b/unit1/exercises/e3/main.go @@ -7,7 +7,7 @@ import ( type ErrNegativeSqrt float64 func (e ErrNegativeSqrt) Error() string { - return fmt.Sprint("cannot Sqrt negative number: -2\n") + return fmt.Sprintln("cannot Sqrt negative number: -2") } func Sqrt(x float64) (float64, error) { From 1ecacf5634f553a429a6ca573ff7d7a2a2a79a28 Mon Sep 17 00:00:00 2001 From: Mycron Date: Mon, 31 Oct 2022 19:32:01 +0600 Subject: [PATCH 13/13] Correct error output --- unit1/exercises/e3/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/unit1/exercises/e3/main.go b/unit1/exercises/e3/main.go index f22dcd5..358bbb6 100644 --- a/unit1/exercises/e3/main.go +++ b/unit1/exercises/e3/main.go @@ -7,16 +7,16 @@ import ( type ErrNegativeSqrt float64 func (e ErrNegativeSqrt) Error() string { - return fmt.Sprintln("cannot Sqrt negative number: -2") + return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e)) } func Sqrt(x float64) (float64, error) { - var e ErrNegativeSqrt + //var e ErrNegativeSqrt epsilon := 0.0000000000001 z := x / 2 i := 1 if x < 0 { - return 0, e + return 0, ErrNegativeSqrt(x) } for { delta := (z*z - x) / (2 * z)