add: constants exercises

This commit is contained in:
Inanc Gumus
2018-10-27 17:39:25 +03:00
parent 14de3fbf11
commit 12b09c3174
39 changed files with 39 additions and 800 deletions

View File

@ -8,7 +8,8 @@
package main
// ---------------------------------------------------------
// EXERCISE
// EXERCISE: Minutes in Weeks
//
// Calculate how many minutes in two weeks.
//
// STEPS:

View File

@ -10,7 +10,8 @@ package main
import "fmt"
// ---------------------------------------------------------
// EXERCISE
// EXERCISE: Remove the Magic
//
// Get rid of the magic numbers in the following code.
//
// RESTRICTIONS

View File

@ -8,7 +8,8 @@
package main
// ---------------------------------------------------------
// EXERCISE
// EXERCISE: Constant Length
//
// Calculate how many characters inside the `home`
// constant and print it.
//

View File

@ -8,7 +8,8 @@
package main
// ---------------------------------------------------------
// EXERCISE
// EXERCISE: TAU
//
// Fix the following program and print the TAU number.
//
// HINT

View File

@ -8,7 +8,8 @@
package main
// ---------------------------------------------------------
// EXERCISE
// EXERCISE: Area
//
// Fix the following program.
//
// RESTRICTION

View File

@ -8,7 +8,8 @@
package main
// ---------------------------------------------------------
// EXERCISE
// EXERCISE: No Conversions Allowed
//
// 1. Fix the program without doing any conversion.
// 2. Explain why it doesn't work.
//

View File

@ -10,7 +10,8 @@ package main
import "fmt"
// ---------------------------------------------------------
// EXERCISE
// EXERCISE: Iota Months
//
// 1. Initialize the constants using iota.
// 2. You should find the correct formula for iota.
//

View File

@ -10,7 +10,8 @@ package main
import "fmt"
// ---------------------------------------------------------
// EXERCISE
// EXERCISE: Iota Months #2
//
// 1. Initialize multiple constants using iota.
// 2. Please follow the instructions inside the code.
//

View File

@ -10,8 +10,9 @@ package main
import "fmt"
// ---------------------------------------------------------
// EXERCISE
// Use iota to initialize month constants.
// EXERCISE: Iota Seasons
//
// Use iota to initialize the season constants.
//
// HINT
// You can change the order of the constants.

View File

@ -0,0 +1,20 @@
# Constants
1. **[Minutes in Weeks](https://github.com/inancgumus/learngo/tree/master/10-constants/exercises/01-minutes-in-weeks)**
2. **[Remove the Magic](https://github.com/inancgumus/learngo/tree/master/10-constants/exercises/02-remove-the-magic)**
3. **[Constant Length](https://github.com/inancgumus/learngo/tree/master/10-constants/exercises/03-constant-length)**
4. **[TAU](https://github.com/inancgumus/learngo/tree/master/10-constants/exercises/04-tau)**
5. **[Area](https://github.com/inancgumus/learngo/tree/master/10-constants/exercises/05-area)**
6. **[No Conversions Allowed](https://github.com/inancgumus/learngo/tree/master/10-constants/exercises/06-no-conversions-allowed)**
7. **[Iota Months](https://github.com/inancgumus/learngo/tree/master/10-constants/exercises/07-iota-months)**
8. **[Iota Months #2](https://github.com/inancgumus/learngo/tree/master/10-constants/exercises/08-iota-months-2)**
9. **[Iota Seasons](https://github.com/inancgumus/learngo/tree/master/10-constants/exercises/09-iota-seasons)**

View File

@ -1,33 +0,0 @@
// 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
// 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

@ -1,26 +0,0 @@
// 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

@ -1,49 +0,0 @@
// 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
// 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

@ -1,20 +0,0 @@
// 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

@ -1,31 +0,0 @@
// 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
// 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

@ -1,33 +0,0 @@
// 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

@ -1,37 +0,0 @@
// 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
// 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

@ -1,35 +0,0 @@
// 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

@ -1,53 +0,0 @@
// 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
// 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

@ -1,34 +0,0 @@
// 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

@ -1,34 +0,0 @@
// 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

@ -1,48 +0,0 @@
// 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
// 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

@ -1,34 +0,0 @@
// 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

@ -1,39 +0,0 @@
// 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

@ -1,35 +0,0 @@
// 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
// 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

@ -1,46 +0,0 @@
// 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

@ -1,20 +0,0 @@
// 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
// 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

@ -1,36 +0,0 @@
// 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

@ -1,89 +0,0 @@
// 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
// 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

@ -1,58 +0,0 @@
// 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)
}