Initial commit
This commit is contained in:
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)
|
||||
}
|
Reference in New Issue
Block a user