Initial commit
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// when an integer and a float value used together
|
||||
// in an expression, the result always becomes
|
||||
// a float value
|
||||
fmt.Println(8 * -4.0) // -32.0 not -32
|
||||
|
||||
// two integer values result in an integer value
|
||||
fmt.Println(-4 / 2)
|
||||
|
||||
// remainder operator
|
||||
// it can only used with integers
|
||||
fmt.Println(5 % 2)
|
||||
// fmt.Println(5.0 % 2) // wrong
|
||||
|
||||
// addition operators
|
||||
fmt.Println(1 + 2.5)
|
||||
fmt.Println(2 - 3)
|
||||
|
||||
// negation operator
|
||||
fmt.Println(-(-2))
|
||||
fmt.Println(- -2) // this also works
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var (
|
||||
myAge = 30
|
||||
yourAge = 35
|
||||
average float64
|
||||
)
|
||||
|
||||
average = float64(myAge+yourAge) / 2
|
||||
|
||||
fmt.Println(average)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
ratio := 1.0 / 10.0
|
||||
|
||||
// after 10 operations
|
||||
// the inaccuracy is clear
|
||||
//
|
||||
// BTW, don't mind about this loop syntax for now
|
||||
// I'm going to explain it afterwards
|
||||
for range [...]int{10: 0} {
|
||||
ratio += 1.0 / 10.0
|
||||
}
|
||||
|
||||
fmt.Printf("%.60f", ratio)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// Go compiler sees these numbers as integers,
|
||||
// since, there are no fractional parts in
|
||||
// integer values,
|
||||
// So, the result becomes 1 instead of 1.5
|
||||
|
||||
// So, ratio variable here is an int variable,
|
||||
// it's because, 3 divided by 2 results
|
||||
// in an integer.
|
||||
|
||||
ratio := 3 / 2
|
||||
|
||||
fmt.Printf("%d", ratio)
|
||||
}
|
||||
@@ -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() {
|
||||
// When you use a float value with an integer
|
||||
// in a calculation,
|
||||
// the result always becomes a float.
|
||||
|
||||
ratio := 3.0 / 2
|
||||
|
||||
// OR:
|
||||
// ratio = 3 / 2.0
|
||||
|
||||
// OR - if 3 is inside an int variable:
|
||||
// n := 3
|
||||
// ratio = float64(n) / 2
|
||||
|
||||
fmt.Printf("%f", ratio)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("sum :", 3+2) // sum - int
|
||||
fmt.Println("sum :", 2+3.5) // sum - float64
|
||||
fmt.Println("dif :", 3-1) // difference - int
|
||||
fmt.Println("dif :", 3-0.5) // difference - float64
|
||||
fmt.Println("prod:", 4*5) // product - int
|
||||
fmt.Println("prod:", 5*2.5) // product - float64
|
||||
fmt.Println("quot:", 8/2) // quotient - int
|
||||
fmt.Println("quot:", 8/1.5) // quotient - float64
|
||||
|
||||
// remainder is only for integers
|
||||
fmt.Println("rem :", 8%3)
|
||||
// fmt.Println("rem:", 8.0%3) // error
|
||||
|
||||
// you can do this
|
||||
// since the fractional part of a float is zero
|
||||
f := 8.0
|
||||
fmt.Println("rem :", int(f)%3)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// what's the value of the ratio?
|
||||
// 3 / 2 = 1.5?
|
||||
var ratio float64 = 3 / 2
|
||||
fmt.Println(ratio)
|
||||
|
||||
// explain
|
||||
// above expression equals to this:
|
||||
ratio = float64(int(3) / int(2))
|
||||
fmt.Println(ratio)
|
||||
|
||||
// how to fix it?
|
||||
//
|
||||
// remember, when one of the values is a float value
|
||||
// the result becomes a float
|
||||
ratio = float64(3) / 2
|
||||
fmt.Println(ratio)
|
||||
|
||||
// or
|
||||
ratio = 3.0 / 2
|
||||
fmt.Println(ratio)
|
||||
}
|
||||
27
08-numbers-and-strings/01-numbers/03-precedence/01/main.go
Normal file
27
08-numbers-and-strings/01-numbers/03-precedence/01/main.go
Normal 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() {
|
||||
fmt.Println(
|
||||
2+2*4/2,
|
||||
2+((2*4)/2), // same as above
|
||||
)
|
||||
|
||||
fmt.Println(
|
||||
1+4-2,
|
||||
(1+4)-2, // same as above
|
||||
)
|
||||
|
||||
fmt.Println(
|
||||
(2+2)*4/2,
|
||||
(2+2)*(4/2), // same as above
|
||||
)
|
||||
}
|
||||
20
08-numbers-and-strings/01-numbers/03-precedence/02/main.go
Normal file
20
08-numbers-and-strings/01-numbers/03-precedence/02/main.go
Normal 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() {
|
||||
n, m := 1, 5
|
||||
|
||||
fmt.Println(2 + 1*m/n)
|
||||
fmt.Println(2 + ((1 * m) / n)) // same as above
|
||||
|
||||
// let's change the precedence using parentheses
|
||||
fmt.Println(((2 + 1) * m) / n)
|
||||
}
|
||||
30
08-numbers-and-strings/01-numbers/03-precedence/03/main.go
Normal file
30
08-numbers-and-strings/01-numbers/03-precedence/03/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
|
||||
|
||||
func main() {
|
||||
// // Precedence: Order of expressions
|
||||
// // Multiplication operators runs first: * and /
|
||||
|
||||
// fmt.Println(
|
||||
// 1 + 5 - 3*10/2,
|
||||
// )
|
||||
|
||||
// // 3 * 10 = 30
|
||||
// // 30 / 2 = 15
|
||||
// // 1 + 5 = 6
|
||||
// // 6 - 15 = -9
|
||||
|
||||
// // **** TIP ****
|
||||
// // Use parentheses to change the order of evaluation.
|
||||
// // First, (1+5-3), then (10/2) will be calculated.
|
||||
|
||||
// fmt.Println(
|
||||
// (1 + 5 - 3) * (10 / 2),
|
||||
// )
|
||||
}
|
||||
20
08-numbers-and-strings/01-numbers/03-precedence/04/main.go
Normal file
20
08-numbers-and-strings/01-numbers/03-precedence/04/main.go
Normal 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() {
|
||||
celsius := 35.
|
||||
|
||||
// Wrong formula : 9*celsius + 160 / 5
|
||||
// Correct formula: (9*celsius + 160) / 5
|
||||
fahrenheit := (9*celsius + 160) / 5
|
||||
|
||||
fmt.Printf("%g ºC is %g ºF\n", celsius, fahrenheit)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// 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 n int
|
||||
|
||||
// ALTERNATIVES:
|
||||
// n = n + 1
|
||||
// n += 1
|
||||
|
||||
// BETTER:
|
||||
n++
|
||||
|
||||
fmt.Println(n)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// 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() {
|
||||
n := 10
|
||||
|
||||
// ALTERNATIVES:
|
||||
// n = n - 1
|
||||
// n -= 1
|
||||
|
||||
// BETTER:
|
||||
n--
|
||||
|
||||
fmt.Println(n)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
import "fmt"
|
||||
|
||||
// incdec is a statement
|
||||
|
||||
func main() {
|
||||
var counter int
|
||||
|
||||
// following "statements" are correct:
|
||||
|
||||
counter++ // 1
|
||||
counter++ // 2
|
||||
counter++ // 3
|
||||
counter-- // 2
|
||||
fmt.Printf("There are %d line(s) in the file\n",
|
||||
counter)
|
||||
|
||||
// the following "expressions" are incorrect:
|
||||
|
||||
// counter = 5+counter--
|
||||
// counter = ++counter + counter--
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// 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() {
|
||||
width, height := 5., 12.
|
||||
|
||||
// calculates the area of a rectangle
|
||||
area := width * height
|
||||
fmt.Printf("%gx%g=%g\n", width, height, area)
|
||||
|
||||
area = area - 10 // decreases area by 10
|
||||
area = area + 10 // increases area by 10
|
||||
area = area * 2 // doubles the area
|
||||
area = area / 2 // divides the area by 2
|
||||
fmt.Printf("area=%g\n", area)
|
||||
|
||||
// // ASSIGNMENT OPERATIONS
|
||||
area -= 10 // decreases area by 10
|
||||
area += 10 // increases area by 10
|
||||
area *= 2 // doubles the area
|
||||
area /= 2 // divides the area by 2
|
||||
fmt.Printf("area=%g\n", area)
|
||||
|
||||
// finds the remainder of area variable
|
||||
// since: area is float, this won't work:
|
||||
// area %= 7
|
||||
|
||||
// this works
|
||||
area = float64(int(area) % 7)
|
||||
fmt.Printf("area=%g\n", area)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
arg := os.Args[1]
|
||||
|
||||
// feet is a float64 now
|
||||
feet, _ := strconv.ParseFloat(arg, 64)
|
||||
|
||||
meters := feet * 0.3048
|
||||
|
||||
fmt.Printf("%f feet is %f meters.\n", feet, meters)
|
||||
|
||||
// pretty print it:
|
||||
|
||||
// fmt.Printf("%g feet is %g meters.\n", feet, meters)
|
||||
}
|
||||
34
08-numbers-and-strings/01-numbers/exercises/01/main.go
Normal file
34
08-numbers-and-strings/01-numbers/exercises/01/main.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1. Print the sum of 50 and 25
|
||||
// 2. Print the difference of 50 and 15.5
|
||||
// 3. Print the product of 50 and 0.5
|
||||
// 4. Print the quotient of 50 and 0.5
|
||||
// 5. Print the remainder of 25 and 3
|
||||
// 6. Print the negation of `5 + 2`
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 75
|
||||
// 34.5
|
||||
// 25
|
||||
// 100
|
||||
// 1
|
||||
// -7
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// ADD YOUR CODE BELOW
|
||||
// USE `fmt.Println` for each question
|
||||
|
||||
// DO NOT TOUCH THIS
|
||||
// fmt.Println(x)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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() {
|
||||
fmt.Println(50 + 25)
|
||||
fmt.Println(50 - 15.5)
|
||||
fmt.Println(50 * 0.5)
|
||||
fmt.Println(50 / 0.5)
|
||||
fmt.Println(25 % 3)
|
||||
fmt.Println(-(5 + 2))
|
||||
}
|
||||
23
08-numbers-and-strings/01-numbers/exercises/02/main.go
Normal file
23
08-numbers-and-strings/01-numbers/exercises/02/main.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// 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
|
||||
// Fix the program to print 2.5 instead of 2
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 2.5
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
x := 5 / 2
|
||||
fmt.Println(x)
|
||||
}
|
||||
@@ -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() {
|
||||
// Below solutions are correct:
|
||||
x := 5. / 2
|
||||
// x := 5 / 2.
|
||||
// x := float64(5) / 2
|
||||
// x := 5 / float64(2)
|
||||
|
||||
fmt.Println(x)
|
||||
}
|
||||
38
08-numbers-and-strings/01-numbers/exercises/03/main.go
Normal file
38
08-numbers-and-strings/01-numbers/exercises/03/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
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// Change the expressions to produce the expected outputs
|
||||
//
|
||||
// RESTRICTION
|
||||
// Use parentheses to change the precedence
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// This expression should print 20
|
||||
fmt.Println(10 + 5 - 5 - 10)
|
||||
|
||||
// This expression should print -16
|
||||
fmt.Println(-10 + 0.5 - 1 + 5.5)
|
||||
|
||||
// This expression should print -25
|
||||
fmt.Println(5 + 10*2 - 5)
|
||||
|
||||
// This expression should print 0.5
|
||||
fmt.Println(0.5*2 - 1)
|
||||
|
||||
// This expression should print 24
|
||||
fmt.Println(3 + 1/2*10 + 4)
|
||||
|
||||
// This expression should print 15
|
||||
fmt.Println(10 / 2 * 10 % 7)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// 10 + 5 - 5 - 10
|
||||
fmt.Println(10 + 5 - (5 - 10))
|
||||
|
||||
// -10 + 0.5 - 1 + 5.5
|
||||
fmt.Println(-10 + 0.5 - (1 + 5.5))
|
||||
|
||||
// 5 + 10*2 - 5
|
||||
fmt.Println(5 + 10*(2-5))
|
||||
|
||||
// 0.5*2 - 1
|
||||
fmt.Println(0.5 * (2 - 1))
|
||||
|
||||
// 3 + 1/2*10 + 4
|
||||
fmt.Println((3+1)/2*10 + 4)
|
||||
|
||||
// 10 / 2 * 10 % 7
|
||||
fmt.Println(10 / 2 * (10 % 7))
|
||||
}
|
||||
32
08-numbers-and-strings/01-numbers/exercises/04/main.go
Normal file
32
08-numbers-and-strings/01-numbers/exercises/04/main.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1. Increase the `counter` 5 times
|
||||
// 2. Decrease the `factor` 2 times
|
||||
// 3. Print the product of counter and factor
|
||||
//
|
||||
// RESTRICTION
|
||||
// Use only the incdec statements
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// -75
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// DO NOT TOUCH THIS
|
||||
counter, factor := 45, 0.5
|
||||
|
||||
// TYPE YOUR CODE BELOW
|
||||
// ...
|
||||
|
||||
// LASTLY: REMOVE THE CODE BELOW
|
||||
_, _ = counter, factor
|
||||
}
|
||||
@@ -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
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
counter, factor := 45, 0.5
|
||||
|
||||
counter++
|
||||
counter++
|
||||
counter++
|
||||
counter++
|
||||
counter++
|
||||
|
||||
factor--
|
||||
factor--
|
||||
|
||||
fmt.Println(float64(counter) * factor)
|
||||
}
|
||||
41
08-numbers-and-strings/01-numbers/exercises/05/main.go
Normal file
41
08-numbers-and-strings/01-numbers/exercises/05/main.go
Normal file
@@ -0,0 +1,41 @@
|
||||
// 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
|
||||
// 1. Write the simplest line of code to increase
|
||||
// the counter variable by 1.
|
||||
//
|
||||
// 2. Write the simplest line of code to decrease
|
||||
// the counter variable by 1.
|
||||
//
|
||||
// 3. Write the simplest line of code to increase
|
||||
// the counter variable by 5.
|
||||
//
|
||||
// 4. Write the simplest line of code to multiply
|
||||
// the counter variable by 10.
|
||||
//
|
||||
// 5. Write the simplest line of code to divide
|
||||
// the counter variable by 5.
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 10
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// DO NOT CHANGE THE CODE BELOW
|
||||
var counter int
|
||||
|
||||
// TYPE YOUR CODE HERE
|
||||
|
||||
// DO NOT CHANGE THE CODE BELOW
|
||||
fmt.Println(counter)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// 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 counter int
|
||||
|
||||
counter++
|
||||
counter--
|
||||
|
||||
counter += 5
|
||||
counter *= 10
|
||||
counter /= 5
|
||||
|
||||
fmt.Println(counter)
|
||||
}
|
||||
35
08-numbers-and-strings/01-numbers/exercises/06/main.go
Normal file
35
08-numbers-and-strings/01-numbers/exercises/06/main.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// Simplify the code (refactor)
|
||||
//
|
||||
// RESTRICTION
|
||||
// Use only the incdec and assignment operations
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 3
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
width, height := 10, 2
|
||||
|
||||
width = width + 1
|
||||
width = width + height
|
||||
width = width - 1
|
||||
width = width - height
|
||||
width = width * 20
|
||||
width = width / 25
|
||||
width = width % 5
|
||||
|
||||
fmt.Println(width)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// 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() {
|
||||
width, height := 10, 2
|
||||
|
||||
width++
|
||||
width += height
|
||||
width--
|
||||
width -= height
|
||||
width *= 20
|
||||
width /= 25
|
||||
width %= 5
|
||||
|
||||
fmt.Println(width)
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// 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
|
||||
// Calculate the area of a circle from the given radius
|
||||
//
|
||||
// CIRCLE AREA FORMULA
|
||||
// area = πr²
|
||||
// https://en.wikipedia.org/wiki/Area#Circles
|
||||
//
|
||||
// HINT
|
||||
// For PI you can use `math.Pi`
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 314.1592653589793
|
||||
//
|
||||
// BONUS EXERCISE!
|
||||
// 1. Print the area as 314.16
|
||||
// 2. To do that you need to use the correct Printf verb :)
|
||||
// Instead of `%g` verb below.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
var (
|
||||
radius = 10.
|
||||
area float64
|
||||
)
|
||||
|
||||
// ADD YOUR CODE HERE
|
||||
// ...
|
||||
|
||||
// DO NOT TOUCH THIS
|
||||
fmt.Printf("radius: %g -> area: %g\n", radius, area)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
radius = 10.
|
||||
area float64
|
||||
)
|
||||
|
||||
area = math.Pi * radius * radius
|
||||
|
||||
fmt.Printf("radius: %g -> area: %.2f\n",
|
||||
radius, area)
|
||||
|
||||
// ALTERNATIVE:
|
||||
// math.Pow calculates the power of a float number
|
||||
// area = math.Pi * math.Pow(radius, 2)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// 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
|
||||
// 1. Get the radius from the command-line
|
||||
// 2. Convert it to a float64
|
||||
// 3. Calculate the surface area of a sphere
|
||||
//
|
||||
// SPHERE SURFACE AREA FORMULA
|
||||
// area = 4πr²
|
||||
// https://en.wikipedia.org/wiki/Sphere#Surface_area
|
||||
//
|
||||
// RESTRICTION
|
||||
// Use `math.Pow` to calculate the area
|
||||
// Read its documentation to see how it works.
|
||||
// https://golang.org/pkg/math/#Pow
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 1256.64
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
var radius, area float64
|
||||
|
||||
// ADD YOUR CODE HERE
|
||||
// ...
|
||||
|
||||
// DO NOT TOUCH THIS
|
||||
fmt.Printf("radius: %g -> area: %.2f\n", radius, area)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var radius, area float64
|
||||
|
||||
radius, _ = strconv.ParseFloat(os.Args[1], 64)
|
||||
|
||||
area = 4 * math.Pi * math.Pow(radius, 2)
|
||||
|
||||
fmt.Printf("radius: %g -> area: %.2f\n",
|
||||
radius, area)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1. Get the radius from the command-line
|
||||
// 2. Convert it to a float64
|
||||
// 3. Calculate the volume of a sphere
|
||||
//
|
||||
// SPHERE VOLUME FORMULA
|
||||
// https://en.wikipedia.org/wiki/Sphere#Enclosed_volume
|
||||
//
|
||||
// RESTRICTION
|
||||
// Use `math.Pow` to calculate the volume
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 4188.79
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
var radius, vol float64
|
||||
|
||||
// ADD YOUR CODE HERE
|
||||
// ...
|
||||
|
||||
// DO NOT TOUCH THIS
|
||||
fmt.Printf("radius: %g -> volume: %.2f\n", radius, vol)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var radius, vol float64
|
||||
|
||||
radius, _ = strconv.ParseFloat(os.Args[1], 64)
|
||||
|
||||
vol = (4 * math.Pi * math.Pow(radius, 3)) / 3
|
||||
|
||||
fmt.Printf("radius: %g -> volume: %.2f\n", radius, vol)
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
## Which group of operators below is arithmetic operators?
|
||||
1. **, /, ^, !, ++, --
|
||||
2. *, /, %, +, - *CORRECT*
|
||||
3. &, |, +, -, /
|
||||
|
||||
|
||||
## Which value below you can use with a remainder operator?
|
||||
1. 3.54
|
||||
2. true
|
||||
3. 57 *CORRECT*
|
||||
4. "Try Me!"
|
||||
|
||||
> 4. Nice Try. But, that's not right. Sorry.
|
||||
> 3. That's right. The remainder operator only works on integer values.
|
||||
|
||||
|
||||
## What's the result of this expression?
|
||||
```go
|
||||
8 % 3
|
||||
```
|
||||
1. 4
|
||||
2. 2
|
||||
3. 0
|
||||
4. 1 *CORRECT*
|
||||
|
||||
|
||||
## What's the result of this expression?
|
||||
```go
|
||||
-(3 * -2)
|
||||
```
|
||||
1. -6
|
||||
2. -1
|
||||
3. 0
|
||||
4. 6 *CORRECT*
|
||||
|
||||
|
||||
## What's the result of this expression?
|
||||
```go
|
||||
var degree float64 = 10 / 4
|
||||
```
|
||||
1. 2.5
|
||||
2. 2.49
|
||||
3. 2 *CORRECT*
|
||||
4. 0
|
||||
|
||||
> 3. That's right. An integer value cannot contain fractional parts.
|
||||
|
||||
|
||||
## What's the result of this expression?
|
||||
```go
|
||||
var degree float64 = 3. / 2
|
||||
```
|
||||
1. 1.5 *CORRECT*
|
||||
2. 1.49
|
||||
3. 1
|
||||
4. 0
|
||||
|
||||
> 1. That's right. `3.` makes the whole expression a float value.
|
||||
|
||||
|
||||
## What's the type of the `x` variable?
|
||||
```go
|
||||
x := 5 * 2.
|
||||
```
|
||||
1. int
|
||||
2. float64 *CORRECT*
|
||||
3. bool
|
||||
4. string
|
||||
|
||||
> 1. Look closely to 2 there.
|
||||
> 2. Why? Because, `2.` there makes the expressions a float value. Cool.
|
||||
> 3. Oh, come on! Life is not always true and false.
|
||||
> 4. I can't see any double-quotes or back-quotes, can you?
|
||||
|
||||
|
||||
## What's the type of the `x` variable?
|
||||
```go
|
||||
x := 5 * -(2)
|
||||
```
|
||||
1. int *CORRECT*
|
||||
2. float64
|
||||
3. bool
|
||||
4. string
|
||||
|
||||
> 1. Why? Because, there only integer numbers.
|
||||
> 2. I can't see any fractional parts there, can you?
|
||||
> 3. Oh, come on! Life is not always true and false.
|
||||
> 4. I can't see any double-quotes or back-quotes, can you?
|
||||
|
||||
|
||||
## Which kind of values can result in inaccurate calculations?
|
||||
1. integers
|
||||
2. floats *CORRECT*
|
||||
3. bools
|
||||
4. strings
|
||||
44
08-numbers-and-strings/01-numbers/questions/02-precedence.md
Normal file
44
08-numbers-and-strings/01-numbers/questions/02-precedence.md
Normal file
@@ -0,0 +1,44 @@
|
||||
## What's the result of the expression?
|
||||
```go
|
||||
5 - 2 * 5 + 7
|
||||
```
|
||||
1. 2 *CORRECT*
|
||||
2. 22
|
||||
3. -19
|
||||
4. 36
|
||||
5. -12
|
||||
|
||||
|
||||
## What's the result of the expression?
|
||||
```go
|
||||
5 - (2 * 5) + 7
|
||||
```
|
||||
1. 2
|
||||
2. 22 *CORRECT*
|
||||
3. -19
|
||||
4. 36
|
||||
5. -12
|
||||
|
||||
|
||||
## What's the result of the expression?
|
||||
```go
|
||||
5 - 2 * (5 + 7)
|
||||
```
|
||||
1. 2
|
||||
2. 22
|
||||
3. -19 *CORRECT*
|
||||
4. 36
|
||||
5. -12
|
||||
|
||||
|
||||
## What's the result of the expression?
|
||||
```go
|
||||
5. -(2 * 5 + 7)
|
||||
```
|
||||
1. 2
|
||||
2. 22
|
||||
3. -19
|
||||
4. -12
|
||||
5. -12.0 *CORRECT*
|
||||
|
||||
> 4. You're close but remember! The result of an expression with floats and integers is always a float.
|
||||
@@ -0,0 +1,98 @@
|
||||
## Which expression increases `n` by 1?
|
||||
```go
|
||||
var n float64
|
||||
```
|
||||
1. `n = +1`
|
||||
2. `n = n++`
|
||||
3. `n = n + 1` *CORRECT*
|
||||
4. `++n`
|
||||
|
||||
> 1. This just assigns 1 to n.
|
||||
> 2. IncDec statement can't be used as an operator.
|
||||
> 4. Go doesn't support prefix incdec notation.
|
||||
|
||||
|
||||
## Which expression decreases `n` by 1?
|
||||
```go
|
||||
var n int
|
||||
```
|
||||
1. `n = -1`
|
||||
2. `n = n--`
|
||||
3. `n = n - 1` *CORRECT*
|
||||
4. `--n`
|
||||
|
||||
> 1. This just assigns -1 to n.
|
||||
> 2. IncDec statement can't be used as an operator.
|
||||
> 4. Go doesn't support prefix incdec notation.
|
||||
|
||||
|
||||
## Which code below equals to `n = n + 1`?
|
||||
1. `n++` *CORRECT*
|
||||
2. `n = n++`
|
||||
3. `++n`
|
||||
4. `n = n ++ 1`
|
||||
|
||||
> 2. IncDec statement can't be used as an operator.
|
||||
> 3. Go doesn't support prefix incdec notation.
|
||||
> 4. What's that? ++?
|
||||
|
||||
|
||||
## Which code below equals to `n = n + 1`?
|
||||
1. `n = n++`
|
||||
2. `n += 1` *CORRECT*
|
||||
3. `++n`
|
||||
4. `n = n ++ 1`
|
||||
|
||||
> 1. IncDec statement can't be used as an operator.
|
||||
> 3. Go doesn't support prefix incdec notation.
|
||||
> 4. What's that? ++?
|
||||
|
||||
|
||||
## Which code below equals to `n -= 1`?
|
||||
1. `n = n--`
|
||||
2. `n += 1--`
|
||||
3. `n--` *CORRECT*
|
||||
4. `--n`
|
||||
|
||||
> 1. IncDec statement can't be used as an operator.
|
||||
> 2. IncDec statement can't be used as an operator. And also, you can't use it with `1--`. The value should be addressable. You're going to learn what that means soon.
|
||||
> 4. Go doesn't support prefix incdec notation.
|
||||
|
||||
|
||||
## Which code below divides the `length` by 10?
|
||||
1. `length = length // 10`
|
||||
2. `length /= 10` *CORRECT*
|
||||
3. `length //= 10`
|
||||
|
||||
> 1. What's that? `//`?
|
||||
> 2. That's right. This equals to: `length = length / 10`
|
||||
> 3. What's that? `//=`?
|
||||
|
||||
|
||||
## Which code below equals to `x = x % 2`?
|
||||
1. `x = x / 2`
|
||||
2. `x =% x`
|
||||
3. `x %= x` *CORRECT*
|
||||
|
||||
> 1. This is a division. You need to use the remainder operator.
|
||||
> 2. Close... But, the `%` operator is on the wrong side of the assignment.
|
||||
|
||||
|
||||
## Which function below converts a string value into a float value?
|
||||
1. `fmtconv.ToFloat`
|
||||
2. `conv.ParseFloat`
|
||||
3. `strconv.ParseFloat` *CORRECT*
|
||||
4. `strconv.ToFloat`
|
||||
|
||||
|
||||
## Which code is correct?
|
||||
If you don't remember it, this its function signature:
|
||||
```go
|
||||
func ParseFloat(s string, bitSize int) (float64, error)
|
||||
```
|
||||
1. `strconv.ParseFloat("10", 128)`
|
||||
2. `strconv.ParseFloat("10", 64)` *CORRECT*
|
||||
3. `strconv.ParseFloat("10", "64")`
|
||||
4. `strconv.ParseFloat(10, 64)`
|
||||
|
||||
> 1. There are no 128-bit floating point values in Go (Actually there are, but they only belong to the compile-time).
|
||||
Reference in New Issue
Block a user