Browse Source

Add remaining code of chapter 02

master
T. Meissner 4 years ago
parent
commit
583f6c8b90
9 changed files with 182 additions and 0 deletions
  1. +27
    -0
      chapter_02/activity_2.02/main.go
  2. +23
    -0
      chapter_02/activity_2.03/main.go
  3. +30
    -0
      chapter_02/exercise_2.05/main.go
  4. +22
    -0
      chapter_02/exercise_2.06/main.go
  5. +17
    -0
      chapter_02/exercise_2.07/main.go
  6. +11
    -0
      chapter_02/exercise_2.08/main.go
  7. +13
    -0
      chapter_02/exercise_2.09/main.go
  8. +17
    -0
      chapter_02/exercise_2.10/main.go
  9. +22
    -0
      chapter_02/exercise_2.11/main.go

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

@ -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)
}

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

@ -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)
}

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

@ -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")
}
}

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

@ -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")
}
}

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

@ -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")
}
}

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

@ -0,0 +1,11 @@
package main
import "fmt"
func main() {
for i := 0; i < 5; i++ {
fmt.Println(i)
}
}

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

@ -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])
}
}

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

@ -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)
}
}

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

@ -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)
}
}

Loading…
Cancel
Save