add: x-tba/foundations
This commit is contained in:
@ -0,0 +1,55 @@
|
||||
// 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/
|
||||
//
|
||||
|
||||
// go run . {1..100}
|
||||
|
||||
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 {
|
||||
// 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; {
|
||||
// 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()
|
||||
}
|
44
x-tba/foundations/03-if-switch-loop/02-switch-months/main.go
Normal file
44
x-tba/foundations/03-if-switch-loop/02-switch-months/main.go
Normal file
@ -0,0 +1,44 @@
|
||||
// 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"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 2 {
|
||||
fmt.Println("Give me a month name")
|
||||
return
|
||||
}
|
||||
|
||||
year := time.Now().Year()
|
||||
leap := year%4 == 0 && (year%100 != 0 || year%400 == 0)
|
||||
|
||||
days, month := 28, os.Args[1]
|
||||
|
||||
switch strings.ToLower(month) {
|
||||
case "april", "june", "september", "november":
|
||||
days = 30
|
||||
case "january", "march", "may", "july",
|
||||
"august", "october", "december":
|
||||
days = 31
|
||||
case "february":
|
||||
if leap {
|
||||
days = 29
|
||||
}
|
||||
default:
|
||||
fmt.Printf("%q is not a month.\n", month)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("%q has %d days.\n", month, days)
|
||||
}
|
@ -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()
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
// 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[0])
|
||||
if err != nil {
|
||||
fmt.Println("Not a number.")
|
||||
return
|
||||
}
|
||||
|
||||
if guess < 0 {
|
||||
fmt.Println("Please pick a positive number.")
|
||||
return
|
||||
}
|
||||
|
||||
for turn := 0; turn < maxTurns; turn++ {
|
||||
n := rand.Intn(guess + 1)
|
||||
|
||||
if verbose {
|
||||
fmt.Printf("%d ", n)
|
||||
}
|
||||
|
||||
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,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"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Get and split the PATH environment variable
|
||||
|
||||
// SplitList function automatically finds the
|
||||
// separator for the path env variable
|
||||
words := filepath.SplitList(os.Getenv("PATH"))
|
||||
|
||||
// Alternative way, but above one is better:
|
||||
// words := strings.Split(
|
||||
// os.Getenv("PATH"),
|
||||
// string(os.PathListSeparator))
|
||||
|
||||
query := os.Args[1:]
|
||||
|
||||
for _, q := range query {
|
||||
for i, w := range words {
|
||||
q, w = strings.ToLower(q), strings.ToLower(w)
|
||||
|
||||
if strings.Contains(w, q) {
|
||||
fmt.Printf("#%-2d: %q\n", i+1, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user