Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions answers/Go/@codehakase/01-name-concatenation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import "fmt"

func main() {
var (
firstname string
lastname string
age int
)
fmt.Print("Enter first name: ")
fmt.Scanf("%s\n", &firstname)

fmt.Print("Enter last name: ")
fmt.Scanf("%s\n", &lastname)

fmt.Print("Enter your age: ")
fmt.Scanf("%d\n", &age)
fmt.Print("\n")

fmt.Printf("Welcome, %s %s (%d)\n", firstname, lastname, age)
}
28 changes: 28 additions & 0 deletions answers/Go/@codehakase/02-lottery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"bufio"
"fmt"
"math/rand"
"os"
"time"
)

func main() {
rand.Seed(time.Now().UnixNano())
var a, b, c int
for {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Press Enter key to continue...")
_, _ = reader.ReadString('\n')
a = rand.Intn(10)
b = rand.Intn(10)
c = rand.Intn(10)
fmt.Printf("%d %d %d\n", a, b, c)
if a == 7 || b == 7 || c == 7 {
fmt.Println("Congratulations!!\n")
} else {
fmt.Println("Try again! Better luck next time.\n")
}
}
}
16 changes: 16 additions & 0 deletions answers/Go/@codehakase/03-word-in-reverse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import "fmt"

func main() {
var word string
for {
fmt.Print("Enter a word: ")
fmt.Scanf("%s\n", &word)

for i := len(word) - 1; i >= 0; i-- {
fmt.Printf("%c", word[i])
}
fmt.Println()
}
}
32 changes: 32 additions & 0 deletions answers/Go/@codehakase/04-type-in-reverse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"
"math/rand"
"reflect"
"time"
)

func main() {
rand.Seed(time.Now().UnixNano())
words := []string{"Discombobulated", "Paradise", "California", "Machinery"}
for {
var userInput string
randWord := words[rand.Intn(len(words))]
fmt.Printf("Type the word '%s' in reverse: ", randWord)
fmt.Scanf("%s\n", &userInput)
if reflect.DeepEqual(userInput, reverseStr(randWord)) {
fmt.Println("\u2705")
} else {
fmt.Println("\u274C")
}
}
}

func reverseStr(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
87 changes: 87 additions & 0 deletions answers/Go/@codehakase/05-temperature-unit-conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"errors"
"fmt"
)

// ErrInvalidUnit is returned when the unit from Stdin
// is not one of the valid units.
var ErrInvalidUnit = errors.New("invalid unit selected")

// Unit represents a temperature unit
type Unit int

const (
// CelsiusToFahrenheit represets a celsius to fahrenheit conversion unit
CelsiusToFahrenheit Unit = 1
// FahrenheitToCelsius represets a farenheit to celsius conversion unit
FahrenheitToCelsius Unit = 2
// CelsiusToKelvin represets a celsius to kelvin conversion unit
CelsiusToKelvin Unit = 3
// KelvinToCelsius represets a kelvin to celsius conversion unit
KelvinToCelsius Unit = 4
// FahrenheitToKelvin represets a farenheit to kelvin conversion unit
FahrenheitToKelvin Unit = 5
// KelvinToFahrenheit represets a kelvin to fahrenheit conversion unit
KelvinToFahrenheit Unit = 6
)

func main() {
var (
optionUnit Unit
input float64
conv float64
)

for {
fmt.Print(notice())
fmt.Scanf("%d\n", &optionUnit)
switch optionUnit {
case CelsiusToFahrenheit:
fmt.Print("Enter a celsius value: ")
fmt.Scanf("%f\n", &input)
conv = (input * (9.0 / 5.0)) + 32.0
fmt.Printf("Fahrenheit value is %.3f ⁰F\n", conv)
case FahrenheitToCelsius:
fmt.Print("Enter a fahrenheit value: ")
fmt.Scanf("%f\n", &input)
conv = (input - 32) * (5.0 / 9.0)
fmt.Printf("Celsius value is %.3f ⁰F\n", conv)
case CelsiusToKelvin:
fmt.Print("Enter a celsius value: ")
fmt.Scanf("%f\n", &input)
conv = (input + 273.15)
fmt.Printf("Kelvin value is %.3fK\n", conv)
case KelvinToCelsius:
fmt.Print("Enter kelvin value: ")
fmt.Scanf("%f\n", &input)
conv = (input - 273.15)
fmt.Printf("Celsius value is %.3f ⁰C\n", conv)
case FahrenheitToKelvin:
fmt.Print("Enter a fahrenheit value: ")
fmt.Scanf("%f\n", &input)
conv = (input-32)*(5.0/9.0) + 273.15
fmt.Printf("Kelvin value is %.3fK\n", conv)
case KelvinToFahrenheit:
fmt.Print("Enter kelvin value: ")
fmt.Scanf("%f\n", &input)
conv = (input-273.15)*(9.0/5.0) + 32
fmt.Printf("Fahrenheit value is %.3f ⁰F\n", conv)
default:
fmt.Println(ErrInvalidUnit)
}
}
}

func notice() string {
return `
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Celsius to Kelvin
4. Kelvin to Celsius
5. Fahrenheit to Kelvin
6. Kelvin to Fahrenheit

Enter an option: `
}
12 changes: 12 additions & 0 deletions answers/Go/@codehakase/06-multiplicaiton-table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import "fmt"

func main() {
for i := 1; i <= 12; i++ {
for j := 2; j <= 12; j++ {
fmt.Print(fmt.Sprintf("%v\t", i*j))
}
fmt.Println()
}
}
67 changes: 67 additions & 0 deletions answers/Go/@codehakase/07-read-line-by-line.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"bufio"
"fmt"
"io"
"log"
"os"
"time"
)

func main() {
file, err := os.OpenFile("./data/07-read-line-by-line.txt", os.O_RDONLY, os.ModePerm)
if err != nil {
log.Fatalf("failed to open file: %v", err)
os.Exit(1)
}
defer file.Close()
scanSimple(file)
//scanLinesToFile(file)
//delayedScan(file)
}

func scanSimple(file *os.File) {
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(scanner.Text()) // read line
}

if err := scanner.Err(); err != nil {
log.Fatalf("failed to scan lines: %v", err)
os.Exit(1)
}
}

func delayedScan(file *os.File) {
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(scanner.Text()) // read line
time.Sleep(time.Second * 1)
}

if err := scanner.Err(); err != nil {
log.Fatalf("failed to scan lines: %v", err)
os.Exit(1)
}
}

func scanLinesToFile(file *os.File) {
scanner := bufio.NewScanner(file)
newFile, err := os.Create("./read-line-to-file.txt")
if err != nil {
log.Fatal(err)
}
defer newFile.Close()
for scanner.Scan() {
_, err := io.WriteString(newFile, fmt.Sprintf("%s\n", scanner.Text()))
if err != nil {
log.Fatal(err)
}
err = newFile.Sync()
}
if err := scanner.Err(); err != nil {
log.Fatalf("failed to scan lines: %v", err)
os.Exit(1)
}
}
27 changes: 27 additions & 0 deletions answers/Go/@codehakase/08-combine-two-files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"strings"
)

func main() {
firstFile, err := ioutil.ReadFile("./data/08-combine-two-files-1.txt")
if err != nil {
log.Fatalf("failed to open file1: %v", err)
}
secondFile, err := ioutil.ReadFile("./data/08-combine-two-files-2.txt")
if err != nil {
log.Fatalf("failed to open file2: %v", err)
}
file1Words := strings.Split(string(firstFile), "\n")
file2Words := strings.Split(string(secondFile), "\n")
words := []string{}
for i := 0; i < len(file1Words); i++ {
word := fmt.Sprintf("%s %s\n", file1Words[i], file2Words[i])
words = append(words, word)
}
fmt.Println(strings.Join(words, ""))
}
27 changes: 27 additions & 0 deletions answers/Go/@codehakase/09-rgb-color-generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"bufio"
"fmt"
"math/rand"
"os"
"time"
)

func main() {
rand.Seed(time.Now().UnixNano())
for {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Press enter to continue...")
reader.ReadString('\n')
fmt.Printf("%s\n", genRGBValues())
}
}

func genRGBValues() string {
var r, g, b int
r = rand.Intn(255)
g = rand.Intn(255)
b = rand.Intn(255)
return fmt.Sprintf("rgb(%d, %d, %d)", r, g, b)
}
17 changes: 17 additions & 0 deletions answers/Go/@codehakase/10-hex-color-generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"math/rand"
"strconv"
"time"
)

func main() {
rand.Seed(time.Now().UnixNano())

fmt.Printf("#%s%s%s\n",
strconv.FormatInt(int64(rand.Intn(256)), 16),
strconv.FormatInt(int64(rand.Intn(256)), 16),
strconv.FormatInt(int64(rand.Intn(256)), 16))
}
19 changes: 19 additions & 0 deletions answers/Go/@codehakase/11-extract-file-ext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
"strings"
)

func main() {
paths := []string{
"/Users/mykeels/Documents/file.txt",
"/Users/mykeels/Documents/file.csv",
"/Users/mykeels/Music/a-whole-new-world.mp3",
"/Users/mykeels/Movies/a-day-to-remember.mp4",
}
for _, path := range paths {
parts := strings.Split(path, ".")
fmt.Println(parts[len(parts)-1])
}
}
22 changes: 22 additions & 0 deletions answers/Go/@codehakase/12-salary-classifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import "fmt"

func main() {
var salary int
for {
fmt.Print("Enter salary amount: ")
fmt.Scanf("%d\n", &salary)
if salary < 1 {
fmt.Println("Invalid salary amount")
}
if salary < 50000 {
fmt.Println("Basic Earner")
} else if salary <= 200000 {
fmt.Println("Mid Earner")
}
if salary > 200000 {
fmt.Println("High Earner")
}
}
}
Loading