Browse Source

Add remaining code of chapter 02

T. Meissner 1 month ago
parent
commit
583f6c8b90

+ 27
- 0
chapter_02/activity_2.02/main.go View File

@@ -0,0 +1,27 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  words := map[string]int{
8
+    "Gonna": 3,
9
+    "You":   3,
10
+    "Give":  2,
11
+    "Never": 1,
12
+    "Up":    4,
13
+  }
14
+
15
+  max_word := ""
16
+  max_count := 0
17
+
18
+  for word, count := range words {
19
+    if count > max_count {
20
+      max_word, max_count = word, count
21
+    }
22
+  }
23
+
24
+  fmt.Println("Most popular word:", max_word)
25
+  fmt.Println("With a count of:", max_count)
26
+
27
+}

+ 23
- 0
chapter_02/activity_2.03/main.go View File

@@ -0,0 +1,23 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  numbers := []int{5, 8, 2, 4, 0, 1, 3, 7, 9, 6}
8
+
9
+  fmt.Println("Before:", numbers)
10
+
11
+  for swapped := true; swapped == true; {
12
+    swapped = false
13
+    for i := 1; i < len(numbers); i++ {
14
+      if numbers[i] < numbers[i-1] {
15
+        numbers[i-1], numbers[i] = numbers[i], numbers[i-1]
16
+        swapped = true
17
+      }
18
+    }
19
+  }
20
+
21
+  fmt.Println("After: ", numbers)
22
+
23
+}

+ 30
- 0
chapter_02/exercise_2.05/main.go View File

@@ -0,0 +1,30 @@
1
+package main
2
+
3
+import (
4
+  "fmt"
5
+  "time"
6
+)
7
+
8
+func main() {
9
+
10
+  dayBorn := time.Monday
11
+
12
+  switch dayBorn {
13
+  case time.Monday:
14
+    fmt.Println("Monday's child is fair of face")
15
+  case time.Tuesday:
16
+    fmt.Println("Tuesday's child is full of grace")
17
+  case time.Wednesday:
18
+    fmt.Println("Wednesday's child is fair of woe")
19
+  case time.Thursday:
20
+    fmt.Println("Thursday's child has far to go")
21
+  case time.Friday:
22
+    fmt.Println("Friday's child is loving and giving")
23
+  case time.Saturday:
24
+    fmt.Println("Saturday's child works hard for a living")
25
+  case time.Sunday:
26
+    fmt.Println("Sunday's child is bonny and blithe")
27
+  default:
28
+    fmt.Println("Error, day born not valid")
29
+  }
30
+}

+ 22
- 0
chapter_02/exercise_2.06/main.go View File

@@ -0,0 +1,22 @@
1
+package main
2
+
3
+import (
4
+  "fmt"
5
+  "time"
6
+)
7
+
8
+func main() {
9
+
10
+  dayBorn := time.Sunday
11
+
12
+  switch dayBorn {
13
+  case time.Monday, time.Tuesday, time.Wednesday,
14
+    time.Thursday, time.Friday:
15
+    fmt.Println("Gorn on a weekday")
16
+  case time.Saturday, time.Sunday:
17
+    fmt.Println("Born on the weekend")
18
+  default:
19
+    fmt.Println("Error, day born not valid")
20
+  }
21
+
22
+}

+ 17
- 0
chapter_02/exercise_2.07/main.go View File

@@ -0,0 +1,17 @@
1
+package main
2
+
3
+import (
4
+  "fmt"
5
+  "time"
6
+)
7
+
8
+func main() {
9
+
10
+  switch dayBorn := time.Sunday; {
11
+  case dayBorn == time.Saturday || dayBorn == time.Sunday:
12
+    fmt.Println("Born on the weekend")
13
+  default:
14
+    fmt.Println("Born some other day")
15
+  }
16
+
17
+}

+ 11
- 0
chapter_02/exercise_2.08/main.go View File

@@ -0,0 +1,11 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  for i := 0; i < 5; i++ {
8
+    fmt.Println(i)
9
+  }
10
+
11
+}

+ 13
- 0
chapter_02/exercise_2.09/main.go View File

@@ -0,0 +1,13 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  names := []string{"Jim", "Jane", "Joe", "June"}
8
+
9
+  for i := 0; i < len(names); i++ {
10
+    fmt.Println(names[i])
11
+  }
12
+
13
+}

+ 17
- 0
chapter_02/exercise_2.10/main.go View File

@@ -0,0 +1,17 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  config := map[string]string{
8
+    "debug":    "1",
9
+    "logLevel": "warn",
10
+    "version":  "1.2.1",
11
+  }
12
+
13
+  for key, value := range config {
14
+    fmt.Println(key, "=", value)
15
+  }
16
+
17
+}

+ 22
- 0
chapter_02/exercise_2.11/main.go View File

@@ -0,0 +1,22 @@
1
+package main
2
+
3
+import (
4
+  "fmt"
5
+  "math/rand"
6
+)
7
+
8
+func main() {
9
+
10
+  for {
11
+    r := rand.Intn(8)
12
+    if r%3 == 0 {
13
+      fmt.Println("Skip")
14
+      continue
15
+    } else if r%2 == 0 {
16
+      fmt.Println("Stop")
17
+      break
18
+    }
19
+    fmt.Println(r)
20
+  }
21
+
22
+}