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

func addUp() {}
func addUp(n int) int {
summa := 0
for i := 1; i <= n; i++ {
summa += i
}
return summa
}
25 changes: 24 additions & 1 deletion exercise1/problem10/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
package main

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

func sum(a, b string) (string, error) {
// Convert the first string to an integer
intA, err := strconv.Atoi(a)
if err != nil {
return "", fmt.Errorf("string: %s cannot be converted", a)
}

// Convert the second string to an integer
intB, err := strconv.Atoi(b)
if err != nil {
return "", fmt.Errorf("string: %s cannot be converted", b)
}

// Sum the integers
result := intA + intB

// Convert the result back to a string and return it
return strconv.Itoa(result), nil
}
6 changes: 5 additions & 1 deletion exercise1/problem2/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package main

func binary() {}
import "strconv"

func binary(n int) string {
return strconv.FormatInt(int64(n), 2)
}
8 changes: 7 additions & 1 deletion exercise1/problem3/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
package main

func numberSquares() {}
func numberSquares(n int) int {
quantity := 0
for i := 1; i <= n; i++ {
quantity += (n - i + 1) * (n - i + 1)
}
return quantity
}
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() {}
import (
"unicode"
)

func detectWord(input string) string {
var result string

for _, char := range input {
if unicode.IsLower(char) {
result += string(char)
}
}

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

func potatoes() {}
import "strings"

func potatoes(input string) int {

searchPotatoes := "potato"
count := strings.Count(input, searchPotatoes)

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

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

func emojify(sentence string) string {

emojiMap := map[string]string{
"smile": "🙂",
"grin": "😀",
"sad": "😥",
"mad": "😠",
}
words := strings.Fields(sentence)

for i, word := range words {
cleanedWord := strings.Trim(word, ".,!?")
if emoji, found := emojiMap[cleanedWord]; found {
words[i] = strings.Replace(word, cleanedWord, emoji, -1)
}
}

return strings.Join(words, " ")
}
24 changes: 23 additions & 1 deletion exercise1/problem7/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
package main

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

func highestDigit(num int) int {

numStr := strconv.Itoa(num)
maxDigit := 0

for _, char := range numStr {
digit, err := strconv.Atoi(string(char))
if err != nil {
fmt.Println("Error converting character to digit:", err)
return -1
}
if digit > maxDigit {
maxDigit = digit
}
}

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

func countVowels() {}
import "strings"

func countVowels(s string) int {

vowels := "aeiouAEIOU"
count := 0

for _, char := range s {
if strings.ContainsRune(vowels, char) {
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(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
}
7 changes: 6 additions & 1 deletion exercise2/problem1/problem1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package problem1

func isChangeEnough() {
func isChangeEnough(cash [4]int, amount float32) bool {

allCash := cash[0]*25 + cash[1]*10 + cash[2]*5 + cash[3]
amountToCent := int(amount * 100)

return allCash >= amountToCent
}
18 changes: 17 additions & 1 deletion exercise2/problem10/problem10.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
package problem10

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

brands := make(map[string]int)

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

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

return func(amount int) {
brands[brandName] += amount
}
}

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

func removeDups() {}
func removeDups[T comparable](items []T) []T {

seen := make(map[T]struct{})
result := []T{}

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

return result
}
27 changes: 26 additions & 1 deletion exercise2/problem12/problem12.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
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))
values := make([]V, 0, len(m))

for k, v := range m {
keys = append(keys, k)
values = append(values, v)
}

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

sortedValues := make([]V, len(keys))
for i, key := range keys {
sortedValues[i] = m[key]
}

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

func capitalize() {
import "strings"

func capitalize(names []string) []string {

capitalizedNames := []string{}
for _, name := range names{
capitalizedNames = append(capitalizedNames, strings.Title(strings.ToLower(name)))
}

return capitalizedNames
}
46 changes: 45 additions & 1 deletion exercise2/problem3/problem3.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,49 @@ const (
lr dir = "lr"
)

func diagonalize() {
func diagonalize(size int, direction dir) [][]int {

array := make([][]int, size)

for i := range array {
array[i] = make([]int, size)
}

switch direction {
case ul:
for i := range array {
for j := range array[i] {
array[i][j] = i + j
}
}
return array
case ur:
for i := range array {
for j := range array[i] {
array[i][j] = (size - 1 - j) + i
}
}
return array
case ll:
for i := range array {
for j := range array[i] {
array[i][j] = (size - 1 - i) + j
}
}
return array
case lr:
for i := range array {
for j := range array[i] {
array[i][j] = (size - 1 - i) + (size - 1 - j)
}
}
return array
default:
for i := range array {
for j := range array[i] {
array[i][j] = 0
}
}
return array
}
}
10 changes: 9 additions & 1 deletion exercise2/problem4/problem4.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
package problem4

func mapping() {
import "strings"

func mapping(lowerCase []string) map[string]string {
result := make(map[string]string)

for _, value := range lowerCase {
result[value] = strings.ToUpper(value)
}
return result
}
20 changes: 19 additions & 1 deletion exercise2/problem5/problem5.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
package problem5

func products() {
import "sort"

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

for product, price := range catalog {
if price >= minimalPrice {
result = append(result, product)
}
}

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
}
15 changes: 14 additions & 1 deletion exercise2/problem6/problem6.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
package problem6

func sumOfTwo() {
func sumOfTwo(a []int, b []int, v int) bool {
sumSet := make(map[int]struct{})

for _, numA := range a {
sumSet[v-numA] = struct{}{}
}

for _, numB := range b {
if _, exists := sumSet[numB]; exists {
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(x, y *int) {
*x, *y = *y, *x
}
Loading