Initial commit

This commit is contained in:
Inanc Gumus
2018-10-13 23:30:21 +03:00
commit cde4e6632c
567 changed files with 17896 additions and 0 deletions

View 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() {
}

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() {
var sum int
for i := 1; i <= 10; i++ {
sum += i
}
fmt.Println("Sum:", sum)
}

View File

@@ -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() {
}

View File

@@ -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)
}

View File

@@ -0,0 +1,38 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
// ---------------------------------------------------------
// 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() {
}

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"
"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)
}

View 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() {
}

View 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)
}

View 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() {
}

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"
"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)
}

View 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() {
}

View File

@@ -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)
}
}

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
// ---------------------------------------------------------
// 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() {
}

View File

@@ -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()
}