Skip to content

Dongle dunkey #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
6 changes: 3 additions & 3 deletions unit1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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
Expand Down
32 changes: 32 additions & 0 deletions unit1/exercises/e1/main.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
29 changes: 29 additions & 0 deletions unit1/exercises/e2/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
36 changes: 36 additions & 0 deletions unit1/exercises/e3/main.go
Original file line number Diff line number Diff line change
@@ -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))
}
35 changes: 35 additions & 0 deletions unit1/exercises/e4/main.go
Original file line number Diff line number Diff line change
@@ -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)
}