From 583f6c8b9082c02c8eebbdc034c8745cc43fd880 Mon Sep 17 00:00:00 2001 From: tmeissner Date: Wed, 1 Jul 2020 15:31:45 +0200 Subject: [PATCH] Add remaining code of chapter 02 --- chapter_02/activity_2.02/main.go | 27 +++++++++++++++++++++++++++ chapter_02/activity_2.03/main.go | 23 +++++++++++++++++++++++ chapter_02/exercise_2.05/main.go | 30 ++++++++++++++++++++++++++++++ chapter_02/exercise_2.06/main.go | 22 ++++++++++++++++++++++ chapter_02/exercise_2.07/main.go | 17 +++++++++++++++++ chapter_02/exercise_2.08/main.go | 11 +++++++++++ chapter_02/exercise_2.09/main.go | 13 +++++++++++++ chapter_02/exercise_2.10/main.go | 17 +++++++++++++++++ chapter_02/exercise_2.11/main.go | 22 ++++++++++++++++++++++ 9 files changed, 182 insertions(+) create mode 100644 chapter_02/activity_2.02/main.go create mode 100644 chapter_02/activity_2.03/main.go create mode 100644 chapter_02/exercise_2.05/main.go create mode 100644 chapter_02/exercise_2.06/main.go create mode 100644 chapter_02/exercise_2.07/main.go create mode 100644 chapter_02/exercise_2.08/main.go create mode 100644 chapter_02/exercise_2.09/main.go create mode 100644 chapter_02/exercise_2.10/main.go create mode 100644 chapter_02/exercise_2.11/main.go diff --git a/chapter_02/activity_2.02/main.go b/chapter_02/activity_2.02/main.go new file mode 100644 index 0000000..01dbf85 --- /dev/null +++ b/chapter_02/activity_2.02/main.go @@ -0,0 +1,27 @@ +package main + +import "fmt" + +func main() { + + words := map[string]int{ + "Gonna": 3, + "You": 3, + "Give": 2, + "Never": 1, + "Up": 4, + } + + max_word := "" + max_count := 0 + + for word, count := range words { + if count > max_count { + max_word, max_count = word, count + } + } + + fmt.Println("Most popular word:", max_word) + fmt.Println("With a count of:", max_count) + +} diff --git a/chapter_02/activity_2.03/main.go b/chapter_02/activity_2.03/main.go new file mode 100644 index 0000000..d111cf6 --- /dev/null +++ b/chapter_02/activity_2.03/main.go @@ -0,0 +1,23 @@ +package main + +import "fmt" + +func main() { + + numbers := []int{5, 8, 2, 4, 0, 1, 3, 7, 9, 6} + + fmt.Println("Before:", numbers) + + for swapped := true; swapped == true; { + swapped = false + for i := 1; i < len(numbers); i++ { + if numbers[i] < numbers[i-1] { + numbers[i-1], numbers[i] = numbers[i], numbers[i-1] + swapped = true + } + } + } + + fmt.Println("After: ", numbers) + +} diff --git a/chapter_02/exercise_2.05/main.go b/chapter_02/exercise_2.05/main.go new file mode 100644 index 0000000..58e3f34 --- /dev/null +++ b/chapter_02/exercise_2.05/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + + dayBorn := time.Monday + + switch dayBorn { + case time.Monday: + fmt.Println("Monday's child is fair of face") + case time.Tuesday: + fmt.Println("Tuesday's child is full of grace") + case time.Wednesday: + fmt.Println("Wednesday's child is fair of woe") + case time.Thursday: + fmt.Println("Thursday's child has far to go") + case time.Friday: + fmt.Println("Friday's child is loving and giving") + case time.Saturday: + fmt.Println("Saturday's child works hard for a living") + case time.Sunday: + fmt.Println("Sunday's child is bonny and blithe") + default: + fmt.Println("Error, day born not valid") + } +} diff --git a/chapter_02/exercise_2.06/main.go b/chapter_02/exercise_2.06/main.go new file mode 100644 index 0000000..acee4e8 --- /dev/null +++ b/chapter_02/exercise_2.06/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + + dayBorn := time.Sunday + + switch dayBorn { + case time.Monday, time.Tuesday, time.Wednesday, + time.Thursday, time.Friday: + fmt.Println("Gorn on a weekday") + case time.Saturday, time.Sunday: + fmt.Println("Born on the weekend") + default: + fmt.Println("Error, day born not valid") + } + +} diff --git a/chapter_02/exercise_2.07/main.go b/chapter_02/exercise_2.07/main.go new file mode 100644 index 0000000..013d3f6 --- /dev/null +++ b/chapter_02/exercise_2.07/main.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + + switch dayBorn := time.Sunday; { + case dayBorn == time.Saturday || dayBorn == time.Sunday: + fmt.Println("Born on the weekend") + default: + fmt.Println("Born some other day") + } + +} diff --git a/chapter_02/exercise_2.08/main.go b/chapter_02/exercise_2.08/main.go new file mode 100644 index 0000000..a8dfbe7 --- /dev/null +++ b/chapter_02/exercise_2.08/main.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func main() { + + for i := 0; i < 5; i++ { + fmt.Println(i) + } + +} diff --git a/chapter_02/exercise_2.09/main.go b/chapter_02/exercise_2.09/main.go new file mode 100644 index 0000000..2587cf8 --- /dev/null +++ b/chapter_02/exercise_2.09/main.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +func main() { + + names := []string{"Jim", "Jane", "Joe", "June"} + + for i := 0; i < len(names); i++ { + fmt.Println(names[i]) + } + +} diff --git a/chapter_02/exercise_2.10/main.go b/chapter_02/exercise_2.10/main.go new file mode 100644 index 0000000..f5148b5 --- /dev/null +++ b/chapter_02/exercise_2.10/main.go @@ -0,0 +1,17 @@ +package main + +import "fmt" + +func main() { + + config := map[string]string{ + "debug": "1", + "logLevel": "warn", + "version": "1.2.1", + } + + for key, value := range config { + fmt.Println(key, "=", value) + } + +} diff --git a/chapter_02/exercise_2.11/main.go b/chapter_02/exercise_2.11/main.go new file mode 100644 index 0000000..8c783fa --- /dev/null +++ b/chapter_02/exercise_2.11/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "math/rand" +) + +func main() { + + for { + r := rand.Intn(8) + if r%3 == 0 { + fmt.Println("Skip") + continue + } else if r%2 == 0 { + fmt.Println("Stop") + break + } + fmt.Println(r) + } + +}