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(i int) int {
var sum int = 0
for j := 1; j <= i; j++ {
sum += int(j)
}

return sum
}
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 (
"fmt"
"strconv"
)

func sum(str1 string, str2 string) (string, error) {
val1, err := strconv.Atoi(string(str1))
val2, err2 := strconv.Atoi(string(str2))

if err != nil {
return "", err
} else if err2 != nil {
return "", err2
}

return fmt.Sprintf("%d", val1+val2), nil
}
40 changes: 39 additions & 1 deletion exercise1/problem2/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
package main

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

func binary(num int) string {
way := 2
var result string

switch way {
case 1:
result = ConvertInt(num, 2)
case 2:
result = convert(num)
}

return result
}

func convert(num int) string {
n := 1

for n*2 <= num {
n = n * 2
}

res := ""
for n >= 1 {
res = res + fmt.Sprintf("%d", num/n)
num = num % n
n = n / 2
}

return res
}

func ConvertInt(val int, toBase int) string {
return strconv.FormatInt(int64(val), toBase)
}
4 changes: 3 additions & 1 deletion exercise1/problem3/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package main

func numberSquares() {}
func numberSquares(n int) int {
return (n * (n + 1) * (2*n + 1)) / 6
}
10 changes: 9 additions & 1 deletion exercise1/problem4/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
package main

func detectWord() {}
func detectWord(str string) string {
var result string
for _, letter := range str {
if letter <= 122 && letter >= 97 {
result = result + string(letter)
}
}
return result
}
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 "strings"

func potatoes(text string) int {
way := 2
var res int
switch way {
case 1:
res = strings.Count(text, "potato")
case 2:
res = way2(text)
}

return res
}

func way2(text string) int {
substr := "potato"
substrLen := len(substr)
count := 0

for i := 0; i <= len(text)-substrLen; i++ {
if text[i:i+substrLen] == substr {
count++
}
}
return count
}
31 changes: 30 additions & 1 deletion exercise1/problem6/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
package main

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

words := []rune(sentence)
emojiSentence := ""

i := 0
for i < len(words) {
match := false
for word, emoji := range wordToEmoji {
if i+len(word) <= len(words) && sentence[i:i+len(word)] == word {
emojiSentence += emoji
i += len(word)
match = true
break
}
}
if !match {
emojiSentence += string(words[i])
i++
}
}

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

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

func highestDigit(num int) int {
highest := 0
strnum := fmt.Sprintf("%d", num)
for _, val := range strnum {
newVal, _ := strconv.Atoi(string(val))
if newVal > highest {
highest = int(newVal)
}
}
return highest
}
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() {}
func countVowels(str string) int {
var count int
for _, ch := range str {
if ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' {
count++
}
}

return count
}
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(val1 int, val2 int) int {
return val1 & val2
}

func bitwiseOR() {}
func bitwiseOR(val1 int, val2 int) int {
return val1 | val2
}

func bitwiseXOR() {}
func bitwiseXOR(val1 int, val2 int) int {
return val1 ^ val2
}
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(change [4]int, total float32) bool {
quarters := float32(change[0]) * 0.25
dimes := float32(change[1]) * 0.10
nickels := float32(change[2]) * 0.05
pennies := float32(change[3]) * 0.01

totalChange := quarters + dimes + nickels + pennies

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

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

makeBrand := func(brand string) func(int) {
if _, exists := brands[brand]; !exists {
brands[brand] = 0
}

return func(count int) {
brands[brand] += count
}
}

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

func removeDups() {}
func removeDups[T int | bool | string](items []T) []T {
var result []T
var check = make(map[T]bool)
for _, item := range items {
if !check[item] {
result = append(result, item)
check[item] = true
}
}

return result
}
25 changes: 24 additions & 1 deletion exercise2/problem12/problem12.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
package problem11

func keysAndValues() {}
import (
"fmt"
"sort"
)

func keysAndValues[K comparable, V any](m map[K]V) ([]K, []V) {
keys := make([]K, 0, len(m))
for k := range m {
keys = append(keys, k)
}

// Sort keys
sort.Slice(keys, func(i, j int) bool {
return fmt.Sprintf("%v", keys[i]) < fmt.Sprintf("%v", keys[j])
})

sortedValues := make([]V, len(keys))
fmt.Println(m)
for i, k := range keys {
sortedValues[i] = m[k]
}

return keys, sortedValues
}
16 changes: 15 additions & 1 deletion exercise2/problem2/problem2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
package problem2

func capitalize() {
import "unicode"

func capitalize(names []string) []string {
for i, name := range names {
if len(name) > 0 {
runes := []rune(name)
for j := range runes {
if j == 0 {
runes[j] = unicode.ToUpper(runes[j])
}
}
names[i] = string(runes)
}
}
return names
}
37 changes: 36 additions & 1 deletion exercise2/problem3/problem3.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,40 @@ const (
lr dir = "lr"
)

func diagonalize() {
func diagonalize(n int, direction dir) [][]int {
matrix := make([][]int, n)
for i := range matrix {
matrix[i] = make([]int, n)
}

if direction == "ul" {
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
matrix[i][j] = i + j
}
}
}
if direction == "ur" {
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
matrix[i][j] = (n - 1 - j) + i
}
}
}
if direction == "ll" {
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
matrix[i][j] = (n - 1 - i) + j
}
}
}
if direction == "lr" {
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
matrix[i][j] = (n - 1 - i) + (n - 1 - j)
}
}
}

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(inpt []string) map[string]string {
result := make(map[string]string)

for _, in := range inpt {
result[in] = strings.ToUpper(in)
}

return result
}
22 changes: 21 additions & 1 deletion exercise2/problem5/problem5.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
package problem5

func products() {
import (
"sort"
)

func products(catalog map[string]int, minPrice int) []string {
var result []string

for name, price := range catalog {
if price >= minPrice {
result = append(result, name)
}
}

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

return result
}
Loading