Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ go.work

# Students
students.txt

# VS Code
.vscode/
*.code-workspace
.history/
7 changes: 6 additions & 1 deletion exercise1/problem1/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package main

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

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

func sum(m, n string) (string, error) {
intM, err := strconv.Atoi(m)
if err != nil {
return "", fmt.Errorf("string: %s cannot be converted", m)
}

intN, err := strconv.Atoi(n)
if err != nil {
return "", fmt.Errorf("string: %s cannot be converted", n)
}

result := intM + intN

return strconv.Itoa(result), nil
}
15 changes: 14 additions & 1 deletion exercise1/problem2/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
package main

func binary() {}
import "strconv"

func binary(n int) (binaryStr string) {
if n == 0 {
return "0"
}

for n > 0 {
binaryStr = strconv.Itoa(n%2) + binaryStr
n /= 2
}

return
}
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
}
13 changes: 12 additions & 1 deletion exercise1/problem4/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
package main

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

func detectWord(word string) (detectWord string) {
for _, char := range word {
if unicode.IsLower(char) {
detectWord += string(char)
}
}
return
}
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(word string) int {
return strings.Count(word, "potato")
}
18 changes: 17 additions & 1 deletion exercise1/problem6/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
package main

func emojify() {}
import "strings"

func emojify(word string) string {
if strings.Contains(word, "smile") {
word = strings.ReplaceAll(word, "smile", "🙂")
}
if strings.Contains(word, "grin") {
word = strings.ReplaceAll(word, "grin", "😀")
}
if strings.Contains(word, "sad") {
word = strings.ReplaceAll(word, "sad", "😥")
}
if strings.Contains(word, "mad") {
word = strings.ReplaceAll(word, "mad", "😠")
}
return word
}
10 changes: 9 additions & 1 deletion exercise1/problem7/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
package main

func highestDigit() {}
func highestDigit(n int) (maxDigit int) {
for n > 0 {
if n%10 > maxDigit {
maxDigit = n % 10
}
n /= 10
}
return
}
10 changes: 9 additions & 1 deletion exercise1/problem8/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
package main

func countVowels() {}
func countVowels(word string) (count int) {
for _, r := range word {
switch r {
case 'a', 'e', 'i', 'o', 'u':
count++
}
}
return
}
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(n, m int) int {
return n & m
}

func bitwiseOR() {}
func bitwiseOR(n, m int) int {
return n | m
}

func bitwiseXOR() {}
func bitwiseXOR(n, m int) int {
return n ^ m
}
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(changes [4]int, total float32) bool {
coinValues := [4]float32{0.25, 0.10, 0.05, 0.01}
var dollars float32

for i, coin := range changes {
dollars += float32(coin) * coinValues[i]
}

return dollars >= total
}
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(string) func(int)) {
brandInfo := make(map[string]int)
return brandInfo, func(brandName string) func(int) {
brandInfo[brandName] = 0
return func(incrementValue int) {
brandInfo[brandName] += incrementValue
}
}
}
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 comparable](slice []T) []T {
result := []T{}
seen := map[T]struct{}{}
for _, value := range slice {
if _, ok := seen[value]; !ok {
seen[value] = struct{}{}
result = append(result, value)
}
}
return result
}
10 changes: 9 additions & 1 deletion exercise2/problem12/problem12.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
package problem11

func keysAndValues() {}
func keysAndValues[K comparable, V comparable](inp map[K]V) ([]K, []V) {
keys := []K{}
values := []V{}
for key, value := range inp {
keys = append(keys, key)
values = append(values, value)
}
return keys, values
}
12 changes: 11 additions & 1 deletion exercise2/problem2/problem2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
package problem2

func capitalize() {
import (
"strings"
)

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

func diagonalize() {
func diagonalize(n int, d dir) [][]int {
exp := make([][]int, n)

for i := 0; i < n; i++ {
exp[i] = make([]int, n)
for j := 0; j < n; j++ {
switch d {
case "ul":
exp[i][j] = i + j
case "ll":
exp[i][j] = n - i + j - 1
case "ur":
exp[i][j] = n + i - j - 1
case "lr":
exp[i][j] = (n - i - 1) + (n - j - 1)
}
}
}
return exp
}
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() {
import "strings"

func mapping(inp []string) map[string]string {
exp := make(map[string]string)
for _, value := range inp {
exp[value] = strings.ToUpper(value)
}
return exp
}
16 changes: 15 additions & 1 deletion exercise2/problem5/problem5.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
package problem5

func products() {
import "sort"

func products(catalog map[string]int, minPrice int) (exp []string) {
for product, price := range catalog {
if price >= minPrice {
exp = append(exp, product)
}
}
sort.Slice(exp, func(i, j int) bool {
if catalog[exp[i]] == catalog[exp[j]] {
return exp[i] < exp[j]
}
return catalog[exp[i]] > catalog[exp[j]]
})
return
}
10 changes: 9 additions & 1 deletion exercise2/problem6/problem6.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
package problem6

func sumOfTwo() {
func sumOfTwo(a, b []int, sum int) bool {
for _, i := range a {
for _, j := range b {
if i+j == sum {
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
}
13 changes: 5 additions & 8 deletions exercise2/problem8/problem8.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
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 {
(*m)[name] = i
func load(m map[string]int, students []string) {
for i, name := range students {
m[name] = i
}
}
12 changes: 11 additions & 1 deletion exercise2/problem9/problem9.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
package problem9

func factory() {}
type resultType func(...int) []int

func factory(multiple int) resultType {
return func(list ...int) []int {
result := make([]int, len(list))
for i, value := range list {
result[i] = value * multiple
}
return result
}
}
34 changes: 33 additions & 1 deletion exercise3/problem1/problem1.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
package problem1

type Queue struct{}
import "errors"

type Queue struct {
items []interface{}
}

func (q *Queue) Enqueue(item interface{}) {
q.items = append(q.items, item)
}

func (q *Queue) Dequeue() (interface{}, error) {
if q.IsEmpty() {
return nil, errors.New("Queue is empty")
}
item := q.items[0]
q.items = q.items[1:]
return item, nil
}

func (q *Queue) Peek() (interface{}, error) {
if q.IsEmpty() {
return nil, errors.New("Queue is empty")
}
return q.items[0], nil
}

func (q *Queue) Size() int {
return len(q.items)
}

func (q *Queue) IsEmpty() bool {
return len(q.items) == 0
}
Loading