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(num int) int {
out := 0
for i := 1; i <= num; i++ {
out += i
}
return out
}
51 changes: 50 additions & 1 deletion exercise1/problem10/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,52 @@
package main

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

func sum(left, right string) (string, error) {
leftNum, err := stringToNumber(left)
if err != nil {
return "", err
}
rightNum, err := stringToNumber(right)
if err != nil {
return "", err
}
return numberToString(leftNum + rightNum), nil
}

func numberToString(num int) string {
out := make([]rune, 0)
for num > 0 {
r := rune(num%10 + '0')
out = append([]rune{r}, out...)
num = num / 10
}
return string(out)
}

func stringToNumber(s string) (int, error) {
out := 0
length := len(s)
for i, c := range s {
n := runeToNumber(c)
if n > 9 || n < 0 {
return 0, errors.New("NaN")
}
out += n * powerTen(length-i-1)
}
return out, nil
}

func runeToNumber(r rune) int {
return int(r) - '0'
}

func powerTen(pow int) int {
out := 1
for range pow {
out *= 10
}
return out
}
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() {}
func binary(num int) string {
if num == 0 {
return "0"
}
out := make([]rune, 0)
for i := 31; i >= 0; i-- {
diff := num - 1<<i
if diff < 0 && len(out) == 0 {
continue
} else if diff < 0 {
out = append(out, '0')
} else {
out = append(out, '1')
num = diff
}
}
return string(out)
}
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(num int) int {
out := 0
for i := 1; i <= num; i++ {
out += i * i
}
return out
}
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(word string) string {
out := make([]rune, 0, len(word))
for _, l := range word {
if l >= 'a' && l <= 'z' {
out = append(out, l)
}
}
return string(out)
}
18 changes: 17 additions & 1 deletion exercise1/problem5/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
package main

func potatoes() {}
func potatoes(word string) int {
buffer := []rune("potato")
count := 0
bufferInd := 0
for _, ch := range word {
if ch == buffer[bufferInd] {
bufferInd++
if bufferInd == len(buffer) {
count++
bufferInd = 0
}
} else {
bufferInd = 0
}
}
return count
}
51 changes: 50 additions & 1 deletion exercise1/problem6/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,52 @@
package main

func emojify() {}
type wordStruct struct {
word []rune
emoji string
}

func emojify(word string) string {
buffer := []wordStruct{
{word: []rune("smile"), emoji: "🙂"},
{word: []rune("grin"), emoji: "😀"},
{word: []rune("sad"), emoji: "😥"},
{word: []rune("mad"), emoji: "😠"},
}
for _, ws := range buffer {
word = replaceWord(word, &ws)
}
return word
}

func replaceWord(str string, ws *wordStruct) string {
buffer := []rune(str)
wordInd := 0
start := make([]int, 0)
tmp := -1
for i, ch := range buffer {
if ch == ws.word[wordInd] {
if wordInd == 0 {
tmp = i
}
wordInd++
if wordInd == len(ws.word) {
wordInd = 0
start = append(start, tmp)
}
} else {
wordInd = 0
}
}

if len(start) == 0 {
return str
}

out := make([]rune, 0)
for _, i := range start {
out = append(out, buffer[:i]...)
out = append(out, []rune(ws.emoji)...)
out = append(out, buffer[i+len(ws.word):]...)
}
return string(out)
}
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 {
highest := 0
for num > 0 {
rem := num % 10
if rem > highest {
highest = rem
}
num = num / 10
}
return highest
}
17 changes: 16 additions & 1 deletion exercise1/problem8/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
package main

func countVowels() {}
func countVowels(word string) int {
count := 0
for _, ch := range word {
if isVowel(ch) {
count++
}
}
return count
}
func isVowel(char rune) bool {
if char == 'a' || char == 'e' || char == 'i' || char == 'o' || char == 'u' ||
char == 'A' || char == 'E' || char == 'I' || char == 'O' || char == 'U' {
return true
}
return false
}
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, b int) int {
return a & b
}

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

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

func isChangeEnough() {
func isChangeEnough(coins [4]int, amount float32) bool {
total := int(amount * 100)
nominal := []int{25, 10, 5, 1}
for i, num := range coins {
if num == 0 {
continue
}
if needed := total / nominal[i]; num >= needed {
total -= needed * nominal[i]
} else {
total -= num * nominal[i]
}
}

return total == 0
}
10 changes: 9 additions & 1 deletion exercise2/problem10/problem10.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
package problem10

func factory() {}
func factory() (map[string]int, func(str string) func(int)) {
res := make(map[string]int)
return res, func(str string) func(int) {
res[str] = 0
return func(in int) {
res[str] += in
}
}
}
12 changes: 11 additions & 1 deletion exercise2/problem11/problem11.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
package problem11

func removeDups() {}
func removeDups[T any](array []T) []T {
out := make([]T, 0, len(array))
filter := make(map[any]struct{})
for _, val := range array {
if _, ok := filter[val]; !ok {
filter[val] = struct{}{}
out = append(out, val)
}
}
return out
}
20 changes: 19 additions & 1 deletion exercise2/problem12/problem12.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
package problem11

func keysAndValues() {}
func keysAndValues[T string | int, S any](m map[T]S) ([]T, []S) {
left := make([]T, 0, len(m))
right := make([]S, 0, len(m))
for k, v := range m {
left, right = appendWithSorting(left, right, k, v)
}
return left, right
}

func appendWithSorting[T string | int, S any](left []T, right []S, key T, val S) ([]T, []S) {
for i, v := range left {
if key <= v {
left = append(left[:i], append([]T{key}, left[i:]...)...)
right = append(right[:i], append([]S{val}, right[i:]...)...)
return left, right
}
}
return append(left, key), append(right, val)
}
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() {
func capitalize(words []string) []string {
out := make([]string, len(words))
for i, word := range words {
runeArray := make([]rune, len(word))
for j, r := range word {
if j == 0 && (r >= 'a' && r <= 'z') {
r = r - 'a' + 'A'
} else if j != 0 && (r >= 'A' && r <= 'Z') {
r = r - 'A' + 'a'
}
runeArray[j] = r
}
out[i] = string(runeArray)
}
return out
}
18 changes: 17 additions & 1 deletion exercise2/problem3/problem3.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,21 @@ const (
lr dir = "lr"
)

func diagonalize() {
func diagonalize(size int, d dir) [][]int {
out := make([][]int, size)
for i := 0; i < size; i++ {
out[i] = make([]int, size)
for j := 0; j < size; j++ {
if d == ul {
out[i][j] = i + j
} else if d == ur {
out[i][j] = size + i - j - 1
} else if d == ll {
out[i][j] = size - i + j - 1
} else {
out[i][j] = 2*size - i - j - 2
}
}
}
return out
}
9 changes: 8 additions & 1 deletion exercise2/problem4/problem4.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package problem4

func mapping() {
func mapping(words []string) map[string]string {
out := make(map[string]string)
for _, word := range words {
runes := []rune(word)
runes[0] = runes[0] - 'a' + 'A'
out[word] = string(runes)
}
return out
}
Loading