add: for statement loop exercises

This commit is contained in:
Inanc Gumus
2018-10-22 12:25:30 +03:00
parent 0d861eb299
commit 0205846da0
50 changed files with 291 additions and 8 deletions

View File

@ -27,6 +27,9 @@ package main
// EXPECTED OUTPUT
// go run main.go 2 4 6 7 a 9 c 11 x 12 13
// 2 7 11 13
//
// go run main.go 1 2 3 5 7 A B C
// 2 3 5 7
// ---------------------------------------------------------
func main() {

View File

@ -13,7 +13,6 @@ import (
"strconv"
)
//
func main() {
// remember [1:] skips the first argument
@ -26,14 +25,14 @@ main:
}
switch {
// not a prime
case n <= 0 || n%2 == 0 || n%3 == 0:
continue
// prime
case n == 2 || n == 3:
fmt.Print(n, " ")
continue
// not a prime
case n <= 1 || n%2 == 0 || n%3 == 0:
continue
}
for i, w := 5, 2; i*i <= n; {

View File

@ -0,0 +1,56 @@
// 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 Table
//
// Get the size of the table from the command-line
// Passing 5 should create a 5x5 table
// Passing 10 for a 10x10 tableCreate an infinite loop
//
// RESTRICTION
// Solve this exercise without looking at the original
// multiplication table exercise.
//
// HINT
// There was a max constant in the original program.
// That determines the size of the table.
//
// EXPECTED OUTPUT
//
// go run main.go
// Give me the size of the table
//
// go run main.go -5
// Wrong size
//
// go run main.go ABC
// Wrong size
//
// go run main.go 1
// X 0 1
// 0 0 0
// 1 0 1
//
// go run main.go 2
// X 0 1 2
// 0 0 0 0
// 1 0 1 2
// 2 0 2 4
//
// go run main.go 3
// X 0 1 2 3
// 0 0 0 0 0
// 1 0 1 2 3
// 2 0 2 4 6
// 3 0 3 6 9
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,47 @@
// 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() {
args := os.Args
if len(args) != 2 {
fmt.Println("Give me the size of the table")
return
}
size, err := strconv.Atoi(args[1])
if err != nil || size <= 0 {
fmt.Println("Wrong size")
return
}
// print the header
fmt.Printf("%5s", "X")
for i := 0; i <= size; i++ {
fmt.Printf("%5d", i)
}
fmt.Println()
for i := 0; i <= size; i++ {
// print the vertical header
fmt.Printf("%5d", i)
// print the cells
for j := 0; j <= size; j++ {
fmt.Printf("%5d", i*j)
}
fmt.Println()
}
}

View File

@ -0,0 +1,92 @@
// 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: Math Tables
//
// Create division, addition and subtraction tables
//
// 1. Get the math operation and
// the size of the table from the user
//
// 2. Print the table accordingly
//
// 3. Correctly handle the division by zero error
//
//
// BONUS #1
//
// Use strings.IndexAny function to detect
// the valid operations.
//
// Search on Google for: golang pkg strings IndexAny
//
// BONUS #2
//
// Add remainder operation as well (remainder table using %).
//
//
// EXPECTED OUTPUT
//
// go run main.go
// Usage: [op=*/+-] [size]
//
// go run main.go "*"
// Size is missing
// Usage: [op=*/+-] [size]
//
// go run main.go "%" 4
// Invalid operator.
// Valid ops one of: */+-
//
// go run main.go "*" 4
// * 0 1 2 3 4
// 0 0 0 0 0 0
// 1 0 1 2 3 4
// 2 0 2 4 6 8
// 3 0 3 6 9 12
// 4 0 4 8 12 16
//
// go run main.go "/" 4
// / 0 1 2 3 4
// 0 0 0 0 0 0
// 1 0 1 0 0 0
// 2 0 2 1 0 0
// 3 0 3 1 1 0
// 4 0 4 2 1 1
//
// go run main.go "+" 4
// + 0 1 2 3 4
// 0 0 1 2 3 4
// 1 1 2 3 4 5
// 2 2 3 4 5 6
// 3 3 4 5 6 7
// 4 4 5 6 7 8
//
// go run main.go "-" 4
// - 0 1 2 3 4
// 0 0 -1 -2 -3 -4
// 1 1 0 -1 -2 -3
// 2 2 1 0 -1 -2
// 3 3 2 1 0 -1
// 4 4 3 2 1 0
//
// BONUS:
//
// go run main.go "%" 4
// % 0 1 2 3 4
// 0 0 0 0 0 0
// 1 0 0 1 1 1
// 2 0 0 0 2 2
// 3 0 0 1 0 3
// 4 0 0 0 1 0
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,85 @@
// 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"
"strings"
)
const (
validOps = "%*/+-"
usageMsg = "Usage: [op=" + validOps + "] [size]"
sizeMissingMsg = "Size is missing"
invalidOpMsg = `Invalid operator.
Valid ops one of: ` + validOps
invalidOp = -1
)
func main() {
args := os.Args[1:]
switch l := len(args); {
case l == 1:
fmt.Println(sizeMissingMsg)
fallthrough
case l < 1:
fmt.Println(usageMsg)
return
}
op := args[0]
if strings.IndexAny(op, validOps) == invalidOp {
fmt.Println(invalidOpMsg)
return
}
size, err := strconv.Atoi(args[1])
if err != nil || size <= 0 {
fmt.Println("Wrong size")
return
}
fmt.Printf("%5s", op)
for i := 0; i <= size; i++ {
fmt.Printf("%5d", i)
}
fmt.Println()
for i := 0; i <= size; i++ {
fmt.Printf("%5d", i)
for j := 0; j <= size; j++ {
var res int
switch op {
case "*":
res = i * j
case "%":
if j != 0 {
res = i % j
}
case "/":
if j != 0 {
res = i / j
}
case "+":
res = i + j
case "-":
res = i - j
}
fmt.Printf("%5d", res)
}
fmt.Println()
}
}

View File

@ -0,0 +1,3 @@
There are 17 exercises in this section.
You can find them inside the subdirectories.

View File

@ -1,2 +0,0 @@
**You can also find more exercises here:**
* https://www.rosettacode.org/wiki/Category:Iteration