add: switch statement exercises

This commit is contained in:
Inanc Gumus
2018-10-21 00:21:33 +03:00
parent 7bd3143076
commit a29cb99fcb
11 changed files with 444 additions and 31 deletions

View File

@ -0,0 +1,72 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
// ---------------------------------------------------------
// STORY
// You're curious about the richter scales. When reporters
// say: "There's been a 5.5 magnitude earthquake",
//
// You want to know what that means.
//
// So, you've decided to write a program to do that for you.
//
// EXERCISE
// 1. Get the earthquake magnitude from the command-line.
// 2. Display its corresponding description.
//
// ---------------------------------------------------------
// MAGNITUDE DESCRIPTION
// ---------------------------------------------------------
// Less than 2.0 micro
// 2.0 to less than 3.0 very minor
// 3.0 to less than 4.0 minor
// 4.0 to less than 5.0 light
// 5.0 to less than 6.0 moderate
// 6.0 to less than 7.0 strong
// 7.0 to less than 8.0 major
// 8.0 to less than 10.0 great
// 10.0 or more massive
//
// EXPECTED OUTPUT
// go run main.go
// Give me the magnitude of the earthquake.
//
// go run main.go ABC
// I couldn't get that, sorry.
//
// go run main.go 0.5
// 0.50 is micro
//
// go run main.go 2.5
// 2.50 is very minor
//
// go run main.go 3
// 3.00 is minor
//
// go run main.go 4.5
// 4.50 is light
//
// go run main.go 5
// 5.00 is moderate
//
// go run main.go 6
// 6.00 is strong
//
// go run main.go 7
// 7.00 is major
//
// go run main.go 8
// 8.00 is great
//
// go run main.go 11
// 11.00 is massive
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,53 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"os"
"strconv"
)
func main() {
args := os.Args
if len(args) != 2 {
fmt.Println("Give me the magnitude of the earthquake.")
return
}
richter, err := strconv.ParseFloat(args[1], 64)
if err != nil {
fmt.Println("I couldn't get that, sorry.")
return
}
var desc string
switch r := richter; {
case r < 2:
desc = "micro"
case r >= 2 && r < 3:
desc = "very minor"
case r >= 3 && r < 4:
desc = "minor"
case r >= 4 && r < 5:
desc = "light"
case r >= 5 && r < 6:
desc = "moderate"
case r >= 6 && r < 7:
desc = "strong"
case r >= 7 && r < 8:
desc = "major"
case r >= 8 && r < 10:
desc = "great"
default:
desc = "meteoric"
}
fmt.Printf("%.2f is %s\n", richter, desc)
}

View File

@ -0,0 +1,68 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
// ---------------------------------------------------------
// EXERCISE
// Repeat the previous exercise.
//
// But, this time, get the description and print the
// corresponding richter scale.
//
// See the expected outputs.
//
// ---------------------------------------------------------
// MAGNITUDE DESCRIPTION
// ---------------------------------------------------------
// Less than 2.0 micro
// 2.0 to less than 3.0 very minor
// 3.0 to less than 4.0 minor
// 4.0 to less than 5.0 light
// 5.0 to less than 6.0 moderate
// 6.0 to less than 7.0 strong
// 7.0 to less than 8.0 major
// 8.0 to less than 10.0 great
// 10.0 or more massive
//
// EXPECTED OUTPUT
// go run main.go
// Tell me the magnitude of the earthquake in human terms.
//
// go run main.go aliens
// alien's richter scale is unknown
//
// go run main.go micro
// micro's richter scale is less than 2.0
//
// go run main.go "very minor"
// very minor's richter scale is 2 - 2.9
//
// go run main.go minor
// minor's richter scale is 3 - 3.9
//
// go run main.go light
// light's richter scale is 4 - 4.9
//
// go run main.go moderate
// moderate's richter scale is 5 - 5.9
//
// go run main.go strong
// strong's richter scale is 6 - 6.9
//
// go run main.go major
// major's richter scale is 7 - 7.9
//
// go run main.go great
// great's richter scale is 8 - 8.9
//
// go run main.go massive
// massive's richter scale is 10+
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,53 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"os"
)
func main() {
args := os.Args
if len(args) != 2 {
fmt.Println("Tell me the magnitude of the earthquake in human terms.")
return
}
var richter string
desc := args[1]
switch desc {
case "micro":
richter = "less than 2.0"
case "very minor":
richter = "2 - 2.9"
case "minor":
richter = "3 - 3.9"
case "light":
richter = "4 - 4.9"
case "moderate":
richter = "5 - 5.9"
case "strong":
richter = "6 - 6.9"
case "major":
richter = "7 - 7.9"
case "great":
richter = "8 - 8.9"
case "massive":
richter = "10+"
default:
richter = "unknown"
}
fmt.Printf(
"%s's richter scale is %s\n",
desc, richter,
)
}

View File

@ -13,34 +13,8 @@ import (
) )
// --------------------------------------------------------- // ---------------------------------------------------------
// CHALLENGE // EXERCISE
// Convert the if statement into a switch statement. // Convert the if statement to a switch statement.
//
// EXAMPLE USERS
// username: jack
// password: 1888
//
// username: inanc
// password: 1879
//
// EXPECTED OUTPUT
// go run main.go
// Usage: [username] [password]
//
// go run main.go hacker 42
// Access denied for "hacker".
//
// go run main.go jack 1888
// Access granted to "jack".
//
// go run main.go inanc 1879
// Access granted to "inanc".
//
// go run main.go jack 1879
// Invalid password for "jack".
//
// go run main.go inanc 1888
// Invalid password for "inanc".
// --------------------------------------------------------- // ---------------------------------------------------------
const ( const (
@ -62,7 +36,9 @@ func main() {
u, p := args[1], args[2] u, p := args[1], args[2]
// refactor this into switch //
// REFACTOR THIS TO A SWITCH
//
if u != user && u != user2 { if u != user && u != user2 {
fmt.Printf(errUser, u) fmt.Printf(errUser, u)
} else if u == user && p == pass { } else if u == user && p == pass {

View File

@ -31,10 +31,12 @@ func main() {
u, p := args[1], args[2] u, p := args[1], args[2]
// More readable, right? 👍
switch { switch {
case u != user && u != user2: case u != user && u != user2:
fmt.Printf(errUser, u) fmt.Printf(errUser, u)
case u == user && p == pass: case u == user && p == pass:
// notice this one (no more duplication)
fallthrough fallthrough
case u == user2 && p == pass2: case u == user2 && p == pass2:
fmt.Printf(accessOK, u) fmt.Printf(accessOK, u)

View File

@ -0,0 +1,45 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
// ---------------------------------------------------------
// STORY
// You want to write a program that will manipulate the
// given strings to uppercase, lowercase, and title case.
//
// EXERCISE
// 1. Get the operation as the first argument.
// 2. Get the string to be manipulated as the 2nd argument.
//
// HINT
// You can find the manipulation functions in the strings
// package Go documentation (ToLower, ToUpper, Title).
//
// EXPECTED OUTPUT
//
// go run main.go
// [command] [string]
//
// Available commands: lower, upper and title
//
// go run main.go lower 'OMG!'
// omg!
//
// go run main.go upper 'omg!'
// OMG!
//
// go run main.go title "mr. charles darwin"
// Mr. Charles Darwin
//
// go run main.go genius "mr. charles darwin"
// Unknown command: "genius"
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,38 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"os"
"strings"
)
const usage = `[command] [string]
Available commands: lower, upper and title`
func main() {
args := os.Args
if len(args) != 3 {
fmt.Println(usage)
return
}
cmd, str := args[1], args[2]
switch cmd {
case "lower":
fmt.Println(strings.ToLower(str))
case "upper":
fmt.Println(strings.ToUpper(str))
case "title":
fmt.Println(strings.Title(str))
default:
fmt.Printf("Unknown command: %q\n", cmd)
}
}

View File

@ -0,0 +1,61 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"os"
"strings"
"time"
)
// ---------------------------------------------------------
// EXERCISE
// Refactor the previous exercise from the if statement
// section.
//
// "Print the number of days in a given month."
//
// Use a switch statement instead of if statements.
// ---------------------------------------------------------
func main() {
if len(os.Args) != 2 {
fmt.Println("Give me a month name")
return
}
year := time.Now().Year()
leap := year%4 == 0 && (year%100 != 0 || year%400 == 0)
days, month := 28, os.Args[1]
if m := strings.ToLower(month); m == "april" ||
m == "june" ||
m == "september" ||
m == "november" {
days = 30
} else if m == "january" ||
m == "march" ||
m == "may" ||
m == "july" ||
m == "august" ||
m == "october" ||
m == "december" {
days = 31
} else if m == "february" {
if leap {
days = 29
}
} else {
fmt.Printf("%q is not a month.\n", month)
return
}
fmt.Printf("%q has %d days.\n", month, days)
}

View File

@ -0,0 +1,44 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"os"
"strings"
"time"
)
func main() {
if len(os.Args) != 2 {
fmt.Println("Give me a month name")
return
}
year := time.Now().Year()
leap := year%4 == 0 && (year%100 != 0 || year%400 == 0)
days, month := 28, os.Args[1]
switch strings.ToLower(month) {
case "april", "june", "september", "november":
days = 30
case "january", "march", "may", "july",
"august", "october", "december":
days = 31
case "february":
if leap {
days = 29
}
default:
fmt.Printf("%q is not a month.\n", month)
return
}
fmt.Printf("%q has %d days.\n", month, days)
}

View File

@ -1,3 +1,4 @@
## ? ## ?
* text *CORRECT* 1. text *CORRECT*
* text 2. text
E