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

func addUp() {}
func addUp(num int) int {

sum := 0

for i := 1; i <= num; i++ {
sum += i
}

return sum
}
22 changes: 21 additions & 1 deletion exercise1/problem10/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
package main

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

func sum(a string, b string) (string, error) {

aNum, err := strconv.Atoi(a)
if err != nil {

return "", fmt.Errorf("string: %s cannot be converted", a)
}

bNum, err := strconv.Atoi(b)
if err != nil {

return "", fmt.Errorf("string: %s cannot be converted", b)
}

return strconv.Itoa(aNum + bNum), nil
}
19 changes: 18 additions & 1 deletion exercise1/problem2/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
package main

func binary() {}
import "strconv"

func binary(num int) string {
output := ""

for {
digit := strconv.Itoa(num % 2)
num /= 2

output = digit + output

if num == 0 {
break
}
}

return output
}
11 changes: 10 additions & 1 deletion exercise1/problem3/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
package main

func numberSquares() {}
func numberSquares(num int) int {

squares := 0

for i := 1; i <= num; i++ {
squares += (i * i)
}

return squares
}
16 changes: 15 additions & 1 deletion exercise1/problem4/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
package main

func detectWord() {}
func detectWord(word string) string {
wanted := ""

for _, char := range word {
// if unicode.IsLower(char) {
// wanted += string(char)
// }
code := int(char)
if code >= 97 && code <= 122 {
wanted += string(char)
}
}

return wanted
}
22 changes: 21 additions & 1 deletion exercise1/problem5/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
package main

func potatoes() {}
import "strings"

func potatoes(word string) int {

// return strings.Count(word, "potato")

substr := "potato"
count := 0

for {
index := strings.Index(word, substr)
if index == -1 {
break
}

count += 1
word = word[index+len(substr):]
}

return count
}
18 changes: 17 additions & 1 deletion exercise1/problem6/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
package main

func emojify() {}
import "strings"

func emojify(word string) string {

matches := map[string]string{
"smile": "🙂",
"grin": "😀",
"sad": "😥",
"mad": "😠",
}

for k, v := range matches {
word = strings.Replace(word, k, v, -1)
}

return word
}
19 changes: 18 additions & 1 deletion exercise1/problem7/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
package main

func highestDigit() {}
func highestDigit(num int) int {

highest := 0

for {
digit := num % 10
if digit > highest {
highest = digit
}

num /= 10
if num == 0 {
break
}
}

return highest
}
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 "unicode"

func countVowels(word string) int {

cnt := 0

vowels := map[string]bool{
"a": true,
"e": true,
"i": true,
"o": true,
"u": true,
}

for _, ch := range word {
ch = unicode.ToLower(ch)

_, ok := vowels[string(ch)]
if ok {
cnt += 1
}
}

return cnt
}
12 changes: 9 additions & 3 deletions exercise1/problem9/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package main

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

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

func bitwiseXOR() {}
func bitwiseXOR(a int, b int) int {
return a ^ b
}
10 changes: 9 additions & 1 deletion exercise2/problem1/problem1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
package problem1

func isChangeEnough() {
func isChangeEnough(changes [4]int, total float32) bool {
current := float32(0)

current += float32(changes[0]) * 0.25
current += float32(changes[1]) * 0.10
current += float32(changes[2]) * 0.05
current += float32(changes[3]) * 0.01

return current >= total
}
14 changes: 13 additions & 1 deletion exercise2/problem10/problem10.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
package problem10

func factory() {}
func factory() (map[string]int, func(string) func(int)) {

brands := make(map[string]int)

makeBrand := func(name string) func(int) {
brands[name] = 0
return func(increment int) {
brands[name] += increment
}
}

return brands, makeBrand
}
14 changes: 13 additions & 1 deletion exercise2/problem11/problem11.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
package problem11

func removeDups() {}
func removeDups[T comparable](list []T) []T {
seen := map[T]struct{}{}
output := make([]T, 0, len(list))
for _, v := range list {
if _, ok := seen[v]; ok {
continue
}
output = append(output, v)
seen[v] = struct{}{}
}

return output
}
22 changes: 21 additions & 1 deletion exercise2/problem12/problem12.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
package problem11

func keysAndValues() {}
import "sort"

func keysAndValues[K string | int, V comparable](mp map[K]V) ([]K, []V) {

keys := make([]K, 0, len(mp))
values := make([]V, 0, len(mp))

for k := range mp {
keys = append(keys, k)
}

sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})

for _, k := range keys {
values = append(values, mp[k])
}

return keys, values
}
21 changes: 20 additions & 1 deletion exercise2/problem2/problem2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
package problem2

func capitalize() {
import (
"unicode"
)

func capitalize(names []string) []string {
for i, v := range names {
// names[i] = strings.Title(strings.ToLower(v))

capitalized := []rune(v)
for pos, char := range capitalized {
if pos == 0 {
capitalized[pos] = unicode.ToUpper(char)
continue
}
capitalized[pos] = unicode.ToLower(char)
}
names[i] = string(capitalized)
}

return names
}
11 changes: 10 additions & 1 deletion exercise2/problem4/problem4.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
package problem4

func mapping() {
import "strings"

func mapping(letters []string) map[string]string {
mp := make(map[string]string)

for _, l := range letters {
mp[strings.ToLower(l)] = strings.ToUpper(l)
}

return mp
}
36 changes: 35 additions & 1 deletion exercise2/problem5/problem5.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
package problem5

func products() {
import (
"sort"
)

func products(products map[string]int, price int) []string {

pricesDict := make(map[int][]string)
prices := make([]int, 0, len(products))

for k, v := range products {
if v < price {
delete(products, k)
continue
}
if _, ok := pricesDict[v]; !ok {
prices = append(prices, v)
}
pricesDict[v] = append(pricesDict[v], k)
}
sort.Slice(prices, func(i, j int) bool {
return prices[i] > prices[j]
})

out := make([]string, 0, len(products))

for _, price := range prices {
keys := pricesDict[price]

if len(keys) > 1 {
sort.Strings(keys)
}
out = append(out, keys...)
}

return out
}
3 changes: 2 additions & 1 deletion exercise2/problem7/problem7.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package problem7

func swap() {
func swap(a *int, b *int) {
*a, *b = *b, *a
}
10 changes: 5 additions & 5 deletions exercise2/problem8/problem8.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package problem8
func simplify(list []string) map[string]int {
var indMap map[string]int

indMap = make(map[string]int)
load(&indMap, &list)
indMap = make(map[string]int, len(list))
load(indMap, list)

return indMap
}

func load(m *map[string]int, students *[]string) {
for i, name := range *students {
(*m)[name] = i
func load(m map[string]int, students []string) {
for i, name := range students {
m[name] = i
}
}
Loading