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

func addUp() {}
func addUp(x int) int {
var c int = 0
var i int
for i = 1; i <= x; i++ {
c += i
}
return c
}
18 changes: 17 additions & 1 deletion exercise1/problem10/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
package main

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

func sum(a, b string) (string, error) {
sum1, err := strconv.Atoi(a)
if err != nil {
return "", errors.New("string: a cannot be converted")
}
sum2, err := strconv.Atoi(b)
if err != nil {
return "", errors.New("string: b cannot be converted")
}
return fmt.Sprintf("%v", sum1+sum2), nil
}
14 changes: 13 additions & 1 deletion exercise1/problem2/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
package main

func binary() {}
import "fmt"

func binary(x int) string {
var result string
if x == 0 {
return "0"
}
for x > 0 {
result = fmt.Sprintf("%v", x%2) + result
x = x / 2
}
return result
}
9 changes: 8 additions & 1 deletion exercise1/problem3/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
package main

func numberSquares() {}
func numberSquares(x int) int {
var ac int
for x > 0 {
ac += x * x
x -= 1
}
return ac
}
12 changes: 11 additions & 1 deletion exercise1/problem4/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
package main

func detectWord() {}
import "unicode"

func detectWord(str string) string {
var result string
for _, ch := range str {
if unicode.IsLower(ch) {
result += string(ch)
}
}
return result
}
6 changes: 5 additions & 1 deletion exercise1/problem5/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package main

func potatoes() {}
import "strings"

func potatoes(str string) int {
return strings.Count(str, "potato")
}
21 changes: 20 additions & 1 deletion exercise1/problem6/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
package main

func emojify() {}
import "strings"

func emojify(str string) string {
return strings.Replace(
strings.Replace(
strings.Replace(
strings.Replace(str, "smile", "🙂", -1),
"grin",
"😀",
-1,
),
"sad",
"😥",
-1,
),
"mad",
"😠",
-1,
)
}
12 changes: 11 additions & 1 deletion exercise1/problem7/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
package main

func highestDigit() {}
func highestDigit(num int) int {
var max int = 0
for num > 1 {
cur := num % 10
num = num / 10
if cur > max {
max = cur
}
}
return max
}
11 changes: 10 additions & 1 deletion exercise1/problem8/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
package main

func countVowels() {}
import "strings"

func countVowels(str string) int {
var c int = 0
var vowels = []string{"a", "e", "i", "o", "u"}
for _, ch := range vowels {
c += strings.Count(str, ch)
}
return c
}
25 changes: 22 additions & 3 deletions exercise1/problem9/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
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
}

// func bitwise(a int) string {
// var result string
// for i := 0; i < 8; i++ {
// if a > 0 {
// result = fmt.Sprintf("%v", a%2) + result
// a = a / 2
// } else {
// result = "0" + result
// }
// }
// return result
// }
3 changes: 2 additions & 1 deletion exercise2/problem1/problem1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package problem1

func isChangeEnough() {
func isChangeEnough(coins [4]int, cost float32) bool {
return (float32(coins[0])*0.25 + float32(coins[1])*0.10 + float32(coins[2])*0.05 + float32(coins[3])*0.01) >= cost
}
11 changes: 10 additions & 1 deletion exercise2/problem10/problem10.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
package problem10

func factory() {}
func factory() (map[string]int, func(string) func(int)) {
m := map[string]int{}
f := func(model string) func(int) {
m[model] = 0
return func(inc int) {
m[model] += inc
}
}
return m, f
}
18 changes: 17 additions & 1 deletion exercise2/problem11/problem11.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
package problem11

func removeDups() {}
func removeDups[T int | bool | string](sl []T) []T {
var result []T
var l bool = false
for i, a := range sl {
l = false
for _, b := range result {
if a == b {
l = true
break
}
}
if l == false {
result = append(result, sl[i])
}
}
return result
}
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 (
"slices"
)

func keysAndValues[T int | string, P int | bool | string](m map[T]P) ([]T, []P) {

var keys []T
var values []P
for key, value := range m {
keys = append(keys, key)
values = append(values, value)
}

slices.SortFunc(keys, func(a, b T) int {
if a > b {
return 1
}
return -1
})
return keys, values
}
17 changes: 16 additions & 1 deletion exercise2/problem2/problem2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
package problem2

func capitalize() {
import (
"strings"
"unicode"
)

func capitalize(names []string) []string {
for i, name := range names {
names[i] = strings.ToLower(name)
r := []rune(names[i])
if len(r) == 0 {
continue
}
r[0] = unicode.ToUpper(r[0])
names[i] = string(r)
}
return names
}
19 changes: 18 additions & 1 deletion exercise2/problem3/problem3.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,22 @@ const (
lr dir = "lr"
)

func diagonalize() {
func diagonalize(d int, direction dir) [][]int {
matrix := make([][]int, d)
for i := 0; i < d; i++ {
matrix[i] = make([]int, d)
for j := 0; j < d; j++ {
switch {
case direction == ul:
matrix[i][j] = j + i
case direction == lr:
matrix[i][j] = (d-1)*2 - i - j
case direction == ur:
matrix[i][j] = (d - 1) + i - j
case direction == ll:
matrix[i][j] = (d - 1) + j - i
}
}
}
return matrix
}
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(arr []string) map[string]string {
result := map[string]string{}
for _, value := range arr {
result[value] = strings.ToUpper(value)
}
return result
}
19 changes: 18 additions & 1 deletion exercise2/problem5/problem5.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
package problem5

func products() {
import "sort"

func products(m map[string]int, price int) []string {
result := []string{}
for key, val := range m {
if val > price {
result = append(result, key)
}
}

sort.Slice(result, func(i, j int) bool {
if m[result[i]] == m[result[j]] {
return result[i] < result[j]
}
return m[result[i]] > m[result[j]]
})

return result
}
10 changes: 9 additions & 1 deletion exercise2/problem6/problem6.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
package problem6

func sumOfTwo() {
func sumOfTwo(a []int, b []int, sum int) bool {
for _, val1 := range a {
for _, val2 := range b {
if val1+val2 == sum {
return true
}
}
}
return false
}
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
}
8 changes: 4 additions & 4 deletions exercise2/problem8/problem8.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ func simplify(list []string) map[string]int {
var indMap map[string]int

indMap = make(map[string]int)
load(&indMap, &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
}
}
11 changes: 10 additions & 1 deletion exercise2/problem9/problem9.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
package problem9

func factory() {}
func factory(a int) func(vals ...int) []int {
fun := func(vals ...int) []int {
for i, val := range vals {
vals[i] = val * a
}
return vals
}

return fun
}
Loading