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
2 changes: 1 addition & 1 deletion exercise1/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Exercise 1

Please provide solution for the following problems * are mandatory
Please provide solution for the following problems * is mandatory

1. [problem1](./problem1/README.md) *
2. [problem2](./problem2/README.md) *
Expand Down
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(a int) int {
sum := 0
for i := 0; i <= a; 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) {
numA, errA := strconv.Atoi(a)
if errA != nil {
return "", fmt.Errorf("string: %s cannot be converted", a)
}
numB, errB := strconv.Atoi(b)
if errB != nil {
return "", fmt.Errorf("string: %s cannot be converted", b)
}
sum := numA + numB

return strconv.Itoa(sum), nil
}
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() {}
import "fmt"

func binary(a int) string {
bi := ""
if a == 0 {
return "0"
}
for a > 0 {
bit := a % 2
bi += fmt.Sprintf("%d", bit)
a /= 2
}
runedbi := []rune(bi)
for i := 0; i < len(runedbi)/2; i++ {
runedbi[i], runedbi[len(runedbi)-1-i] = runedbi[len(runedbi)-1-i], runedbi[i]
}
return string(runedbi)
}
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 {
squares := 0
for i := 1; i <= n; i++ {
squares += (n - i + 1) * (n - i + 1)
}
return squares
}
12 changes: 11 additions & 1 deletion exercise1/problem4/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
package main

func detectWord() {}
import "unicode"

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

func emojify() {}
import "strings"

func emojify(a string) string {
slovavsmile := map[string]string{
"smile": "🙂",
"grin": "😀",
"sad": "😥",
"mad": "😠",
}
for i, slovo := range slovavsmile {
a = strings.ReplaceAll(a, i, slovo)
}

return a
}
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 {
max := 0
m := 0
for a > 0 {
m = a % 10
if m > max {
max = m
}
a /= 10
}
return max
}
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(a string) int {
vowels := "aeiouAEIOU"
sum := 0
for _, ch := range a {
if strings.ContainsRune(vowels, ch) {
sum++
}
}
return sum
}
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
}
15 changes: 14 additions & 1 deletion exercise2/problem1/problem1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
package problem1

func isChangeEnough() {
func isChangeEnough(change [4]int, amountDue float32) bool {
quartersValue := 25
dimesValue := 10
nickelsValue := 5
pennieValue := 1

totalCents := (change[0] * quartersValue) +
(change[1] * dimesValue) +
(change[2] * nickelsValue) +
(change[3] * pennieValue)

amountDueCents := int(amountDue * 100)

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

func factory() {}
func factory() (map[string]int, func(string) func(int)) {
brands := make(map[string]int)
makeBrand := func(brand string) func(int) {
brands[brand] = 0
return func(count int) {
brands[brand] += count
}
}
return brands, makeBrand
}
14 changes: 13 additions & 1 deletion exercise2/problem11/problem11.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
package problem11

func removeDups() {}
func removeDups[T comparable](items []T) []T {
seen := make(map[T]bool)
result := []T{}

for _, item := range items {
if !seen[item] {
seen[item] = true
result = append(result, item)
}
}

return result
}
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))
values := make([]V, 0, len(m))
for k, v := range m {
keys = append(keys, k)
values = append(values, v)
}
sort.Slice(keys, func(i, j int) bool {
return fmt.Sprintf("%v", keys[i]) < fmt.Sprintf("%v", keys[j])
})
sortedValues := make([]V, len(keys))
for i, k := range keys {
sortedValues[i] = m[k]
}

return keys, sortedValues
}
8 changes: 7 additions & 1 deletion exercise2/problem2/problem2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
package problem2

func capitalize() {
import "strings"

func capitalize(names []string) []string {
for i, name := range names {
names[i] = strings.Title(strings.ToLower(name))
}
return names
}
35 changes: 34 additions & 1 deletion exercise2/problem3/problem3.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,38 @@ const (
lr dir = "lr"
)

func diagonalize() {
func diagonalize(n int, direction dir) [][]int {
matrix := make([][]int, n)
for i := range matrix {
matrix[i] = make([]int, n)
}

switch direction {
case ul:
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
matrix[i][j] = i + j
}
}
case ur:
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
matrix[i][j] = (n - 1 - j) + i
}
}
case ll:
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
matrix[i][j] = (n - 1 - i) + j
}
}
case lr:
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
matrix[i][j] = (n - 1 - i) + (n - 1 - j)
}
}
}

return matrix
}
7 changes: 6 additions & 1 deletion exercise2/problem4/problem4.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package problem4

func mapping() {
func mapping(letters []string) map[string]string {
result := make(map[string]string)
for _, letter := range letters {
result[letter] = string(letter[0] - 32)
}
return result
}
21 changes: 20 additions & 1 deletion exercise2/problem5/problem5.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
package problem5

func products() {
import "sort"

func products(prices map[string]int, minPrice int) []string {

var result []string

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

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

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

func sumOfTwo() {
func sumOfTwo(a []int, b []int, target int) bool {
complements := make(map[int]bool)
for _, num := range a {
complements[target-num] = true
}
for _, num := range b {
if complements[num] {
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
}
5 changes: 1 addition & 4 deletions exercise2/problem8/problem8.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package problem8

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

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

return indMap
}

Expand Down
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(factor int) func(...int) []int {
return func(nums ...int) []int {
results := make([]int, len(nums))
for i, num := range nums {
results[i] = num * factor
}
return results
}
}
Loading