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 {
sum := 0
for i := 1; i <= n; i++ {
sum += i
}
return sum
}
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, b string) (string, error) {
num1, err := strconv.Atoi(a)
if err != nil {
return "", fmt.Errorf("string: %s cannot be converted", a)
}
num2, err := strconv.Atoi(b)
if err != nil {
return "", fmt.Errorf("string: %s cannot be converted", b)
}

res := num1 + num2
return strconv.Itoa(res), 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 (
"fmt"
"strconv"
)

func binary(a string) (string, error) {
num, err := strconv.Atoi(a)
if err != nil {
return "", fmt.Errorf("invalid decimal number: %s", a)
}

binary := strconv.FormatInt(int64(num), 2)
return binary, nil
}
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 {
total := 0
for i := 1; i <= n; i++ {
total += (n - i + 1) * (n - i + 1)
}
return total
}
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(word string) string {
var result []rune
for _, ch := range word {
if unicode.IsLower(ch) {
result = append(result, ch)
}
}
return string(result)
}
8 changes: 7 additions & 1 deletion exercise1/problem5/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
package main

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

func potatoes(word string) int {
return strings.Count(word, "potato")
}
17 changes: 16 additions & 1 deletion exercise1/problem6/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
package main

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

func emojify(res string) string {
replacer := map[string]string{
"smile": "🙂",
"grin": "😀",
"sad": "😥",
"mad": "😠",
}
for word, emoji := range replacer {
res = strings.ReplaceAll(res, word, emoji)
}
return res
}
17 changes: 16 additions & 1 deletion exercise1/problem7/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
package main

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

func highestDigit(num int) int {
numToStr := strconv.Itoa(num)
highest := 0

for _, val := range numToStr {
digit, _ := strconv.Atoi(string(val))
if digit > highest {
highest = digit
}
}
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() {}
import (
"strings"
)

func countVowels(str string) int {
str = strings.ToLower(str)
vowels := "aoiue"
count := 0

for _, ch := range str {
if strings.ContainsRune(vowels, ch) {
count++
}
}
return count
}
17 changes: 14 additions & 3 deletions exercise1/problem9/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package main

func bitwiseAND() {}
import "fmt"

func bitwiseOR() {}
func bitwiseAND(a, b int) string {
res := a & b
return fmt.Sprintf("%08b", res)
}

func bitwiseXOR() {}
func bitwiseOR(a, b int) string {
res := a | b
return fmt.Sprintf("%08b", res)
}

func bitwiseXOR(a, b int) string {
res := a ^ b
return fmt.Sprintf("%08b", res)
}
9 changes: 8 additions & 1 deletion exercise2/problem1/problem1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package problem1

func isChangeEnough() {
func IsChangeEnough(change []int, amount float64) bool {
coinValues := []float64{0.25, 0.10, 0.05, 0.01}

totalChange := 0.0
for i := 0; i < len(change); i++ {
totalChange += float64(change[i]) * coinValues[i]
}
return totalChange >= amount
}
12 changes: 11 additions & 1 deletion exercise2/problem10/problem10.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
package problem10

func factory() {}
func factory() (map[string]int, func(string) func(int)) {
brands := make(map[string]int)

makeB := func(brand string) func(int) {
return func(count int) {
brands[brand] += count
}
}

return brands, makeB
}
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](items []T) []T {
seen := make(map[T]bool)
var result []T

for _, item := range items {
if !seen[item] {
seen[item] = true
result = append(result, item)
}
}
return result
}
2 changes: 2 additions & 0 deletions exercise2/problem12/problem12.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package problem11

func keysAndValues() {}

//idk((
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 {
result := make([]string, len(names))

for i, name := range names {
result[i] = strings.Title(strings.ToLower(name))
}
return result
}
16 changes: 15 additions & 1 deletion exercise2/problem4/problem4.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
package problem4

func mapping() {
import (
"fmt"
"strings"
)

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

for _, s := range lowerCase {
result[s] = strings.ToUpper(s)
}
return result
}
func main() {
fmt.Println(mapping([]string{"p", "s"}))
}
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(prices map[string]int, minimum int) []string {
var result []string

for product, price := range prices {
if price >= minimum {
result = append(result, product)
}
}

sort.Slice(result, func(i, j int) bool {
return prices[result[i]] < prices[result[j]] ||
(prices[result[i]] == prices[result[j]] && result[i] < result[j])
})

return result
}
17 changes: 16 additions & 1 deletion exercise2/problem6/problem6.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
package problem6

func sumOfTwo() {
func sumOfTwo(a []int, b []int, target int) bool {

bMap := make(map[int]struct{})

for _, num := range b {
bMap[num] = struct{}{}
}

for _, num := range a {
complement := target - num
if _, exists := bMap[complement]; exists {
return true
}
}

return false
}
5 changes: 4 additions & 1 deletion exercise2/problem7/problem7.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package problem7

func swap() {
func swap(x *int, y *int) {
temp := *x
*x = *y
*y = temp
}
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
}
}
10 changes: 9 additions & 1 deletion exercise2/problem9/problem9.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
package problem9

func factory() {}
func factory(multiplier int) func(...int) []int {
return func(nums ...int) []int {
result := make([]int, len(nums))
for i, num := range nums {
result[i] = num * multiplier
}
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 {
elements []interface{}
}

func (q *Queue) Enqueue(element interface{}) {
q.elements = append(q.elements, element)
}

func (q Queue) IsEmpty() bool {
return len(q.elements) == 0
}

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

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

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