Skip to content
Open
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
21 changes: 21 additions & 0 deletions 1_partition/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
)

func rearrange(s []int, pivot int) (i int) {
p := s[pivot]
for j := 0; j < len(s); j++ {
if s[j] < p {
s[i], s[j] = s[j], s[i]
i++
}
}
return
}

func main() {
s := []int{3, 2, 4, 6, 1}
fmt.Println(rearrange(s, 2), s)
}
41 changes: 41 additions & 0 deletions 1_partition/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"
"testing"
)

func TestRearrange(t *testing.T) {
for _, tc := range []struct {
s []int
pivot int
want int
}{
{[]int{1, 2}, 1, 1},
{[]int{3, 2, 4, 6}, 2, 2},
{[]int{3, 2, 4, 6, 1}, 2, 3},
{[]int{4, 4, 4, 4}, 2, 0},
{[]int{8, 4, 1, 0, 5, 9, 3, 7, 2, 6}, 4, 5}, // no repeated elements
{[]int{9, 6, 1, 7, 5, 9, 6, 5, 2, 6}, 9, 4}, // repeated elements
{[]int{9, 6, 1, 7, 5, 9, 6, 5, 2, 0}, 9, 0}, // empty left side
} {
t.Run(fmt.Sprint(tc.s)+"_"+fmt.Sprint(tc.pivot), func(t *testing.T) {
got := rearrange(tc.s, tc.pivot)
CompareLeftAndRight(tc.s, got)
if got != tc.want {
t.Errorf("rearrange(%v, %v) = %v, want %v", tc.s, tc.pivot, got, tc.want)
}
})
}
}

func CompareLeftAndRight(s []int, index int) error {
for _, i := range s[:index] {
for _, j := range s[index:] {
if i >= j {
panic("the returned slice is incorrect")
}
}
}
return nil
}
22 changes: 22 additions & 0 deletions 2_numbers/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import "fmt"

func Numbers(n int) ([]int, []int) {
return Num(n, nil, nil)
}

func Num(n int, ascending, descenging []int) ([]int, []int) {
if n == 0 {
return ascending, descenging
}
ascending, descenging = Num(n-1, ascending, append(descenging, n)) // (2)
return append(ascending, n), descenging // (1)
}

func main() {
fmt.Println(Numbers(4))
}

// Time complexity: O(n)
// Space complexity: O(n)
49 changes: 49 additions & 0 deletions 2_numbers/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"fmt"
"reflect"
"testing"
)

func TestNumbers(t *testing.T) {
for _, tc := range []struct {
input int
}{
{0},
{1},
{2},
{3},
{4},
{5},
{6},
{7},
{8},
{9},
{10},
{11},
{19},
{20},
{23},
{25},
{33},
{57},
{68},
{100},
{101},
{123},
{376},
{1279},
{98764},
} {
t.Run(fmt.Sprint(tc.input), func(t *testing.T) {
var wantascending, wantdescending []int
for i, j := 1, tc.input; i <= tc.input; i, j = i+1, j-1 {
wantascending, wantdescending = append(wantascending, i), append(wantdescending, j)
}
if gotascending, gotdescending := Numbers(tc.input); !reflect.DeepEqual(gotascending, wantascending) || !reflect.DeepEqual(gotdescending, wantdescending) {
t.Errorf("Numbers(%v) = %v %v, want %v %v", tc.input, gotascending, gotdescending, wantascending, wantdescending)
}
})
}
}
23 changes: 23 additions & 0 deletions 3_fibonacci/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import "fmt"

func Fibonacci(n int) []int {
return fib(n, 0, 1, nil)
}

func fib(n, a, b int, result []int) []int {
result = append(result, a)
if b > n {
return result
}
return fib(n, b, a+b, result)
}

func main() {
fmt.Println(Fibonacci(13))
fmt.Println(Fibonacci(15))
}

// Time complexity: O(n)
// Space complexity: O(n)
51 changes: 51 additions & 0 deletions 3_fibonacci/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"fmt"
"reflect"
"testing"
)

var numbers = []int{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181}

func TestFibonacci(t *testing.T) {
for _, tc := range []struct {
input int
}{
{1},
{2},
{3},
{4},
{5},
{6},
{7},
{8},
{12},
{13},
{18},
{20},
{25},
{30},
{56},
{99},
{100},
{143},
{144},
{145},
{228},
{4200},
} {
t.Run(fmt.Sprint(tc.input), func(t *testing.T) {
want := numbers
for i := range numbers {
if numbers[i] > tc.input {
want = numbers[:i]
break
}
}
if got := Fibonacci(tc.input); !reflect.DeepEqual(got, want) {
t.Errorf("Fibonacci(%v) = %v, want %v", tc.input, got, want)
}
})
}
}
22 changes: 22 additions & 0 deletions 4_is_power_of_2/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import "fmt"

func IsPowerOf2(n int) bool {
if n == 1 {
return true
}
if n%2 != 0 || n == 0 {
return false
}
return IsPowerOf2(n / 2)
}

func main() {
fmt.Println(IsPowerOf2(32))
fmt.Println(IsPowerOf2(31))
fmt.Println(IsPowerOf2(48))
}

// Time complexity: O(log(n))
// Space complexity: O(log(n))
49 changes: 49 additions & 0 deletions 4_is_power_of_2/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"fmt"
"testing"
)

func TestIsPowerOf2(t *testing.T) {
for _, tc := range []struct {
input int
want bool
}{
{0, false},
{1, true},
{-1, false},
{2, true},
{3, false},
{4, true},
{5, false},
{6, false},
{7, false},
{8, true},
{9, false},
{10, false},
{15, false},
{16, true},
{20, false},
{32, true},
{-32, false},
{64, true},
{-64, false},
{65, false},
{-65, false},
{66, false},
{-66, false},
{128, true},
{256, true},
{512, true},
{1024, true},
{1524, false},
{2048, true},
} {
t.Run(fmt.Sprint(tc.input), func(t *testing.T) {
if got := IsPowerOf2(tc.input); got != tc.want {
t.Errorf("IsPowerOf2(%v) = %v, want %v", tc.input, got, tc.want)
}
})
}
}
17 changes: 17 additions & 0 deletions 5_sum_digits/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import "fmt"

func SumDigits(n int) int {
if n == 0 {
return 0
}
return SumDigits(n/10) + n%10
}

func main() {
fmt.Println(SumDigits(561))
}

// Time complexity: O(log(n))
// Space complexity: O(log(n))
38 changes: 38 additions & 0 deletions 5_sum_digits/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"
"testing"
)

func TestSumDigits(t *testing.T) {
for _, tc := range []struct {
input int
want int
}{
{0, 0},
{1, 1},
{2, 2},
{5, 5},
{9, 9},
{10, 1},
{11, 2},
{12, 3},
{18, 9},
{19, 10},
{20, 2},
{24, 6},
{55, 10},
{89, 17},
{100, 1},
{123, 6},
{561, 12},
{345678, 33},
} {
t.Run(fmt.Sprint(tc.input), func(t *testing.T) {
if got := SumDigits(tc.input); got != tc.want {
t.Errorf("SumDigits(%v) = %v, want %v", tc.input, got, tc.want)
}
})
}
}
28 changes: 28 additions & 0 deletions 6_max_number/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import "fmt"

func MaxNumber(s []int) (int, int) {
return Max(s, -1, -1)
}

func Max(s []int, max, secondmax int) (int, int) {
if len(s) == 0 {
return max, secondmax
}
if s[0] >= max {
secondmax = max
max = s[0]
}
if s[0] > secondmax && s[0] < max {
secondmax = s[0]
}
return Max(s[1:], max, secondmax)
}

func main() {
fmt.Println(MaxNumber([]int{1, 2, 6, 9, 3, 1, 10, 15, 7, 11}))
}

// Time complexity: O(n)
// Space complexity: O(n)
Loading