diff --git a/unit1/README.md b/unit1/README.md index 848d074..e17e6b1 100644 --- a/unit1/README.md +++ b/unit1/README.md @@ -152,11 +152,11 @@ Read more about slices in [go blog article](https://go.dev/blog/slices-intro) 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` @@ -172,7 +172,7 @@ Read more about slices in [go blog article](https://go.dev/blog/slices-intro) 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 diff --git a/unit1/exercises/e1/main.go b/unit1/exercises/e1/main.go new file mode 100644 index 0000000..dff2cec --- /dev/null +++ b/unit1/exercises/e1/main.go @@ -0,0 +1,32 @@ +package main + +import "fmt" + +// fibonacci is a function that returns +// a function that returns an int. +func fibonacci() func() int { + x := 0 + y := x + z := x + return func() int { + if z == 0 { + z = 1 + return 0 + } 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 < 10; i++ { + fmt.Println(f()) + } +} 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) + } +} diff --git a/unit1/exercises/e3/main.go b/unit1/exercises/e3/main.go new file mode 100644 index 0000000..358bbb6 --- /dev/null +++ b/unit1/exercises/e3/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "fmt" +) + +type ErrNegativeSqrt float64 + +func (e ErrNegativeSqrt) Error() string { + return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e)) +} + +func Sqrt(x float64) (float64, error) { + //var e ErrNegativeSqrt + epsilon := 0.0000000000001 + z := x / 2 + i := 1 + if x < 0 { + return 0, ErrNegativeSqrt(x) + } + 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 main() { + fmt.Println(Sqrt(2)) + fmt.Println(Sqrt(-2)) +} diff --git a/unit1/exercises/e4/main.go b/unit1/exercises/e4/main.go new file mode 100644 index 0000000..eb9c1d5 --- /dev/null +++ b/unit1/exercises/e4/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "io" + "os" + "strings" + // "fmt" +) + +type rot13Reader struct { + r io.Reader +} + +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 b +} + +func main() { + s := strings.NewReader("Lbh penpxrq gur pbqr!") + r := rot13Reader{s} + io.Copy(os.Stdout, &r) +}