Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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(a int) int {
res := 0

for i:=1; i<=a; i++ {
res +=i
}
return res
}
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 "strconv"

func sum(a, b string) (string, error) {
numA, err := strconv.Atoi(a)
if err != nil {
return "", err
}

numB, err := strconv.Atoi(b)
if err != nil {
return "", err
}

res := numA + numB
str := strconv.Itoa(res)
return str, nil
}
17 changes: 16 additions & 1 deletion exercise1/problem2/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
package main

func binary() {}
import "strconv"

func binary(a int) string {
if a == 0 {
return "0"
}
res := ""

for a != 0 {
b := a % 2
c := strconv.Itoa(b)
res = c + res
a = a / 2
}
return res
}
12 changes: 11 additions & 1 deletion exercise1/problem3/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
package main

func numberSquares() {}
func numberSquares(a int) int {
res := 0

for i:=1; i<=a; i++ {
b := a - i + 1
b = b * b
res+= b
}

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

func detectWord() {}
func detectWord(a string) string {
res := ""

for _, i := range a {
if i >='a' && i <= 'z' {
res += string(i)
}
}
return res
}
17 changes: 16 additions & 1 deletion exercise1/problem5/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
package main

func potatoes() {}
func potatoes(a string) int {
res := 0
str := ""
for i := 0; i<len(a); i++ {
str += string(a[i])
if str[0] != 'p' {
str = ""
}
if str == "potato" {
res++
str = ""
}

}
return res
}
43 changes: 42 additions & 1 deletion exercise1/problem6/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
package main

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

func emojify(a string) string {
emoji := []string{"smile", "grin", "sad", "mad"}
emojires := []string{"🙂", "😀", "😥", "😠"}

arr := strings.Split(a, " ")

res := ""

for _, i := range arr {
em, tr := smileChek(i, emoji, emojires)
if tr {
res += em
} else {
res += i
}
res += " "
}
res = res[:len(res)-1]
return res
}

func smileChek(a string, b []string, c []string) (string, bool) {
char := ""
if a[len(a)-1] < 'A' || a[len(a)-1] > 'z' {
char = string(a[len(a)-1])
a = a[:len(a)-1]
}

for j, i := range b {
if i == a {
if char != "" {
return c[j]+char, true
}
return c[j], true
}
}
return "", false
}
13 changes: 12 additions & 1 deletion exercise1/problem7/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
package main

func highestDigit() {}
func highestDigit(a int) int {
res := 0

for a != 0 {
b := a%10
if b > res {
res = b
}
a = a/10
}
return res
}
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() {}
func countVowels(a string) int {
res := 0

for _, i := range a {
if vowels(i) {
res++
}
}

return res
}

func vowels(a rune) bool {
return a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u' || a == 'A' || a == 'E' || a == 'I' || a == 'O' || a == 'U'
}
96 changes: 93 additions & 3 deletions exercise1/problem9/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,97 @@
package main

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

func bitwiseOR() {}
func bitwiseAND(a int, b int) int {
joinab := ""
ares,bres := backJob(a,b)

func bitwiseXOR() {}
for i := range bres {
if bres[i] == '1' && ares[i] == '1' {
joinab += "1"
} else {
joinab += "0"
}
}

num, err := strconv.ParseInt(joinab, 2, 64)
if err != nil {
return 0
}
res := int(num)
return res

}

func bitwiseOR(a int, b int) int {
joinab := ""
ares,bres := backJob(a,b)

for i := range bres {
if bres[i] == '1' || ares[i] == '1' {
joinab += "1"
} else {
joinab += "0"
}
}
num, err := strconv.ParseInt(joinab, 2, 64)
if err != nil {
return 0
}
res := int(num)
return res
}

func bitwiseXOR(a int, b int) int {
joinab := ""
ares,bres := backJob(a,b)

for i := range bres {
if bres[i] != ares[i] {
joinab += "1"
} else {
joinab += "0"
}
}
num, err := strconv.ParseInt(joinab, 2, 64)
if err != nil {
return 0
}
res := int(num)
return res

}

func backJob(a int, b int) (string, string) {
ares := ""
count := 0
for a != 0 {
k := a % 2
c := strconv.Itoa(k)
ares = c + ares
a = a / 2
}
bres := ""
for b != 0 {
k := b % 2
c := strconv.Itoa(k)
bres = c + bres
b = b / 2
}

if len(bres) > len(ares) {
count = len(bres) - len(ares)
for i := 0; i < count; i++ {
ares = "0" + ares
}
} else {
count = len(ares) - len(bres)
for i := 0; i < count; i++ {
bres = "0" + bres
}
}
return ares, bres

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

func isChangeEnough() {
func isChangeEnough(nums [4]int, num float32) bool {

var res float32
n := [4]float32{0.25, 0.10, 0.05, 0.01}
for i := 0; i < len(nums); i++ {
count := float32(nums[i]) * n[i]
res += count
}

return res >= num
}
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
}
17 changes: 16 additions & 1 deletion exercise2/problem11/problem11.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
package problem11

func removeDups() {}
func removeDups[T comparable](input []T) []T {
var res []T
for _, i := range input {
count := 0
for _, j := range res {
if j == i {
count++
}
}
if count == 0 {
res = append(res, i)
}
}
return res
}

23 changes: 22 additions & 1 deletion exercise2/problem12/problem12.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
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.Slice(keys, func(i, j int) bool {
return fmt.Sprint(keys[i]) < fmt.Sprint(keys[j])
})

values := make([]V, 0, len(m))
for _, k := range keys {
values = append(values, m[k])
}

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

func capitalize() {
func capitalize(str []string) []string {
res := []string{}
for i := 0; i < len(str); i++ {
name := ""
if len(str[i]) > 0 {
if str[i][0] > 96 && str[i][0] < 123 {
name += string(str[i][0] - 32)
} else {
name += string(str[i][0])
}
for j := 1; j < len(str[i]); j++ {
if str[i][j] > 64 && str[i][j] < 91 {
name += string(str[i][j] + 32)
} else {
name += string(str[i][j])
}

}
}
res = append(res, name)
}
return res
}
Loading