add: if statement exercises

This commit is contained in:
Inanc Gumus
2018-10-27 17:40:20 +03:00
parent 12b09c3174
commit 1d1280d76b
22 changed files with 835 additions and 0 deletions

View File

@ -0,0 +1,34 @@
// 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: Age Seasons
//
// Let's start simple. Print the expected outputs,
// depending on the age variable.
//
// EXPECTED OUTPUT
// If age is greater than 60, print:
// Getting older
// If age is greater than 30, print:
// Getting wiser
// If age is greater than 20, print:
// Adulthood
// If age is greater than 10, print:
// Young blood
// Otherwise, print:
// Booting up
// ---------------------------------------------------------
func main() {
// Change this accordingly to produce the expected outputs
// age := 10
// Type your if statement here.
}

View File

@ -0,0 +1,26 @@
// 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"
func main() {
age := 10
if age > 60 {
fmt.Println("Getting older")
} else if age > 30 {
fmt.Println("Getting wiser")
} else if age > 20 {
fmt.Println("Adulthood")
} else if age > 10 {
fmt.Println("Young blood")
} else {
fmt.Println("Booting up")
}
}

View File

@ -0,0 +1,50 @@
// 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"
// ---------------------------------------------------------
// EXERCISE: Simplify It
//
// Can you simplify the if statement inside the code below?
//
// When:
// isSphere == true and
// radius is equal or greater than 200
//
// It will print "It's a big sphere."
//
// Otherwise, it will print "I don't know."
//
// EXPECTED OUTPUT
// It's a big sphere.
// ---------------------------------------------------------
func main() {
// DO NOT TOUCH THIS
isSphere, radius := true, 100
var big bool
if radius >= 50 {
if radius >= 100 {
if radius >= 200 {
big = true
}
}
}
if big != true {
fmt.Println("I don't know.")
} else if !(isSphere == false) {
fmt.Println("It's a big sphere.")
} else {
fmt.Println("I don't know.")
}
}

View File

@ -0,0 +1,20 @@
// 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"
func main() {
isSphere, radius := true, 200
if isSphere && radius >= 200 {
fmt.Println("It's a big sphere.")
} else {
fmt.Println("I don't know.")
}
}

View File

@ -0,0 +1,32 @@
// 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: Arg Count
//
// 1. Get arguments from command-line.
// 2. Print the expected outputs below depending on the number
// of arguments.
//
// EXPECTED OUTPUT
// go run main.go
// Give me args
//
// go run main.go hello
// There is one: "hello"
//
// go run main.go hi there
// There are two: "hi there"
//
// go run main.go i wanna be a gopher
// There are 5 arguments
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,33 @@
// 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() {
var (
args = os.Args
l = len(args) - 1
)
if l == 0 {
fmt.Println("Give me args")
} else if l == 1 {
fmt.Printf("There is one: %q\n", args[1])
} else if l == 2 {
fmt.Printf(
`There are two: "%s %s"`+"\n",
args[1], args[2],
)
} else {
fmt.Printf("There are %d arguments\n", l)
}
}

View File

@ -0,0 +1,49 @@
// 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: Vowel or Consonant
//
// Detect whether a letter is vowel or consonant.
//
// NOTE
// y or w is called a semi-vowel.
// Check out: https://en.oxforddictionaries.com/explore/is-the-letter-y-a-vowel-or-a-consonant/
//
// HINT
// + You can find the length of an argument using the len function.
//
// + len(os.Args[1]) will give you the length of the 1st argument.
//
// BONUS
// Use strings.IndexAny function to detect the vowels.
// Search on Google for: golang pkg strings IndexAny
//
// EXPECTED OUTPUT
// go run main.go
// Give me a letter
//
// go run main.go hey
// Give me a letter
//
// go run main.go a
// "a" is a vowel.
//
// go run main.go y
// "y" is sometimes a vowel, sometimes not.
//
// go run main.go w
// "w" is sometimes a vowel, sometimes not.
//
// go run main.go x
// "x" is a consonant.
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,34 @@
// 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 || len(args[1]) != 1 {
fmt.Println("Give me a letter")
return
}
// I didn't use a short-if here because, it's already
// hard to read. Do not make it harder.
s := args[1]
if s == "a" || s == "e" || s == "i" || s == "o" || s == "u" {
fmt.Printf("%q is a vowel.\n", s)
} else if s == "w" || s == "y" {
fmt.Printf("%q is sometimes a vowel, sometimes not.\n", s)
} else {
fmt.Printf("%q is a consonant.\n", s)
}
}

View File

@ -0,0 +1,39 @@
// 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"
)
func main() {
args := os.Args
if len(args) != 2 || len(args[1]) != 1 {
fmt.Println("Give me a letter")
return
}
s := args[1]
if strings.IndexAny(s, "aeiou") != -1 {
fmt.Printf("%q is a vowel.\n", s)
} else if s == "w" || s == "y" {
fmt.Printf("%q is sometimes a vowel, sometimes not.\n", s)
} else {
fmt.Printf("%q is a consonant.\n", s)
}
// Notice that:
//
// I didn't use IndexAny for the else if above.
//
// It's because, calling a function is a costly operation.
// And, this way, the code is simpler.
}

View File

@ -0,0 +1,55 @@
// 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
//
// Your boss wants you to create a program that will check
// whether a person can watch a particular movie or not.
//
// Imagine that another program provides the age to your
// program. Depending on what you return, the other program
// will issue the tickets to the person automatically.
//
// EXERCISE: Movie Ratings
//
// 1. Get the age from the command-line.
//
// 2. Return one of the following messages if the age is:
// -> Above 17 : "R-Rated"
// -> Between 13 and 17: "PG-13"
// -> Below 13 : "PG-Rated"
//
// RESTRICTIONS:
// 1. If age data is wrong or absent let the user know.
// 2. Do not accept negative age.
//
// BONUS:
// 1. BONUS: Use if statements only twice throughout your program.
// 2. BONUS: Use an if statement only once.
//
// EXPECTED OUTPUT
// go run main.go 18
// R-Rated
//
// go run main.go 17
// PG-13
//
// go run main.go 12
// PG-Rated
//
// go run main.go
// Requires age
//
// go run main.go -5
// Wrong age: "-5"
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,34 @@
// 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() {
if len(os.Args) != 2 {
fmt.Println("Requires age")
return
}
age, err := strconv.Atoi(os.Args[1])
if err != nil || age < 0 {
fmt.Printf(`Wrong age: %q`+"\n", os.Args[1])
return
} else if age > 17 {
fmt.Println("R-Rated")
} else if age >= 13 && age <= 17 {
fmt.Println("PG-13")
} else if age < 13 {
fmt.Println("PG-Rated")
}
}

View File

@ -0,0 +1,34 @@
// 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"
)
// 🛑 DON'T DO THIS:
// It's hard to read.
// It's just an exercise.
func main() {
if len(os.Args) != 2 {
fmt.Println("Requires age")
return
} else if age, err := strconv.Atoi(os.Args[1]); err != nil || age < 0 {
fmt.Printf(`Wrong age: %q`+"\n", os.Args[1])
return
} else if age > 17 {
fmt.Println("R-Rated")
} else if age >= 13 && age <= 17 {
fmt.Println("PG-13")
} else if age < 13 {
fmt.Println("PG-Rated")
}
}

View File

@ -0,0 +1,39 @@
// 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: Odd or Even
//
// 1. Get a number from the command-line.
//
// 2. Find whether the number is odd, even and divisible by 8.
//
// RESTRICTION
// Handle the error. If the number is not a valid number,
// or it's not provided, let the user know.
//
// EXPECTED OUTPUT
// go run main.go 16
// 16 is an even number and it's divisible by 8
//
// go run main.go 4
// 4 is an even number
//
// go run main.go 3
// 3 is an odd number
//
// go run main.go
// Pick a number
//
// go run main.go ABC
// "ABC" is not a number
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,35 @@
// 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() {
if len(os.Args) != 2 {
fmt.Println("Pick a number")
return
}
n, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Printf("%q is not a number\n", os.Args[1])
return
}
if n%8 == 0 {
fmt.Printf("%d is an even number and it's divisible by 8\n", n)
} else if n%2 == 0 {
fmt.Printf("%d is an even number\n", n)
} else {
fmt.Printf("%d is an odd number\n", n)
}
}

View File

@ -0,0 +1,36 @@
// 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: Leap Year
//
// Find out whether a given year is a leap year or not.
//
// EXPECTED OUTPUT
// go run main.go
// Give me a year number
//
// go run main.go eighties
// "eighties" is not a valid year.
//
// go run main.go 2018
// 2018 is not a leap year.
//
// go run main.go 2019
// 2019 is not a leap year.
//
// go run main.go 2020
// 2020 is a leap year.
//
// go run main.go 2024
// 2024 is a leap year.
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,46 @@
// 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() {
if len(os.Args) != 2 {
fmt.Println("Give me a year number")
return
}
year, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Printf("%q is not a valid year.\n", os.Args[1])
return
}
// Notice that:
// I've intentionally created this solution as verbose
// as I can.
//
// See the next exercise.
var leap bool
if year%400 == 0 {
leap = true
} else if year%4 == 0 {
leap = true
}
if leap {
fmt.Printf("%d is a leap year.\n", year)
} else {
fmt.Printf("%d is not a leap year.\n", year)
}
}

View File

@ -0,0 +1,22 @@
// 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: Simplify the Leap Year
//
// 1. Look at the solution of "the previous exercise".
//
// 2. And simplify the code (especially the if statements!).
//
// EXPECTED OUTPUT
// It's the same as the previous exercise.
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,36 @@
// 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() {
if len(os.Args) != 2 {
fmt.Println("Give me a year number")
return
}
year, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Printf("%q is not a valid year.\n", os.Args[1])
return
}
if year%4 == 0 && (year%100 != 0 || year%400 == 0) {
fmt.Printf("%d is a leap year.\n", year)
} else {
fmt.Printf("%d is not a leap year.\n", year)
}
}
// Review the original source code here:
// https://github.com/golang/go/blob/ad644d2e86bab85787879d41c2d2aebbd7c57db8/src/time/time.go#L1289

View File

@ -0,0 +1,90 @@
// 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: Days in a Month
//
// Print the number of days in a given month.
//
// RESTRICTIONS
// 1. On a leap year, february should print 29. Otherwise, 28.
//
// Set your computer clock to 2020 to see whether it works.
//
// 2. It should work case-insensitive. See below.
//
// Search on Google: golang pkg strings ToLower
//
// 3. Get the current year using the time.Now()
//
// Search on Google: golang pkg time now year
//
//
// EXPECTED OUTPUT
//
// -----------------------------------------
// Your solution should not accept invalid months
// -----------------------------------------
// go run main.go
// Give me a month name
//
// go run main.go sheep
// "sheep" is not a month.
//
// go run main.go january
// "january" has 31 days.
//
// -----------------------------------------
// Your solution should handle the leap years
// -----------------------------------------
// go run main.go february
// "february" has 28 days.
//
// go run main.go march
// "march" has 31 days.
//
// go run main.go april
// "april" has 30 days.
//
// go run main.go may
// "may" has 31 days.
//
// go run main.go june
// "june" has 30 days.
//
// go run main.go july
// "july" has 31 days.
//
// go run main.go august
// "august" has 31 days.
//
// go run main.go september
// "september" has 30 days.
//
// go run main.go october
// "october" has 31 days.
//
// go run main.go november
// "november" has 30 days.
//
// go run main.go december
// "december" has 31 days.
//
// -----------------------------------------
// Your solution should be case insensitive
// -----------------------------------------
// go run main.go DECEMBER
// "DECEMBER" has 31 days.
//
// go run main.go dEcEmBeR
// "dEcEmBeR" has 31 days.
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,58 @@
// 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
}
// get the current year and find out whether it's a leap year
year := time.Now().Year()
leap := year%4 == 0 && (year%100 != 0 || year%400 == 0)
// setting it to 28, saves me typing it below again
days := 28
month := os.Args[1]
// case insensitive
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" {
// try a "logical and operator" above.
// like: `else if m == "february" && leap`
if leap {
days = 29
}
} else {
fmt.Printf("%q is not a month.\n", month)
return
}
fmt.Printf("%q has %d days.\n", month, days)
}

22
11-if/exercises/README.md Normal file
View File

@ -0,0 +1,22 @@
# If Statement
1. **[Age Seasons](https://github.com/inancgumus/learngo/tree/master/11-if/exercises/01-age-seasons)**
2. **[Simplify It](https://github.com/inancgumus/learngo/tree/master/11-if/exercises/02-simplify-it)**
3. **[Arg Count](https://github.com/inancgumus/learngo/tree/master/11-if/exercises/03-arg-count)**
4. **[Vowel or Consonant](https://github.com/inancgumus/learngo/tree/master/11-if/exercises/04-vowel-or-cons)**
## Error Handling
5. **[Movie Ratings](https://github.com/inancgumus/learngo/tree/master/11-if/exercises/05-movie-ratings)**
6. **[Odd or Even](https://github.com/inancgumus/learngo/tree/master/11-if/exercises/06-odd-even)**
7. **[Leap Year](https://github.com/inancgumus/learngo/tree/master/11-if/exercises/07-leap-year)**
8. **[Simplify the Leap Year](https://github.com/inancgumus/learngo/tree/master/11-if/exercises/08-simplify-leap-year)**
9. **[Days in a Month](https://github.com/inancgumus/learngo/tree/master/11-if/exercises/09-days-in-month)**

View File

@ -0,0 +1,11 @@
# Header
What you will learn?
1. **[text](https://github.com/inancgumus/learngo/tree/master/)**
text
2. **[text](https://github.com/inancgumus/learngo/tree/master/)**
text