Initial commit
This commit is contained in:
21
13-loops/exercises/01-loops/01-sum-the-numbers/main.go
Normal file
21
13-loops/exercises/01-loops/01-sum-the-numbers/main.go
Normal file
@ -0,0 +1,21 @@
|
||||
// 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: Sum the numbers
|
||||
//
|
||||
// 1. By using a loop, sum the numbers between 1 and 10.
|
||||
// 2. Print the sum.
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// Sum: 55
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
@ -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() {
|
||||
var sum int
|
||||
for i := 1; i <= 10; i++ {
|
||||
sum += i
|
||||
}
|
||||
fmt.Println("Sum:", sum)
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
// 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: Sum the numbers verbose edition
|
||||
//
|
||||
// By using a loop, sum the numbers between 1 and 10.
|
||||
//
|
||||
// HINT
|
||||
// 1. For printing it as in the expected output, use Print
|
||||
// and Printf functions. They don't print a newline
|
||||
// automatically (unlike a Println).
|
||||
//
|
||||
// 2. Also, you need to use an if statement to prevent
|
||||
// printing the last plus sign.
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
// 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() {
|
||||
const min, max = 1, 10
|
||||
|
||||
var sum int
|
||||
for i := min; i <= max; i++ {
|
||||
sum += i
|
||||
|
||||
fmt.Print(i)
|
||||
if i != max {
|
||||
fmt.Print(" + ")
|
||||
}
|
||||
}
|
||||
fmt.Printf(" = %d\n", sum)
|
||||
}
|
38
13-loops/exercises/01-loops/03-sum-up-to-n/main.go
Normal file
38
13-loops/exercises/01-loops/03-sum-up-to-n/main.go
Normal 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
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Sum up to N
|
||||
//
|
||||
// 1. Get two numbers from the command-line: min and max
|
||||
// 2. Convert them to integers (using Atoi)
|
||||
// 3. By using a loop, sum the numbers between min and max
|
||||
//
|
||||
// RESTRICTIONS
|
||||
// 1. Be sure to handle the errors. So, if a user doesn't
|
||||
// pass enough arguments or she passes non-numerics,
|
||||
// then warn the user and exit from the program.
|
||||
//
|
||||
// 2. Also, check that, min < max.
|
||||
//
|
||||
// HINT
|
||||
// For converting the numbers, you can use `strconv.Atoi`.
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// Let's suppose that the user runs it like this:
|
||||
//
|
||||
// go run main.go 1 10
|
||||
//
|
||||
// Then it should print:
|
||||
//
|
||||
// 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
39
13-loops/exercises/01-loops/03-sum-up-to-n/solution/main.go
Normal file
39
13-loops/exercises/01-loops/03-sum-up-to-n/solution/main.go
Normal 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"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 3 {
|
||||
fmt.Println("gimme two numbers")
|
||||
return
|
||||
}
|
||||
|
||||
min, err1 := strconv.Atoi(os.Args[1])
|
||||
max, err2 := strconv.Atoi(os.Args[2])
|
||||
if err1 != nil || err2 != nil || min >= max {
|
||||
fmt.Println("wrong numbers")
|
||||
return
|
||||
}
|
||||
|
||||
var sum int
|
||||
for i := min; i <= max; i++ {
|
||||
sum += i
|
||||
|
||||
fmt.Print(i)
|
||||
if i != max {
|
||||
fmt.Print(" + ")
|
||||
}
|
||||
}
|
||||
fmt.Printf(" = %d\n", sum)
|
||||
}
|
30
13-loops/exercises/01-loops/04-only-evens/main.go
Normal file
30
13-loops/exercises/01-loops/04-only-evens/main.go
Normal file
@ -0,0 +1,30 @@
|
||||
// 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: Only evens
|
||||
//
|
||||
// 1. Extend the "Sum up to N" exercise
|
||||
// 2. Sum only the even numbers
|
||||
//
|
||||
// RESTRICTIONS
|
||||
// Skip odd numbers using the `continue` statement
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// Let's suppose that the user runs it like this:
|
||||
//
|
||||
// go run main.go 1 10
|
||||
//
|
||||
// Then it should print:
|
||||
//
|
||||
// 2 + 4 + 6 + 8 + 10 = 30
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
42
13-loops/exercises/01-loops/04-only-evens/solution/main.go
Normal file
42
13-loops/exercises/01-loops/04-only-evens/solution/main.go
Normal file
@ -0,0 +1,42 @@
|
||||
// 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) < 3 {
|
||||
fmt.Println("gimme two numbers")
|
||||
return
|
||||
}
|
||||
|
||||
min, err1 := strconv.Atoi(os.Args[1])
|
||||
max, err2 := strconv.Atoi(os.Args[2])
|
||||
if err1 != nil || err2 != nil || min >= max {
|
||||
fmt.Println("wrong numbers")
|
||||
return
|
||||
}
|
||||
|
||||
var sum int
|
||||
for i := min; i <= max; i++ {
|
||||
if i%2 != 0 {
|
||||
continue
|
||||
}
|
||||
sum += i
|
||||
|
||||
fmt.Print(i)
|
||||
if i != max {
|
||||
fmt.Print(" + ")
|
||||
}
|
||||
}
|
||||
fmt.Printf(" = %d\n", sum)
|
||||
}
|
30
13-loops/exercises/01-loops/05-break-up/main.go
Normal file
30
13-loops/exercises/01-loops/05-break-up/main.go
Normal file
@ -0,0 +1,30 @@
|
||||
// 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: Break up
|
||||
//
|
||||
// 1. Extend the "Only Evens" exercise
|
||||
// 2. This time, use an infinite loop.
|
||||
// 3. Break the loop when it reaches to the `max`.
|
||||
//
|
||||
// RESTRICTIONS
|
||||
// You should use the `break` statement once.
|
||||
//
|
||||
// HINT
|
||||
// Do not forget incrementing the `i` before the `continue`
|
||||
// statement and at the end of the loop.
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// go run main.go 1 10
|
||||
// 2 + 4 + 6 + 8 + 10 = 30
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
50
13-loops/exercises/01-loops/05-break-up/solution/main.go
Normal file
50
13-loops/exercises/01-loops/05-break-up/solution/main.go
Normal 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"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 3 {
|
||||
fmt.Println("gimme two numbers")
|
||||
return
|
||||
}
|
||||
|
||||
min, err1 := strconv.Atoi(os.Args[1])
|
||||
max, err2 := strconv.Atoi(os.Args[2])
|
||||
if err1 != nil || err2 != nil || min >= max {
|
||||
fmt.Println("wrong numbers")
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
i = min
|
||||
sum int
|
||||
)
|
||||
|
||||
for {
|
||||
if i%2 != 0 {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
sum += i
|
||||
|
||||
fmt.Print(i)
|
||||
if i != max {
|
||||
fmt.Print(" + ")
|
||||
} else {
|
||||
break
|
||||
}
|
||||
i++
|
||||
}
|
||||
fmt.Printf(" = %d\n", sum)
|
||||
}
|
42
13-loops/exercises/01-loops/06-infinite-kill/main.go
Normal file
42
13-loops/exercises/01-loops/06-infinite-kill/main.go
Normal file
@ -0,0 +1,42 @@
|
||||
// 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: Infinite Kill
|
||||
//
|
||||
// 1. Create an infinite loop
|
||||
// 2. On each step of the loop print a random character.
|
||||
// 3. And, sleep for 250 milliseconds.
|
||||
// 4. Run the program and wait a couple of seconds
|
||||
// then kill it using CTRL+C keys
|
||||
//
|
||||
// RESTRICTIONS
|
||||
// 1. Print one of those characters randomly: \ / |
|
||||
// 2. Before printing a character print also this
|
||||
// escape sequence: \r
|
||||
//
|
||||
// Like this: "\r/", or this: "\r|", and so on...
|
||||
//
|
||||
// HINTS
|
||||
// 1. Use `time.Sleep` to sleep.
|
||||
// 2. You should pass a `time.Duration` value to it.
|
||||
// 3. Check out the Go online documentation for the
|
||||
// `Millisecond` constant.
|
||||
// 4. When printing, do not use a newline! Or a Println!
|
||||
// Use Print or Printf instead.
|
||||
//
|
||||
// NOTE
|
||||
// If this exercise is hard for you now, wait until the
|
||||
// lucky number lecture. Even then so, then just skip it.
|
||||
//
|
||||
// You can return back to it afterwards.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
// 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"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
for {
|
||||
var c string
|
||||
|
||||
switch rand.Intn(3) {
|
||||
case 0:
|
||||
c = "\\"
|
||||
case 1:
|
||||
c = "/"
|
||||
case 2:
|
||||
c = "|"
|
||||
}
|
||||
fmt.Printf("\r%s", c)
|
||||
time.Sleep(time.Millisecond * 150)
|
||||
}
|
||||
}
|
33
13-loops/exercises/01-loops/07-crunch-the-primes/main.go
Normal file
33
13-loops/exercises/01-loops/07-crunch-the-primes/main.go
Normal 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
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Crunch the primes
|
||||
//
|
||||
// 1. Get numbers from the command-line.
|
||||
// 2. `for range` over them.
|
||||
// 4. Convert each one to an int.
|
||||
// 5. If one of the numbers isn't an int, just skip it.
|
||||
// 6. Print the ones that are only the prime numbers.
|
||||
//
|
||||
// RESTRICTION
|
||||
// The user can run the program with any number of
|
||||
// arguments.
|
||||
//
|
||||
// HINT
|
||||
// Find whether a number is prime using this algorithm:
|
||||
// https://stackoverflow.com/a/1801446/115363
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// go run main.go 2 4 6 7 a 9 c 11 x 12 13
|
||||
// 2 7 11 13
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
// 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() {
|
||||
// remember [1:] skips the first argument
|
||||
|
||||
main:
|
||||
for _, arg := range os.Args[1:] {
|
||||
n, err := strconv.Atoi(arg)
|
||||
if err != nil {
|
||||
// skip non-numerics
|
||||
continue
|
||||
}
|
||||
|
||||
switch {
|
||||
// not a prime
|
||||
case n <= 0 || n%2 == 0 || n%3 == 0:
|
||||
continue
|
||||
|
||||
// prime
|
||||
case n == 2 || n == 3:
|
||||
fmt.Print(n, " ")
|
||||
continue
|
||||
}
|
||||
|
||||
for i, w := 5, 2; i*i <= n; {
|
||||
// not a prime
|
||||
if n%i == 0 {
|
||||
continue main
|
||||
}
|
||||
|
||||
i += w
|
||||
w = 6 - w
|
||||
}
|
||||
|
||||
// all checks ok: it's a prime
|
||||
fmt.Print(n, " ")
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
// 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: First Turn Winner
|
||||
//
|
||||
// If the player wins on the first turn, then display
|
||||
// a special bonus message.
|
||||
//
|
||||
// RESTRICTION
|
||||
// The first picked random number by the computer should
|
||||
// match the player's guess.
|
||||
//
|
||||
// EXAMPLE SCENARIO
|
||||
// 1. The player guesses 6
|
||||
// 2. The computer picks a random number and it happens
|
||||
// to be 6
|
||||
// 3. Terminate the game and print the bonus message
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
// 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"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
maxTurns = 5 // less is more difficult
|
||||
usage = `Welcome to the Lucky Number Game!
|
||||
|
||||
The program will pick %d random numbers.
|
||||
Your mission is to guess one of those numbers.
|
||||
|
||||
The greater your number is, harder it gets.
|
||||
|
||||
Wanna play?
|
||||
`
|
||||
)
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
args := os.Args[1:]
|
||||
|
||||
if len(args) != 1 {
|
||||
fmt.Printf(usage, maxTurns)
|
||||
return
|
||||
}
|
||||
|
||||
guess, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
fmt.Println("Not a number.")
|
||||
return
|
||||
}
|
||||
|
||||
if guess < 0 {
|
||||
fmt.Println("Please pick a positive number.")
|
||||
return
|
||||
}
|
||||
|
||||
for turn := 1; turn <= maxTurns; turn++ {
|
||||
n := rand.Intn(guess + 1)
|
||||
|
||||
if n == guess {
|
||||
if turn == 1 {
|
||||
fmt.Println("🥇 FIRST TIME WINNER!!!")
|
||||
} else {
|
||||
fmt.Println("🎉 YOU WON!")
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("☠️ YOU LOST... Try again?")
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
// 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: Random Messages
|
||||
//
|
||||
// Display a few different won and lost messages "randomly".
|
||||
//
|
||||
// HINTS
|
||||
// 1. You can use a switch statement to do that.
|
||||
// 2. Set its condition to the random number generator.
|
||||
// 3. I would use a short switch.
|
||||
//
|
||||
// EXAMPLES
|
||||
// Player wins: then randomly printone of these:
|
||||
//
|
||||
// go run main.go 5
|
||||
// YOU WON
|
||||
// go run main.go 5
|
||||
// YOU'RE AWESOME
|
||||
//
|
||||
// Player loses: then randomly printone of these:
|
||||
//
|
||||
// go run main.go 5
|
||||
// LOSER!
|
||||
// go run main.go 5
|
||||
// YOU LOST. TRY AGAIN?
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
// 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"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
maxTurns = 5 // less is more difficult
|
||||
usage = `Welcome to the Lucky Number Game!
|
||||
|
||||
The program will pick %d random numbers.
|
||||
Your mission is to guess one of those numbers.
|
||||
|
||||
The greater your number is, harder it gets.
|
||||
|
||||
Wanna play?
|
||||
`
|
||||
)
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
args := os.Args[1:]
|
||||
|
||||
if len(args) != 1 {
|
||||
fmt.Printf(usage, maxTurns)
|
||||
return
|
||||
}
|
||||
|
||||
guess, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
fmt.Println("Not a number.")
|
||||
return
|
||||
}
|
||||
|
||||
if guess < 0 {
|
||||
fmt.Println("Please pick a positive number.")
|
||||
return
|
||||
}
|
||||
|
||||
for turn := 1; turn <= maxTurns; turn++ {
|
||||
n := rand.Intn(guess + 1)
|
||||
|
||||
if n == guess {
|
||||
switch rand.Intn(3) {
|
||||
case 0:
|
||||
fmt.Println("🎉 YOU WIN!")
|
||||
case 1:
|
||||
fmt.Println("🎉 YOU'RE AWESOME!")
|
||||
case 2:
|
||||
fmt.Println("🎉 PERFECT!")
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
msg := "%s Try again?\n"
|
||||
|
||||
switch rand.Intn(2) {
|
||||
case 0:
|
||||
fmt.Printf(msg, "☠️ YOU LOST...")
|
||||
case 1:
|
||||
fmt.Printf(msg, "☠️ JUST A BAD LUCK...")
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
// 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: Double Guesses
|
||||
//
|
||||
// Let the player guess two numbers instead of one.
|
||||
//
|
||||
// HINT:
|
||||
// Generate random numbers using the greatest number
|
||||
// among the guessed numbers.
|
||||
//
|
||||
// EXAMPLES
|
||||
// go run main.go 5 6
|
||||
// Player wins if the random number is either 5 or 6.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
// 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"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
maxTurns = 5 // less is more difficult
|
||||
usage = `Welcome to the Lucky Number Game!
|
||||
|
||||
The program will pick %d random numbers.
|
||||
Your mission is to guess one of those numbers.
|
||||
|
||||
The greater your number is, harder it gets.
|
||||
|
||||
Wanna play?
|
||||
`
|
||||
)
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
args := os.Args[1:]
|
||||
|
||||
if len(args) < 1 {
|
||||
fmt.Printf(usage, maxTurns)
|
||||
return
|
||||
}
|
||||
|
||||
guess, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
fmt.Println("Not a number.")
|
||||
return
|
||||
}
|
||||
|
||||
var guess2 int
|
||||
if len(args) == 2 {
|
||||
guess2, err = strconv.Atoi(args[1])
|
||||
if err != nil {
|
||||
fmt.Println("Not a number.")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if guess < 0 || guess2 < 0 {
|
||||
fmt.Println("Please pick positive numbers.")
|
||||
return
|
||||
}
|
||||
|
||||
min := guess
|
||||
if guess < guess2 {
|
||||
min = guess2
|
||||
}
|
||||
|
||||
for turn := 1; turn <= maxTurns; turn++ {
|
||||
n := rand.Intn(min + 1)
|
||||
|
||||
if n == guess || n == guess2 {
|
||||
fmt.Println("🎉 YOU WIN!")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("☠️ YOU LOST... Try again?")
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
// 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: Verbose Mode
|
||||
//
|
||||
// When the player runs the game like this:
|
||||
//
|
||||
// go run main.go -v 4
|
||||
//
|
||||
// Display each generated random number:
|
||||
|
||||
// 1 3 4 🎉 YOU WIN!
|
||||
//
|
||||
// In this example, computer picks 1, 3, and 4. And the
|
||||
// player wins.
|
||||
//
|
||||
// HINT
|
||||
// You need to get and interpret the command-line arguments.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
// 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"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
maxTurns = 5 // less is more difficult
|
||||
usage = `Welcome to the Lucky Number Game!
|
||||
|
||||
The program will pick %d random numbers.
|
||||
Your mission is to guess one of those numbers.
|
||||
|
||||
The greater your number is, harder it gets.
|
||||
|
||||
Wanna play?
|
||||
|
||||
(Provide -v flag to see the picked numbers.)
|
||||
`
|
||||
)
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
args := os.Args[1:]
|
||||
|
||||
if len(args) < 1 {
|
||||
fmt.Printf(usage, maxTurns)
|
||||
return
|
||||
}
|
||||
|
||||
var verbose bool
|
||||
|
||||
if args[0] == "-v" {
|
||||
verbose = true
|
||||
}
|
||||
|
||||
guess, err := strconv.Atoi(args[len(args)-1])
|
||||
if err != nil {
|
||||
fmt.Println("Not a number.")
|
||||
return
|
||||
}
|
||||
|
||||
if guess < 0 {
|
||||
fmt.Println("Please pick a positive number.")
|
||||
return
|
||||
}
|
||||
|
||||
for turn := 1; turn <= maxTurns; turn++ {
|
||||
n := rand.Intn(guess + 1)
|
||||
|
||||
if verbose {
|
||||
fmt.Printf("%d ", n)
|
||||
}
|
||||
|
||||
if n == guess {
|
||||
if turn == 1 {
|
||||
fmt.Println("🥇 FIRST TIME WINNER!!!")
|
||||
} else {
|
||||
fmt.Println("🎉 YOU WON!")
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("☠️ YOU LOST... Try again?")
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
// 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: Enough Picks
|
||||
//
|
||||
// If the player's guess number is below 10;
|
||||
// then make sure that the computer generates a random
|
||||
// number between 0 and 10.
|
||||
//
|
||||
// However, if the guess number is above 10; then the
|
||||
// computer should generate the numbers
|
||||
// between 0 and the guess number.
|
||||
//
|
||||
// WHY?
|
||||
// This way the game will be harder.
|
||||
//
|
||||
// Because, in the current version of the game, if
|
||||
// the player's guess number is for example 3; then the
|
||||
// computer picks a random number between 0 and 3.
|
||||
//
|
||||
// So, currently a player can easily win the game.
|
||||
//
|
||||
// EXAMPLE
|
||||
// Suppose that the player runs the game like this:
|
||||
// go run main.go 9
|
||||
//
|
||||
// Or like this:
|
||||
// go run main.go 2
|
||||
//
|
||||
// Then the computer should pick a random number
|
||||
// between 0-10.
|
||||
//
|
||||
// Or, if the player runs it like this:
|
||||
// go run main.go 15
|
||||
//
|
||||
// Then the computer should pick a random number
|
||||
// between 0-15.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
@ -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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
maxTurns = 5 // less is more difficult
|
||||
usage = `Welcome to the Lucky Number Game!
|
||||
|
||||
The program will pick %d random numbers.
|
||||
Your mission is to guess one of those numbers.
|
||||
|
||||
The greater your number is, harder it gets.
|
||||
|
||||
Wanna play?
|
||||
`
|
||||
)
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
args := os.Args[1:]
|
||||
|
||||
if len(args) < 1 {
|
||||
fmt.Printf(usage, maxTurns)
|
||||
return
|
||||
}
|
||||
|
||||
guess, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
fmt.Println("Not a number.")
|
||||
return
|
||||
}
|
||||
|
||||
if guess < 0 {
|
||||
fmt.Println("Please pick a positive number.")
|
||||
return
|
||||
}
|
||||
|
||||
min := 10
|
||||
if guess > min {
|
||||
min = guess
|
||||
}
|
||||
|
||||
for turn := 1; turn <= maxTurns; turn++ {
|
||||
n := rand.Intn(min + 1)
|
||||
fmt.Println(n)
|
||||
|
||||
if n == guess {
|
||||
fmt.Println("🎉 YOU WIN!")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("☠️ YOU LOST... Try again?")
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
// 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: Dynamic Difficulty
|
||||
//
|
||||
// Current game picks only 5 numbers (5 turns).
|
||||
//
|
||||
// Make sure that the game adjust its own difficulty
|
||||
// depending on the guess number.
|
||||
//
|
||||
// RESTRICTION
|
||||
// Do not make the game to easy. Only adjust the
|
||||
// difficulty if the guess is above 10.
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// Suppose that the player runs the game like this:
|
||||
// go run main.go 5
|
||||
//
|
||||
// Then the computer should pick 5 random numbers.
|
||||
//
|
||||
// Or, if the player runs it like this:
|
||||
// go run main.go 25
|
||||
//
|
||||
// Then the computer may pick 11 random numbers
|
||||
// instead.
|
||||
//
|
||||
// Or, if the player runs it like this:
|
||||
// go run main.go 100
|
||||
//
|
||||
// Then the computer may pick 30 random numbers
|
||||
// instead.
|
||||
//
|
||||
// As you can see, greater guess number causes the
|
||||
// game to increase the game turns, which in turn
|
||||
// adjust the game's difficulty dynamically.
|
||||
//
|
||||
// Because, greater guess number makes it harder to win.
|
||||
// But, this way, game's difficulty will be dynamic.
|
||||
// It will adjust its own difficulty depending on the
|
||||
// guess number.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
// 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"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
maxTurns = 5 // less is more difficult
|
||||
usage = `Welcome to the Lucky Number Game!
|
||||
|
||||
The program will pick %d random numbers.
|
||||
Your mission is to guess one of those numbers.
|
||||
|
||||
The greater your number is, harder it gets.
|
||||
|
||||
Wanna play?
|
||||
`
|
||||
)
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
args := os.Args[1:]
|
||||
|
||||
if len(args) != 1 {
|
||||
fmt.Printf(usage, maxTurns)
|
||||
return
|
||||
}
|
||||
|
||||
guess, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
fmt.Println("Not a number.")
|
||||
return
|
||||
}
|
||||
|
||||
if guess < 0 {
|
||||
fmt.Println("Please pick a positive number.")
|
||||
return
|
||||
}
|
||||
|
||||
for turn := (maxTurns + guess/4); turn > 0; turn-- {
|
||||
n := rand.Intn(guess + 1)
|
||||
|
||||
if n == guess {
|
||||
fmt.Println("🎉 YOU WIN!")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("☠️ YOU LOST... Try again?")
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
// 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: Case Insentive Search
|
||||
//
|
||||
// Allow for case-insensitive searching
|
||||
//
|
||||
// EXAMPLE
|
||||
// Let's say that the user runs the program like this:
|
||||
// go run main.go LAZY
|
||||
//
|
||||
// Or like this: go run main.go lAzY
|
||||
// Or like this: go run main.go lazy
|
||||
//
|
||||
// For all cases above, the program should find
|
||||
// the "lazy" keyword.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
// 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 corpus = "lazy cat jumps again and again and again"
|
||||
|
||||
func main() {
|
||||
words := strings.Fields(corpus)
|
||||
query := os.Args[1:]
|
||||
|
||||
queries:
|
||||
for _, q := range query {
|
||||
// case insensitive search
|
||||
q = strings.ToLower(q)
|
||||
|
||||
search:
|
||||
for i, w := range words {
|
||||
switch q {
|
||||
case "and", "or", "the":
|
||||
break search
|
||||
}
|
||||
|
||||
if q == w {
|
||||
fmt.Printf("#%-2d: %q\n", i+1, w)
|
||||
continue queries
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
// 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: Path Searcher
|
||||
//
|
||||
// Your program should search inside the path environment
|
||||
// variable.
|
||||
//
|
||||
// Remove the corpus constant then get the corpus from the
|
||||
// environment variable "Path" or "PATH" which
|
||||
// constains paths to the executable programs on your
|
||||
// operating system.
|
||||
//
|
||||
// HINTS
|
||||
// 1. Search the web for what is an environment
|
||||
// variable and how to use it, if you don't know
|
||||
// what it is.
|
||||
//
|
||||
// 2. Look up for the necessary function for getting
|
||||
// an environment variable. It's in the "os" package.
|
||||
//
|
||||
// Search for it on the Go online documentation.
|
||||
//
|
||||
// 3. Look up for the necessary function for splitting
|
||||
// the path variable into directories. It's in
|
||||
// the "strings" package.
|
||||
//
|
||||
// EXAMPLE
|
||||
// For example, on my Mac, my PATH environment variable
|
||||
// looks like this:
|
||||
//
|
||||
// "/usr/local/bin:/sbin:/Users/inanc/go/bin"
|
||||
//
|
||||
// So, if the user runs the program like this:
|
||||
//
|
||||
// go run main.go /sbin
|
||||
//
|
||||
// It should print this:
|
||||
//
|
||||
// #2 : "/sbin"
|
||||
// ---------------------------------------------------------
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// BONUS EXERCISE
|
||||
// Make your program cross platform. So, it can search
|
||||
// the path environment variable when you run it on
|
||||
// a Windows or on a Mac (OS X) or on a Linux.
|
||||
//
|
||||
// HINT
|
||||
// 1. What you're looking for is the runtime.GOOS constant.
|
||||
// 2. Get the operating system name using GOOS.
|
||||
// 3. Adjust the path environment variable name and
|
||||
// the directory separator accordingly.
|
||||
//
|
||||
// FOR EXAMPLE: On OS X, path environment variable's name
|
||||
// is PATH and the separator is a colon `:`.
|
||||
//
|
||||
// Or, on Windows, its name is Path and the separator is
|
||||
// a semicolon `;`.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
@ -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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// get and split the PATH environment variable
|
||||
// this only works for the unix-based systems
|
||||
words := strings.Split(os.Getenv("PATH"), ":")
|
||||
query := os.Args[1:]
|
||||
queries:
|
||||
for _, q := range query {
|
||||
search:
|
||||
for i, w := range words {
|
||||
switch q {
|
||||
case "and", "or", "the":
|
||||
break search
|
||||
}
|
||||
|
||||
if q == w {
|
||||
fmt.Printf("#%-2d: %q\n", i+1, w)
|
||||
continue queries
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
2
13-loops/exercises/more-exercises.md
Normal file
2
13-loops/exercises/more-exercises.md
Normal file
@ -0,0 +1,2 @@
|
||||
**You can find more exercises here:**
|
||||
* https://www.rosettacode.org/wiki/Category:Iteration
|
Reference in New Issue
Block a user