diff --git a/exercises/main.go b/exercises/main.go new file mode 100644 index 0000000..fa19367 --- /dev/null +++ b/exercises/main.go @@ -0,0 +1,73 @@ +package main + +import "fmt" + +func partitions(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-- + } + if l >= r { + return l + } + s[l], s[r] = s[r], s[l] + r-- + // код писал я сам, по памяти с того что разбирали на уроке + } +} + +func fibonacci(n int) int { + if n <= 1 { + return n + } + return fibonacci(n-1) + fibonacci(n-2) + // time complexity = O(1) + // space complaxity = O(1) +} +func powerof2(n int) bool { + return (n & (n - 1)) == 0 + // time complexity = O(1) + // space complaxity = O(1) +} +func sumdigits(n int, sum int) int { + if n != 0 { + i := n % 10 + sum += i + return sumdigits(n/10, sum) + } + return sum + //time complexity = O(n) or maybe O(log n) idk + // space complaxity = O(n) +} + +func maxnum(n []int, i int, max int, max2 int) (int, int) { + if len(n) != i { + if n[i] > max { + max2 = max + max = n[i] + } + i++ + return maxnum(n, i, max, max2) + } + return max, max2 + //time complexity = O(n) + // space complaxity = O(n) +} + +func main() { + s := []int{9, 6, 1, 7, 5, 9, 6, 5, 2, 6} + i := partitions(s, 9) + fmt.Println("Output: ", i, s) + for i := 0; i <= 6; i++ { + fmt.Print(fibonacci(i), " ") + } + fmt.Println("\n", powerof2(256)) + fmt.Println(sumdigits(8999999991, 0)) + n := []int{10, 2, -3, 14} + fmt.Println(maxnum(n, 0, 0, 0)) +} diff --git a/exercises/main_test.go b/exercises/main_test.go new file mode 100644 index 0000000..eacea5d --- /dev/null +++ b/exercises/main_test.go @@ -0,0 +1,108 @@ +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 + }{ + {"case1", Input{[]int{8, 4, 1, 0, 5, 9, 3, 7, 2, 6}, 4}, 5}, + {"case2", Input{[]int{9, 6, 1, 7, 5, 9, 6, 5, 2, 6}, 9}, 4}, + {"case3", 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 := partitions(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) + } + }) + } +} + +// func Testfibonacci(t* testing.T){ +// for _, tc := range []struct{ +// n int +// want int +// }{ +// {0, 1, 1, 2, 3, 5, 8} +// } +// } +// } +func TestIspowerof2(t *testing.T) { + for _, tc := range []struct { + n int + want bool + }{ + {512, true}, + {128, true}, + {9, false}, + {3, false}, + {64, true}, + {31, false}, + } { + if got := powerof2(tc.n); got != tc.want { + t.Errorf("powerof2(%v) got = %v, want = %v", tc.n, got, tc.want) + } + } +} + +func TestSumdigits(t *testing.T) { + for _, tc := range []struct { + n int + sum int + want int + }{ + {333, 0, 9}, + {321, 0, 6}, + {21, 0, 3}, + {10000, 0, 1}, + {420, 0, 6}, + {228, 0, 12}, + } { + if got := sumdigits(tc.n, tc.sum); got != tc.want { + t.Errorf("numSum(%v) got = %v, want = %v", tc.n, got, tc.want) + } + } +} + +func TestMaxnum(t *testing.T) { + for _, tc := range []struct { + n []int + i int + max int + max2 int + wmax int + wmax2 int + }{ + {[]int{1000, 1001, -100, 1}, 0, 0, 0, 1001, 1000}, + {[]int{1, -3, 99, 675}, 0, 0, 0, 675, 99}, + {[]int{10, 11, -100, 1}, 0, 0, 0, 11, 10}, + } { + if max, max2 := maxnum(tc.n, tc.i, tc.max, tc.max2); max != tc.wmax || max2 != tc.wmax2 { + t.Errorf("numSum(%v) got = %v, %v, want = %v, %v", tc.n, max, max2, tc.wmax, tc.wmax2) + } + } +}