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
18 changes: 17 additions & 1 deletion exercise1/problem1/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
package main

func addUp() {}
import (
"fmt"
)

func addUp(n int) int {
total := 0
for i := 1; i <= n; i++ {
total += i
}
return total
}

func main() {
fmt.Println(addUp(4))
fmt.Println(addUp(13))
fmt.Println(addUp(600))
}
27 changes: 26 additions & 1 deletion exercise1/problem10/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
package main

func sum() {}
import (
"fmt"
"strconv"
)

func sum(a, b string) (string, error) {
num1, err1 := strconv.Atoi(a)
if err1 != nil {
return "", fmt.Errorf("string: %s cannot be converted", a)
}

num2, err2 := strconv.Atoi(b)
if err2 != nil {
return "", fmt.Errorf("string: %s cannot be converted", b)
}

result := num1 + num2

return strconv.Itoa(result), nil
}

func main() {
fmt.Println(sum("1", "2"))
fmt.Println(sum("10", "20"))
fmt.Println(sum("a", "2"))
}
15 changes: 14 additions & 1 deletion exercise1/problem2/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
package main

func binary() {}
import (
"fmt"
"strconv"
)

func binary(decimal int) string {
return strconv.FormatInt(int64(decimal), 2)
}

func main() {
fmt.Println(binary(1))
fmt.Println(binary(5))
fmt.Println(binary(10))
}
19 changes: 18 additions & 1 deletion exercise1/problem3/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
package main

func numberSquares() {}
import (
"fmt"
)

func numberSquares(n int) int {
sum := 0
for k := 1; k <= n; k++ {
sum += (n - k + 1) * (n - k + 1)
}
return sum
}

func main() {
fmt.Println(numberSquares(2))
fmt.Println(numberSquares(3))
fmt.Println(numberSquares(4))
fmt.Println(numberSquares(5))
}
22 changes: 21 additions & 1 deletion exercise1/problem4/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
package main

func detectWord() {}
import (
"fmt"
"strings"
)

func detectWord(crowd string) string {
var result strings.Builder
for _, char := range crowd {
if char >= 'a' && char <= 'z' {
result.WriteRune(char)
}
}

return result.String()
}

func main() {
fmt.Println(detectWord("UcUNFYGaFYFYGtNUH"))
fmt.Println(detectWord("bEEFGBuFBRrHgUHlNFYaYr"))
fmt.Println(detectWord("YFemHUFBbezFBYzFBYLleGBYEFGBMENTment"))
}
28 changes: 27 additions & 1 deletion exercise1/problem5/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
package main

func potatoes() {}
import (
"fmt"
"strings"
)

func potatoes(s string) int {
count := 0
substring := "potato"

for {
index := strings.Index(s, substring)
if index == -1 {
break
}
count++
s = s[index+len(substring):]
}

return count
}

func main() {
fmt.Println(potatoes("potato"))
fmt.Println(potatoes("potatopotato"))
fmt.Println(potatoes("potatoapple"))
fmt.Println(potatoes("potatopotatopotat"))
}
32 changes: 31 additions & 1 deletion exercise1/problem6/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
package main

func emojify() {}
import (
"fmt"
"strings"
)

func emojify(sentence string) string {
emojis := map[string]string{
"smile": "🙂",
"grin": "😀",
"sad": "😥",
"mad": "😠",
}

words := strings.Fields(sentence)

for i, word := range words {
cleanWord := strings.Trim(word, ",.!?;:")
if emoji, exists := emojis[cleanWord]; exists {
words[i] = strings.Replace(word, cleanWord, emoji, 1)
}
}

return strings.Join(words, " ")
}

func main() {
fmt.Println(emojify("Make me smile"))
fmt.Println(emojify("Make me grin"))
fmt.Println(emojify("Make me sad"))
fmt.Println(emojify("I am so mad!"))
}
27 changes: 26 additions & 1 deletion exercise1/problem7/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
package main

func highestDigit() {}
import (
"fmt"
"strconv"
)

func highestDigit(num int) int {
numStr := strconv.Itoa(num)

highest := 0

for _, char := range numStr {
digit, _ := strconv.Atoi(string(char))

if digit > highest {
highest = digit
}
}

return highest
}

func main() {
fmt.Println(highestDigit(379))
fmt.Println(highestDigit(2))
fmt.Println(highestDigit(377401))
}
26 changes: 25 additions & 1 deletion exercise1/problem8/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
package main

func countVowels() {}
import (
"fmt"
"strings"
)

func countVowels(s string) int {
vowels := "aeiou"
count := 0

s = strings.ToLower(s)

for _, char := range s {
if strings.ContainsRune(vowels, char) {
count++
}
}

return count
}

func main() {
fmt.Println(countVowels("Celebration"))
fmt.Println(countVowels("Palm"))
fmt.Println(countVowels("Prediction"))
}
32 changes: 29 additions & 3 deletions exercise1/problem9/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
package main

func bitwiseAND() {}
import (
"fmt"
)

func bitwiseOR() {}
func toBinary(n int) string {
return fmt.Sprintf("%08b", n)
}

func bitwiseXOR() {}
func bitwiseAND(a, b int) int {
return a & b
}

func bitwiseOR(a, b int) int {
return a | b
}

func bitwiseXOR(a, b int) int {
return a ^ b
}

func main() {
a := 6
b := 23

fmt.Println("a =", toBinary(a))
fmt.Println("b =", toBinary(b))

fmt.Println("bitwiseAND =", toBinary(bitwiseAND(a, b)))
fmt.Println("bitwiseOR =", toBinary(bitwiseOR(a, b)))
fmt.Println("bitwiseXOR =", toBinary(bitwiseXOR(a, b)))
}
20 changes: 19 additions & 1 deletion exercise2/problem1/problem1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
package problem1

func isChangeEnough() {
import (
"fmt"
)

func isChangeEnough(change [4]int, amountDue float32) bool {

totalDueCents := int(amountDue * 100)

totalChangeCents := change[0]*25 + change[1]*10 + change[2]*5 + change[3]*1

return totalChangeCents >= totalDueCents
}

func main() {
fmt.Println(isChangeEnough([4]int{2, 100, 0, 0}, 14.11))
fmt.Println(isChangeEnough([4]int{0, 0, 20, 5}, 0.75))
fmt.Println(isChangeEnough([4]int{30, 40, 20, 5}, 12.55))
fmt.Println(isChangeEnough([4]int{10, 0, 0, 50}, 3.85))
fmt.Println(isChangeEnough([4]int{1, 0, 5, 219}, 19.99))
}
36 changes: 35 additions & 1 deletion exercise2/problem10/problem10.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
package problem10

func factory() {}
import (
"fmt"
)

func factory() (map[string]int, func(string) func(int) int) {
brands := make(map[string]int)

makeBrand := func(brand string) func(int) int {

if _, exists := brands[brand]; !exists {
brands[brand] = 0
}

return func(count int) int {

brands[brand] += count
return brands[brand]
}
}

return brands, makeBrand
}

func main() {
brands, makeBrand := factory()

toyotaIncrementer := makeBrand("Toyota")
toyotaIncrementer(1)
toyotaIncrementer(2)
hyundaiIncrementer := makeBrand("Hyundai")
hyundaiIncrementer(5)
makeBrand("Kia")

fmt.Println(brands)
}
25 changes: 24 additions & 1 deletion exercise2/problem11/problem11.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
package problem11

func removeDups() {}
import (
"fmt"
)

func removeDups[T comparable](items []T) []T {
seen := make(map[T]bool)
result := []T{}

for _, item := range items {
if !seen[item] {
seen[item] = true
result = append(result, item)
}
}

return result
}

func main() {

fmt.Println(removeDups([]int{1, 0, 1, 0}))
fmt.Println(removeDups([]bool{true, false, false, true}))
fmt.Println(removeDups([]string{"John", "Taylor", "John"}))
}
Loading