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

func addUp() {}
import "fmt"

func addUp(n int) int {
sum := 0
for i := 1; i <= n; i++ {
sum += i
}
return sum
}

func main() {
fmt.Println(addUp(4)) // 10
fmt.Println(addUp(13)) // 91
fmt.Println(addUp(600)) // 180300
}

19 changes: 18 additions & 1 deletion exercise1/problem10/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
package main

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

func sum(a string, b string) (string, error) {
num1, err1 := strconv.Atoi(a)
if err1 != nil {
return "", fmt.Errorf("string: %s cannot be converted", a)
}

num2, err2 := strconv.Atoi(b)
if err2 != nil {
return "", fmt.Errorf("string: %s cannot be converted", b)
}
result := num1 + num2
return strconv.Itoa(result), nil
}
8 changes: 7 additions & 1 deletion exercise1/problem2/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
package main

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

func binary(num int) string {
return strconv.FormatInt(int64(num), 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 {
sum := 0
for i := 1; i <= n; i++ {
sum += i * i
}
return sum
}
14 changes: 13 additions & 1 deletion exercise1/problem4/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
package main

func detectWord() {}
import (
"unicode"
)

func detectWord(crowd string) string {
var result string
for _, char := range crowd {
if unicode.IsLower(char) {
result += string(char)
}
}
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 {
if strings.Contains(str, "smile") {
str = strings.ReplaceAll(str, "smile", "🙂")
}
if strings.Contains(str, "grin") {
str = strings.ReplaceAll(str, "grin", "😀")
}
if strings.Contains(str, "sad") {
str = strings.ReplaceAll(str, "sad", "😥")
}
if strings.Contains(str, "mad") {
str = strings.ReplaceAll(str, "mad", "😠")

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

func highestDigit() {}
import "strconv"

func highestDigit(x int) int {
ans := '0'
s := strconv.Itoa(x)
for _, c := range s {
if c > ans {
ans = c
}
}
maxDigitInt, _ := strconv.Atoi(string(ans))

return maxDigitInt
}
13 changes: 12 additions & 1 deletion exercise1/problem8/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
package main

func countVowels() {}
import "strings"

func countVowels(s string) int {
count := 0
vowels := "aeiou"
for _, c := range s {
if strings.Contains(vowels, string(c)) {
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(x int, y int) int {
return x & y
}

func bitwiseOR() {}
func bitwiseOR(x int, y int) int {
return x | y
}

func bitwiseXOR() {}
func bitwiseXOR(x int, y int) int {
return x ^ y
}
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(changes [4]int, total float32) bool {

quarter := float32(changes[0]) * 0.25
dime := float32(changes[1]) * 0.10
nickel := float32(changes[2]) * 0.05
penny := float32(changes[3]) * 0.01

ChangeEnough := quarter + dime + nickel + penny

return ChangeEnough >= total
}
22 changes: 21 additions & 1 deletion exercise2/problem10/problem10.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
package problem10

func factory() {}
import "sync"

func factory() (map[string]int, func(string) func(int)) {
brands := make(map[string]int)
var mu sync.Mutex

makeBrand := func(brand string) func(int) {
mu.Lock()
if _, exists := brands[brand]; !exists {
brands[brand] = 0
}
mu.Unlock()
return func(increment int) {
mu.Lock()
brands[brand] += increment
mu.Unlock()
}
}

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 comparable](inp []T) []T {
seen := make(map[T]struct{})
var result []T

for _, v := range inp {
if _, exists := seen[v]; !exists {
seen[v] = struct{}{}
result = append(result, v)
}
}
return result
}
11 changes: 10 additions & 1 deletion exercise2/problem12/problem12.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
package problem11

func keysAndValues() {}
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)
}
return keys, values
}
15 changes: 14 additions & 1 deletion exercise2/problem2/problem2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
package problem2

func capitalize() {
import (
"strings"
)

func capitalize(names []string) []string {
Upper := make([]string, len(names))
for i, name := range names {
if len(name) > 0 {
Upper[i] = strings.ToUpper(string(name[0])) + strings.ToLower(name[1:])
} else {
Upper[i] = name
}
}
return Upper
}
21 changes: 20 additions & 1 deletion exercise2/problem3/problem3.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,24 @@ const (
lr dir = "lr"
)

func diagonalize() {
func diagonalize(n int, d dir) [][]int {
matrix := make([][]int, n)
for i := range matrix {
matrix[i] = make([]int, n)
}
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
switch d {
case ul:
matrix[i][j] = i + j
case ur:
matrix[i][j] = i + (n - 1 - j)
case ll:
matrix[i][j] = (n - 1 - i) + j
case lr:
matrix[i][j] = (n - 1 - i) + (n - 1 - j)
}
}
}
return matrix
}
12 changes: 11 additions & 1 deletion exercise2/problem4/problem4.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
package problem4

func mapping() {
import (
"strings"
)

func mapping(inp []string) map[string]string {
m := make(map[string]string)

for _, letter := range inp {
m[letter] = strings.ToUpper(letter)
}
return m
}
18 changes: 17 additions & 1 deletion exercise2/problem5/problem5.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
package problem5

func products() {
import "sort"

func products(catalog map[string]int, minPrice int) []string {
var result []string
for product, price := range catalog {
if price >= minPrice {
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
}
12 changes: 11 additions & 1 deletion exercise2/problem6/problem6.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
package problem6

func sumOfTwo() {
func sumOfTwo(a []int, b []int, sum int) bool {
complements := make(map[int]bool)
for _, numA := range a {
complements[sum-numA] = true
}
for _, numB := range b {
if complements[numB] {
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, b *int) {
*a, *b = *b, *a
}
10 changes: 4 additions & 6 deletions exercise2/problem8/problem8.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package problem8

func simplify(list []string) map[string]int {
var indMap map[string]int

indMap = make(map[string]int)
load(&indMap, &list)

indMap := make(map[string]int)
load(&indMap, list)
return indMap
}

func load(m *map[string]int, students *[]string) {
for i, name := range *students {
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(multiple int) func(...int) []int {
return func(numbers ...int) []int {
result := make([]int, len(numbers))

for i, num := range numbers {
result[i] = num * multiple
}
return result
}
}
Loading