diff --git a/10-constants/exercises/01/main.go b/10-constants/exercises/01-minutes-in-weeks/main.go similarity index 95% rename from 10-constants/exercises/01/main.go rename to 10-constants/exercises/01-minutes-in-weeks/main.go index d804bde..e9cf45f 100644 --- a/10-constants/exercises/01/main.go +++ b/10-constants/exercises/01-minutes-in-weeks/main.go @@ -8,7 +8,8 @@ package main // --------------------------------------------------------- -// EXERCISE +// EXERCISE: Minutes in Weeks +// // Calculate how many minutes in two weeks. // // STEPS: diff --git a/10-constants/exercises/01/solution/main.go b/10-constants/exercises/01-minutes-in-weeks/solution/main.go similarity index 100% rename from 10-constants/exercises/01/solution/main.go rename to 10-constants/exercises/01-minutes-in-weeks/solution/main.go diff --git a/10-constants/exercises/02/main.go b/10-constants/exercises/02-remove-the-magic/main.go similarity index 95% rename from 10-constants/exercises/02/main.go rename to 10-constants/exercises/02-remove-the-magic/main.go index ee208f0..96c74f0 100644 --- a/10-constants/exercises/02/main.go +++ b/10-constants/exercises/02-remove-the-magic/main.go @@ -10,7 +10,8 @@ package main import "fmt" // --------------------------------------------------------- -// EXERCISE +// EXERCISE: Remove the Magic +// // Get rid of the magic numbers in the following code. // // RESTRICTIONS diff --git a/10-constants/exercises/02/solution/main.go b/10-constants/exercises/02-remove-the-magic/solution/main.go similarity index 100% rename from 10-constants/exercises/02/solution/main.go rename to 10-constants/exercises/02-remove-the-magic/solution/main.go diff --git a/10-constants/exercises/03/main.go b/10-constants/exercises/03-constant-length/main.go similarity index 96% rename from 10-constants/exercises/03/main.go rename to 10-constants/exercises/03-constant-length/main.go index f3805d0..5e63332 100644 --- a/10-constants/exercises/03/main.go +++ b/10-constants/exercises/03-constant-length/main.go @@ -8,7 +8,8 @@ package main // --------------------------------------------------------- -// EXERCISE +// EXERCISE: Constant Length +// // Calculate how many characters inside the `home` // constant and print it. // diff --git a/10-constants/exercises/03/solution/main.go b/10-constants/exercises/03-constant-length/solution/main.go similarity index 100% rename from 10-constants/exercises/03/solution/main.go rename to 10-constants/exercises/03-constant-length/solution/main.go diff --git a/10-constants/exercises/04/main.go b/10-constants/exercises/04-tau/main.go similarity index 96% rename from 10-constants/exercises/04/main.go rename to 10-constants/exercises/04-tau/main.go index 3fc51df..6941c12 100644 --- a/10-constants/exercises/04/main.go +++ b/10-constants/exercises/04-tau/main.go @@ -8,7 +8,8 @@ package main // --------------------------------------------------------- -// EXERCISE +// EXERCISE: TAU +// // Fix the following program and print the TAU number. // // HINT diff --git a/10-constants/exercises/04/solution/main.go b/10-constants/exercises/04-tau/solution/main.go similarity index 100% rename from 10-constants/exercises/04/solution/main.go rename to 10-constants/exercises/04-tau/solution/main.go diff --git a/10-constants/exercises/05/main.go b/10-constants/exercises/05-area/main.go similarity index 96% rename from 10-constants/exercises/05/main.go rename to 10-constants/exercises/05-area/main.go index ed7ec1f..bd76c85 100644 --- a/10-constants/exercises/05/main.go +++ b/10-constants/exercises/05-area/main.go @@ -8,7 +8,8 @@ package main // --------------------------------------------------------- -// EXERCISE +// EXERCISE: Area +// // Fix the following program. // // RESTRICTION diff --git a/10-constants/exercises/05/solution/main.go b/10-constants/exercises/05-area/solution/main.go similarity index 100% rename from 10-constants/exercises/05/solution/main.go rename to 10-constants/exercises/05-area/solution/main.go diff --git a/10-constants/exercises/06/main.go b/10-constants/exercises/06-no-conversions-allowed/main.go similarity index 93% rename from 10-constants/exercises/06/main.go rename to 10-constants/exercises/06-no-conversions-allowed/main.go index 9cdf4f8..73e1f8b 100644 --- a/10-constants/exercises/06/main.go +++ b/10-constants/exercises/06-no-conversions-allowed/main.go @@ -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. // diff --git a/10-constants/exercises/06/solution/main.go b/10-constants/exercises/06-no-conversions-allowed/solution/main.go similarity index 100% rename from 10-constants/exercises/06/solution/main.go rename to 10-constants/exercises/06-no-conversions-allowed/solution/main.go diff --git a/10-constants/exercises/07/main.go b/10-constants/exercises/07-iota-months/main.go similarity index 96% rename from 10-constants/exercises/07/main.go rename to 10-constants/exercises/07-iota-months/main.go index e1a8b59..6e5a948 100644 --- a/10-constants/exercises/07/main.go +++ b/10-constants/exercises/07-iota-months/main.go @@ -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. // diff --git a/10-constants/exercises/07/solution/main.go b/10-constants/exercises/07-iota-months/solution/main.go similarity index 100% rename from 10-constants/exercises/07/solution/main.go rename to 10-constants/exercises/07-iota-months/solution/main.go diff --git a/10-constants/exercises/08/main.go b/10-constants/exercises/08-iota-months-2/main.go similarity index 96% rename from 10-constants/exercises/08/main.go rename to 10-constants/exercises/08-iota-months-2/main.go index 3cc8bb3..7c0a1f9 100644 --- a/10-constants/exercises/08/main.go +++ b/10-constants/exercises/08-iota-months-2/main.go @@ -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. // diff --git a/10-constants/exercises/08/solution/main.go b/10-constants/exercises/08-iota-months-2/solution/main.go similarity index 100% rename from 10-constants/exercises/08/solution/main.go rename to 10-constants/exercises/08-iota-months-2/solution/main.go diff --git a/10-constants/exercises/09/main.go b/10-constants/exercises/09-iota-seasons/main.go similarity index 89% rename from 10-constants/exercises/09/main.go rename to 10-constants/exercises/09-iota-seasons/main.go index 5fc9f4a..dd0dac2 100644 --- a/10-constants/exercises/09/main.go +++ b/10-constants/exercises/09-iota-seasons/main.go @@ -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. diff --git a/10-constants/exercises/09/solution/main.go b/10-constants/exercises/09-iota-seasons/solution/main.go similarity index 100% rename from 10-constants/exercises/09/solution/main.go rename to 10-constants/exercises/09-iota-seasons/solution/main.go diff --git a/10-constants/exercises/README.md b/10-constants/exercises/README.md new file mode 100644 index 0000000..a15af85 --- /dev/null +++ b/10-constants/exercises/README.md @@ -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)** + diff --git a/11-if/exercises/01/main.go b/11-if/exercises/01/main.go deleted file mode 100644 index cf1b0b8..0000000 --- a/11-if/exercises/01/main.go +++ /dev/null @@ -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. -} diff --git a/11-if/exercises/01/solution/main.go b/11-if/exercises/01/solution/main.go deleted file mode 100644 index 80e543b..0000000 --- a/11-if/exercises/01/solution/main.go +++ /dev/null @@ -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") - } -} diff --git a/11-if/exercises/02/main.go b/11-if/exercises/02/main.go deleted file mode 100644 index 602dee2..0000000 --- a/11-if/exercises/02/main.go +++ /dev/null @@ -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.") - } -} diff --git a/11-if/exercises/02/solution/main.go b/11-if/exercises/02/solution/main.go deleted file mode 100644 index 5eb4e39..0000000 --- a/11-if/exercises/02/solution/main.go +++ /dev/null @@ -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.") - } -} diff --git a/11-if/exercises/03/main.go b/11-if/exercises/03/main.go deleted file mode 100644 index a4074b0..0000000 --- a/11-if/exercises/03/main.go +++ /dev/null @@ -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() { -} diff --git a/11-if/exercises/03/solution/main.go b/11-if/exercises/03/solution/main.go deleted file mode 100644 index a69911e..0000000 --- a/11-if/exercises/03/solution/main.go +++ /dev/null @@ -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) - } -} diff --git a/11-if/exercises/04/main.go b/11-if/exercises/04/main.go deleted file mode 100644 index 93d9dea..0000000 --- a/11-if/exercises/04/main.go +++ /dev/null @@ -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() { -} diff --git a/11-if/exercises/04/solution/main.go b/11-if/exercises/04/solution/main.go deleted file mode 100644 index 03dea19..0000000 --- a/11-if/exercises/04/solution/main.go +++ /dev/null @@ -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) - } -} diff --git a/11-if/exercises/05/main.go b/11-if/exercises/05/main.go deleted file mode 100644 index cf38483..0000000 --- a/11-if/exercises/05/main.go +++ /dev/null @@ -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() { -} diff --git a/11-if/exercises/05/solution/main.go b/11-if/exercises/05/solution/main.go deleted file mode 100644 index 6aa5ebc..0000000 --- a/11-if/exercises/05/solution/main.go +++ /dev/null @@ -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") - } -} diff --git a/11-if/exercises/05/solution2/main.go b/11-if/exercises/05/solution2/main.go deleted file mode 100644 index 79392d0..0000000 --- a/11-if/exercises/05/solution2/main.go +++ /dev/null @@ -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") - } -} diff --git a/11-if/exercises/06/main.go b/11-if/exercises/06/main.go deleted file mode 100644 index 1ff2c76..0000000 --- a/11-if/exercises/06/main.go +++ /dev/null @@ -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() { -} diff --git a/11-if/exercises/06/solution/main.go b/11-if/exercises/06/solution/main.go deleted file mode 100644 index 7115aaa..0000000 --- a/11-if/exercises/06/solution/main.go +++ /dev/null @@ -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) - } -} diff --git a/11-if/exercises/06/solution2/main.go b/11-if/exercises/06/solution2/main.go deleted file mode 100644 index fe714df..0000000 --- a/11-if/exercises/06/solution2/main.go +++ /dev/null @@ -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. -} diff --git a/11-if/exercises/07/main.go b/11-if/exercises/07/main.go deleted file mode 100644 index 0232081..0000000 --- a/11-if/exercises/07/main.go +++ /dev/null @@ -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() { -} diff --git a/11-if/exercises/07/solution/main.go b/11-if/exercises/07/solution/main.go deleted file mode 100644 index fa0c780..0000000 --- a/11-if/exercises/07/solution/main.go +++ /dev/null @@ -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) - } -} diff --git a/11-if/exercises/08/main.go b/11-if/exercises/08/main.go deleted file mode 100644 index cc97d78..0000000 --- a/11-if/exercises/08/main.go +++ /dev/null @@ -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() { -} diff --git a/11-if/exercises/08/solution/main.go b/11-if/exercises/08/solution/main.go deleted file mode 100644 index 7c6c018..0000000 --- a/11-if/exercises/08/solution/main.go +++ /dev/null @@ -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 diff --git a/11-if/exercises/09/main.go b/11-if/exercises/09/main.go deleted file mode 100644 index 8ad76de..0000000 --- a/11-if/exercises/09/main.go +++ /dev/null @@ -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() { -} diff --git a/11-if/exercises/09/solution/main.go b/11-if/exercises/09/solution/main.go deleted file mode 100644 index e51ee9f..0000000 --- a/11-if/exercises/09/solution/main.go +++ /dev/null @@ -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) -}