From 2095be419b773e4cc9769730faab44c6cb5d1ba8 Mon Sep 17 00:00:00 2001 From: vay4ie <75880193+vay4ie@users.noreply.github.com> Date: Sat, 24 Sep 2022 20:06:44 +0300 Subject: [PATCH 1/2] Partition Homework --- partition.go | 33 +++++++++++++++++++++++++++++++++ partition_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 partition.go create mode 100644 partition_test.go diff --git a/partition.go b/partition.go new file mode 100644 index 0000000..62d3e3e --- /dev/null +++ b/partition.go @@ -0,0 +1,33 @@ +package main + +import "fmt" + +func main() { + s := []int{9, 6, 1, 7, 5, 9, 6, 5, 2, 0} + fmt.Println("Initial s: ", s) + i := partition(s, 9) + fmt.Println("I: ", i) + fmt.Println("Recieved s: ", s) + fmt.Println("Left side: ", s[:i]) // < i + fmt.Println("Right side: ", s[i:]) // other +} + +func partition(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 { // 3rd works with validation of r on 0 + r-- + } + if l >= r && r > 0 { + return r + 1 + } else if l >= r && r <= 0 { + return r + } + s[l], s[r] = s[r], s[l] + + } +} diff --git a/partition_test.go b/partition_test.go new file mode 100644 index 0000000..fc2f941 --- /dev/null +++ b/partition_test.go @@ -0,0 +1,44 @@ +package main + +import "testing" + +func TestPartition(t *testing.T) { + for _, tc := range []tests{ + {"#1: no repeated elements", Input{[]int{8, 4, 1, 0, 5, 9, 3, 7, 2, 6}, 4}, 5}, + {"#2: repeated elements", Input{[]int{9, 6, 1, 7, 5, 9, 6, 5, 2, 6}, 9}, 4}, + {"#3: 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 := partition(input.s, input.p) + + check(input.s[:got], input.s[got], t) + + if got != tc.want { + t.Errorf("got = %v, want = %v", got, tc.want) + } + }) + } +} + +func check(s []int, p int, t *testing.T) { + //checking if each el in left side is smaller then pivot + for _, elem := range s { + if elem > p { + t.Errorf("%v > %v", elem, p) + } + } +} + +type tests struct { + name string + input Input + want int +} + +type Input struct { + s []int + p int +} From 828d07e96aeda45e70e1b41f4f8c10a40864c7db Mon Sep 17 00:00:00 2001 From: vay4ie <75880193+vay4ie@users.noreply.github.com> Date: Sat, 1 Oct 2022 15:29:52 +0300 Subject: [PATCH 2/2] Recursion Homework --- fibonacci.go | 23 +++++++++++++ isPowerOf2.go | 21 ++++++++++++ main_test.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ maxNumber.go | 49 +++++++++++++++++++++++++++ numbers.go | 21 ++++++++++++ sumDigits.go | 26 +++++++++++++++ 6 files changed, 231 insertions(+) create mode 100644 fibonacci.go create mode 100644 isPowerOf2.go create mode 100644 main_test.go create mode 100644 maxNumber.go create mode 100644 numbers.go create mode 100644 sumDigits.go diff --git a/fibonacci.go b/fibonacci.go new file mode 100644 index 0000000..db36648 --- /dev/null +++ b/fibonacci.go @@ -0,0 +1,23 @@ +package main + +import "fmt" + +func main23() { + fibonacci(0) +} + +//my interpritation of fibonacci using recursion +func fibonacci(n int) { + var n1, n2 = 1, 0 + fibo(n1, n2, n) +} + +func fibo(n1, n2, n int) { + if n2 <= n { + fmt.Println(n2) + fibo(n1+n2, n1, n) + } +} + +//Time complexity: O(n) - calling recursion till we will not reach value of N +//Space complexity: O(1) - creating 2 variables at the start and then creating 3 input variables for each recursive function diff --git a/isPowerOf2.go b/isPowerOf2.go new file mode 100644 index 0000000..d595fe8 --- /dev/null +++ b/isPowerOf2.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "math" +) + +func main() { + fmt.Println(isPowerOf2(1)) +} +func isPowerOf2(n int) bool { + if n < 2 { + return false + } + logN := math.Log2(float64(n)) //taking logarithm with base of 2 from N + return logN == math.Round(logN) //if number is integer (has no leftover) + +} + +//Time complexity: O(1) - no loops, no recursion, one calculation +//Space complexity: O(1) - only input variable and logN variable diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..0f71c59 --- /dev/null +++ b/main_test.go @@ -0,0 +1,91 @@ +package main + +import "fmt" + +func ExampleAllNum() { + allNum(1, 10) + + //Output: + //1 + //2 + //3 + //4 + //5 + //6 + //7 + //8 + //9 + //10 + //10 + //9 + //8 + //7 + //6 + //5 + //4 + //3 + //2 + //1 +} + +func ExampleFibonacci() { + fibonacci(10) + fibonacci(1) + fibonacci(0) + + //Output: + //0 + //1 + //1 + //2 + //3 + //5 + //8 + //0 + //1 + //1 + //0 +} + +func ExamplePowerOf2() { + fmt.Println(isPowerOf2(256)) + fmt.Println(isPowerOf2(5)) + fmt.Println(isPowerOf2(65536)) + fmt.Println(isPowerOf2(0)) + fmt.Println(isPowerOf2(1)) + + //Output: + //true + //false + //true + //false + //false +} + +func ExampleSumDigits() { + fmt.Println(sumDigits(343)) + fmt.Println(sumDigits(11)) + fmt.Println(sumDigits(1)) + fmt.Println(sumDigits(0)) + fmt.Println(sumDigits(1000)) + + //Output: + //10 + //2 + //1 + //0 + //1 +} + +func ExampleMaxNumber() { + maxNumber([]int{7, 1, 2, 3, 5, 4}) + maxNumber([]int{0}) + maxNumber([]int{0, 1}) + maxNumber([]int{-100, -50, -200}) + + //Output: + //First max number: 7 Second max number: 5 + //First max number: 0 + //First max number: 1 Second max number: 0 + //First max number: -50 Second max number: -100 +} diff --git a/maxNumber.go b/maxNumber.go new file mode 100644 index 0000000..91481a9 --- /dev/null +++ b/maxNumber.go @@ -0,0 +1,49 @@ +package main + +import "fmt" + +func main2() { + s := []int{ /*7, 1, 2, 3, 5, 4*/ 0, 1} + maxNumber(s) +} + +// Time Complexity: O(N^2) - Worst case +// Space Complexity: O(logN) + + +func maxNumber(s []int) { + if len(s)-1 > 0 { + quickSort(s) // programmers must be lazy ;) + fmt.Println("First max number:", s[len(s)-1], "Second max number:", s[len(s)-2]) + } else { + fmt.Println("First max number:", s[len(s)-1]) + } +} + +func quickSort(s []int) { + if len(s) < 2 { + return + } + p := partition(s) + 1 + quickSort(s[:p]) + quickSort(s[p:]) +} + +func partition(s []int) (i int) { + l, r := 0, len(s)-1 + p := s[r/2] + for { + for s[l] < p { + l++ + } + for s[r] > p { + r-- + } + if l >= r { + return l + } + s[l], s[r] = s[r], s[l] + //l++ + r-- + } +} diff --git a/numbers.go b/numbers.go new file mode 100644 index 0000000..fe5ee03 --- /dev/null +++ b/numbers.go @@ -0,0 +1,21 @@ +package main + +import "fmt" + +func allNum(i, n int) { + fmt.Println(i) + if i < n { + allNum(i+1, n) + } + if i > 0 { + fmt.Println(i) + i-- + } +} + +// func allNumOld(n int) { +// if n > 0 { +// fmt.Println(n) +// allNumOld(n - 1) +// } +// } diff --git a/sumDigits.go b/sumDigits.go new file mode 100644 index 0000000..e502295 --- /dev/null +++ b/sumDigits.go @@ -0,0 +1,26 @@ +package main + +import "fmt" + +func main44() { + fmt.Println(sumDigits(1)) +} + +func sumDigits(n int) int { + var sum, remainder int + return sDigits(n, sum, remainder) +} + +func sDigits(n, sum, remainder int) int { + if n > 0 { + remainder = n % 10 + n /= 10 + sum += remainder + return sDigits(n, sum, remainder) + } else { + return sum + } +} + +//Time complexity: O(n) +//Space complexity: O(n)