Skip to content
Draft
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: 17 additions & 0 deletions exercises/fibonacci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

// Outputs first n fibonacci numbers
func fibonacci(n int) []int {
s := []int{0, 1}
fib(n, &s)
return s
}

func fib(n int, s *[]int) {
if len(*s) < n {
(*s) = append((*s), (*s)[len(*s)-1]+(*s)[len(*s)-2])
fib(n, s)
}
// Time complexity O(N) -- Because fib() gets called 1 time per element
// Space complexity O(N) -- Due to new slice allocation happening every time append() is called
}
26 changes: 26 additions & 0 deletions exercises/isPowerOfTwo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"math"
)

func isWhole(n float64) bool {
return n == math.Round(n)
}

func isPowerOfTwo(n int) bool {
log := math.Log2(float64(n))
Copy link

Choose a reason for hiding this comment

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

Please implement this function without using math package (you will need a recursion).

return isWhole(log)
// Time complexity O(1)
// Space complexity O(1)
}

// func main() {
// n := 16

// if isPowerOfTwo(n) {
// fmt.Println("YEAH")
// } else {
// fmt.Println("NOPE")
// }
// }
55 changes: 55 additions & 0 deletions exercises/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"reflect"
"testing"
)

func TestFibonacci(t *testing.T) {
t.Run("Fibonacci test", func(t *testing.T) {
want := []int{0, 1, 1, 2, 3, 5, 8, 13, 21, 34}
got := fibonacci(10)
if !reflect.DeepEqual(got, want) {
t.Errorf("got = %v, want = %v", got, want)
}
})
}

func TestIsPowerOfTwo(t *testing.T) {
for _, tc := range []struct {
name string
input int
want bool
}{
{"Is power of two", 8, true},
{"Not power of two", 9, false},
} {
t.Run(tc.name, func(t *testing.T) {
got := isPowerOfTwo(tc.input)
if got != tc.want {
t.Errorf("got = %v, want = %v", got, tc.want)
}
})
}
}

func TestSumDigits(t *testing.T) {
t.Run("Digit sum test", func(t *testing.T) {
want := 4
got := sumDigits(121)
if want != got {
t.Errorf("got = %v, want = %v", got, want)
}
})
}

func TestMaxNum(t *testing.T) {
t.Run("Max num test", func(t *testing.T) {
want := []int{3, 2}
input := []int{1, 2, 3}
got := maxNum(input)
if !reflect.DeepEqual(got, want) {
t.Errorf("got = %v, want = %v", got, want)
}
})
}
43 changes: 43 additions & 0 deletions exercises/maxNum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

func maxNumLoop(s []int) int {
max := s[0]
for _, el := range s {
if el > max {
max = el
}
}

return max
}

func maxNum(s []int) []int {
// Getting max num index
var maxTwo []int
max := maxN(0, 1, s)
maxTwo = append(maxTwo, s[max])

// Removing max num from array
tmp := s[:max]
s = append(tmp, s[max+1:]...)

// Getting max num one more time
max = maxN(0, 1, s)
maxTwo = append(maxTwo, s[max])

return maxTwo

// Time complexity O(N) -- ~2N times maxN will be called
// Space complexity O(N) -- ~2N times we will add new return address,
// and also tmp and s allocation, but it's constant
}
func maxN(max int, i int, s []int) int {
if s[i] > s[max] {
max = i
}
if i < len(s)-1 {
return maxN(max, i+1, s)
} else {
return max
}
}
21 changes: 21 additions & 0 deletions exercises/numbers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import "fmt"

func numbers1(n int) {

fmt.Println(n)

if n > 1 {
numbers1(n - 1)
}
}

func numbers2(n int) {

if n > 1 {
numbers2(n - 1)
}

fmt.Println(n)
}
20 changes: 20 additions & 0 deletions exercises/sumDigits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

// func sumDigitsLoop(n int) int {
// var sum int
// for ; n != 0; n /= 10 {
// sum += n % 10
// }
// return sum
// }

func sumDigits(n int) int {
if n != 0 {
return n%10 + sumDigits(n/10)
} else {
return 0
}

// Time complexity O(N) -- One function call for each digit
// Space complexity O(N) -- One return address for each digit
}