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
67 changes: 67 additions & 0 deletions exercises/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"fmt"
"math"
)

func partition(s []int, pivot int) (i int) {
l, r, p := 0, len(s)-1, s[pivot]
for {
for s[l] < p {
l++
}
for s[r] > p {
r--
}
if l >= r {
return l
}
s[l], s[r] = s[r], s[l]

}
}

func fromNtoOne(n int) {
if n <= 0 {
return
}
fromNtoOne(n - 1)
fmt.Println(n)
fromNtoOne(n - 1)
//Time / Space complexity O(n) - linear func
}
func fibonacci(n int) int {
if n <= 1 {
return n
}
return fibonacci(n-1) + fibonacci(n-2)
//Time / Space complexity O(n) - linear func
}
func isPowerOfTwo(n int) {
n = n / 2
if n == 2 {
fmt.Println("TRUE")
} else if n > 2 {
isPowerOfTwo(n)
} else {
fmt.Println("FALSE")
}
//Time / Space complexity O(n) - logarithmic func, beacuse we divide by two every iteration i suppose
}
func sumOfDigits(n int) int {
if n == 0 {
return 0
}
return n%10 + sumOfDigits(n/10)
//Time / Space complexity O(n) - logarithmic func, same as previous but i am not sure beacuse of %

}
func sliceMax(s []int) int {
if len(s) == 1 {
return s[0]
}
return int(math.Max(float64(s[0]), float64(sliceMax(s[1:]))))
//Time / Space complexity ? can even we count complexity of something thats is not really an algorithm

}
68 changes: 68 additions & 0 deletions exercises/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"testing"
)

func TestRearrange(t *testing.T) {
for _, tc := range []struct {
name string
s []int
pivot int
want int
}{
{"no repeated elements", []int{8, 4, 1, 0, 5, 9, 3, 7, 2, 6}, 4, 5},
{"repeated elements", []int{9, 6, 1, 7, 5, 9, 6, 5, 2, 6}, 9, 4},
{"empty left side", []int{9, 6, 1, 7, 5, 9, 6, 5, 2, 0}, 9, 0},
Comment on lines +14 to +16
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add more tests (not only given examples).

} {
t.Run(tc.name, func(t *testing.T) {
got := partition(tc.s, tc.pivot)
areLess(tc.s[:got], tc.s[got], t)
if got != tc.want {
t.Errorf("got = %v, want = %v", got, tc.want)
}
})
}
}

func areLess(s []int, p int, t *testing.T) {
for _, e := range s {
if e > p {
t.Errorf("%v > %v", e, p)
}
}
}

func TestSumOfDigits(t *testing.T) {
for _, tc := range []struct {
input int
want int
}{
{0, 0},
{123, 6},
{1234567890, 45},
{666, 18},
{-12, -3}, // considering this test func counts both numbers as negative -1 + -2
{-123, -6},
} {
if got := sumOfDigits(tc.input); got != tc.want {
t.Errorf("numSum(%v) got = %v, want = %v", tc.input, got, tc.want)
}
}
}
func TestSliceMax(t *testing.T) {
for _, tc := range []struct {
input []int
want int
}{
//{nil, 0}, FAIL
{[]int{1}, 1},
{[]int{1, 34, 45, -67}, 45},
{[]int{1, 1, 1, 1, 1, 1}, 1},
{[]int{-5, -8, -90, -4567}, -5},
} {
if got := sliceMax(tc.input); got != tc.want {
t.Errorf("numSum(%v) got = %v, want = %v", tc.input, got, tc.want)
}
}
}