Skip to content
Open

hw #8

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
73 changes: 73 additions & 0 deletions exercises/main.go
Original file line number Diff line number Diff line change
@@ -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)
Comment on lines +29 to +30
Copy link

Choose a reason for hiding this comment

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

Не совсем. 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))
}
108 changes: 108 additions & 0 deletions exercises/main_test.go
Original file line number Diff line number Diff line change
@@ -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},
Comment on lines +26 to +28
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) {

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)
}
}
}