From 0968f45e40c39f5b74540ffcc4bba093040f7374 Mon Sep 17 00:00:00 2001 From: Aleksandr Stukalov Date: Wed, 28 Sep 2022 22:51:34 +0300 Subject: [PATCH 1/3] Added partition implementation --- exercises/main.go | 39 ++++++++++++++++++++++++++++++++++++++ exercises/main_test.go | 43 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 exercises/main.go create mode 100644 exercises/main_test.go diff --git a/exercises/main.go b/exercises/main.go new file mode 100644 index 0000000..77cb893 --- /dev/null +++ b/exercises/main.go @@ -0,0 +1,39 @@ +package main + +import "fmt" + +func rearrange(s []int, pivot int) (i int) { + l, r := 0, len(s)-1 + p := s[pivot] + for { + for ; s[l] < p; l++ { + } + for ; s[r] >= p && r > 0; r-- { + } + if l >= r { + if r == 0 { + return r + } else { + return r + 1 + } + } + + s[l], s[r] = s[r], s[l] + + l++ + r-- + } +} + +func main() { + p := 9 + fmt.Println("Pivot:", p) + s := []int{9, 6, 1, 7, 5, 9, 6, 5, 2, 6} + fmt.Println("Initial:", s) + fmt.Println() + i := rearrange(s, p) + fmt.Println("Index:", i) + fmt.Println("Processed:", s) + fmt.Println("s[:i]:", s[:i]) + fmt.Println("s[i:]:", s[i:]) +} diff --git a/exercises/main_test.go b/exercises/main_test.go new file mode 100644 index 0000000..69b6989 --- /dev/null +++ b/exercises/main_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "testing" +) + +type Input struct { + s []int + p int +} + +func areLess(s []int, p int, t *testing.T) { + for _, el := range s { + if el > p { + t.Errorf("%v > %v", el, p) + } + } +} + +func TestRearrange(t *testing.T) { + for _, tc := range []struct { + name string + input Input + want int + }{ + {"no repeated elements", Input{[]int{8, 4, 1, 0, 5, 9, 3, 7, 2, 6}, 4}, 5}, + {"repeated elements", Input{[]int{9, 6, 1, 7, 5, 9, 6, 5, 2, 6}, 9}, 4}, + {"empty left side", Input{[]int{9, 6, 1, 7, 5, 9, 6, 5, 2, 0}, 9}, 0}, + } { + t.Run(tc.name, func(t *testing.T) { + + input := tc.input + + got := rearrange(input.s, input.p) + + areLess(input.s[:got], input.s[got], t) + + if got != tc.want { + t.Errorf("got = %v, want = %v", got, tc.want) + } + }) + } +} From 770c17113a3699c0df5201eeb1c75b4a12396b6e Mon Sep 17 00:00:00 2001 From: Aleksandr Stukalov Date: Wed, 28 Sep 2022 23:06:04 +0300 Subject: [PATCH 2/3] Added 2nd Numbers excercise --- exercises/main.go | 49 ++++++++++++++++-------------------------- exercises/main_test.go | 43 ------------------------------------ 2 files changed, 19 insertions(+), 73 deletions(-) delete mode 100644 exercises/main_test.go diff --git a/exercises/main.go b/exercises/main.go index 77cb893..894dfc5 100644 --- a/exercises/main.go +++ b/exercises/main.go @@ -2,38 +2,27 @@ package main import "fmt" -func rearrange(s []int, pivot int) (i int) { - l, r := 0, len(s)-1 - p := s[pivot] - for { - for ; s[l] < p; l++ { - } - for ; s[r] >= p && r > 0; r-- { - } - if l >= r { - if r == 0 { - return r - } else { - return r + 1 - } - } - - s[l], s[r] = s[r], s[l] - - l++ - r-- +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) + } + func main() { - p := 9 - fmt.Println("Pivot:", p) - s := []int{9, 6, 1, 7, 5, 9, 6, 5, 2, 6} - fmt.Println("Initial:", s) - fmt.Println() - i := rearrange(s, p) - fmt.Println("Index:", i) - fmt.Println("Processed:", s) - fmt.Println("s[:i]:", s[:i]) - fmt.Println("s[i:]:", s[i:]) + numbers1(10) } diff --git a/exercises/main_test.go b/exercises/main_test.go deleted file mode 100644 index 69b6989..0000000 --- a/exercises/main_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package main - -import ( - "testing" -) - -type Input struct { - s []int - p int -} - -func areLess(s []int, p int, t *testing.T) { - for _, el := range s { - if el > p { - t.Errorf("%v > %v", el, p) - } - } -} - -func TestRearrange(t *testing.T) { - for _, tc := range []struct { - name string - input Input - want int - }{ - {"no repeated elements", Input{[]int{8, 4, 1, 0, 5, 9, 3, 7, 2, 6}, 4}, 5}, - {"repeated elements", Input{[]int{9, 6, 1, 7, 5, 9, 6, 5, 2, 6}, 9}, 4}, - {"empty left side", Input{[]int{9, 6, 1, 7, 5, 9, 6, 5, 2, 0}, 9}, 0}, - } { - t.Run(tc.name, func(t *testing.T) { - - input := tc.input - - got := rearrange(input.s, input.p) - - areLess(input.s[:got], input.s[got], t) - - if got != tc.want { - t.Errorf("got = %v, want = %v", got, tc.want) - } - }) - } -} From 3ae0e189af008bd376100d009816d3698da01316 Mon Sep 17 00:00:00 2001 From: Aleksandr Stukalov Date: Fri, 30 Sep 2022 17:42:11 +0300 Subject: [PATCH 3/3] Finished tasks and added tests --- exercises/fibonacci.go | 17 ++++++++++ exercises/isPowerOfTwo.go | 26 +++++++++++++++ exercises/main_test.go | 55 +++++++++++++++++++++++++++++++ exercises/maxNum.go | 43 ++++++++++++++++++++++++ exercises/{main.go => numbers.go} | 11 ++----- exercises/sumDigits.go | 20 +++++++++++ 6 files changed, 163 insertions(+), 9 deletions(-) create mode 100644 exercises/fibonacci.go create mode 100644 exercises/isPowerOfTwo.go create mode 100644 exercises/main_test.go create mode 100644 exercises/maxNum.go rename exercises/{main.go => numbers.go} (68%) create mode 100644 exercises/sumDigits.go diff --git a/exercises/fibonacci.go b/exercises/fibonacci.go new file mode 100644 index 0000000..07a064f --- /dev/null +++ b/exercises/fibonacci.go @@ -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 +} diff --git a/exercises/isPowerOfTwo.go b/exercises/isPowerOfTwo.go new file mode 100644 index 0000000..5269c86 --- /dev/null +++ b/exercises/isPowerOfTwo.go @@ -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)) + 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") +// } +// } diff --git a/exercises/main_test.go b/exercises/main_test.go new file mode 100644 index 0000000..a877a02 --- /dev/null +++ b/exercises/main_test.go @@ -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) + } + }) +} diff --git a/exercises/maxNum.go b/exercises/maxNum.go new file mode 100644 index 0000000..7b63607 --- /dev/null +++ b/exercises/maxNum.go @@ -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 + } +} diff --git a/exercises/main.go b/exercises/numbers.go similarity index 68% rename from exercises/main.go rename to exercises/numbers.go index 894dfc5..04dbfec 100644 --- a/exercises/main.go +++ b/exercises/numbers.go @@ -7,22 +7,15 @@ func numbers1(n int) { fmt.Println(n) if n > 1 { - numbers1(n-1) + numbers1(n - 1) } - } func numbers2(n int) { if n > 1 { - numbers2(n-1) + numbers2(n - 1) } fmt.Println(n) - -} - - -func main() { - numbers1(10) } diff --git a/exercises/sumDigits.go b/exercises/sumDigits.go new file mode 100644 index 0000000..a0aa04c --- /dev/null +++ b/exercises/sumDigits.go @@ -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 +}