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
92 changes: 92 additions & 0 deletions exercises/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package main

import (
"fmt"
"math"
)

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--
}
if l >= r {
return r + 1
}
if l == pivot {
s[l], s[l+1] = s[l+1], s[l]
pivot++
}
s[l], s[r] = s[r], s[l]

l, r = l+1, r-1
}
}

// Time complexity: O(n)
// Space complexity: O(n)
func numbers(n int) {
if n < 1 {
return
}
numbers(n - 1)
fmt.Println(n)
}

// Time complexity: O(n)
// Space complexity: O(n)
func fibonacci(n int) []int {
if n == 1 {
return []int{1}
}
if n == 2 {
return []int{1, 1}
}
a := fibonacci(n - 1)
return append(a, a[len(a)-1]+a[len(a)-2])
}

// Time complexity: O(log n)
// Space complexity: O(log n)
func isPowerOf2(n float64) bool {
if n == 2 || n == 1 {
return true
} else if n < 2 {
return false
}
return isPowerOf2(n / 2)
}

// Time complexity: O(len(n))
// Space complexity: O(len(n))
func numSum(n int) int {
if n == 0 {
return 0
}
return n%10 + numSum(n/10)
}

// Time complexity: O(len(n))
// Space complexity: O(len(n))
func maxNum(s []int) (int, int) {
if len(s) == 1 {
return s[0], math.MinInt
}
max1, max2 := maxNum(s[1:])
if max1 < s[0] {
return s[0], max1
} else if max2 < s[0] {
return max1, s[0]
}
return max1, max2

}

func main() {
fmt.Println(fibonacci(30))
}
130 changes: 130 additions & 0 deletions exercises/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package main

import (
"reflect"
"testing"
)

func ExampleNumbers1() {
numbers(10)

//Output:
//1
//2
//3
//4
//5
//6
//7
//8
//9
//10

}

func ExampleNumbers2() {
numbers(4)

//Output:
//1
//2
//3
//4
}

var fibonacciNumbers = []int{1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040}

func TestFibanachi(t *testing.T) {
for i := range fibonacciNumbers {
if got := fibonacci(i + 1); !reflect.DeepEqual(got, fibonacciNumbers[:i+1]) {
t.Errorf("TestFibanachi(%v) got %v, want %v", i, got, fibonacciNumbers[:i])
}
}
}

func TestIsPowerOf2(t *testing.T) {
for _, tc := range []struct {
input float64
want bool
}{
{1, true},
{2, true},
{3, false},
{5, false},
{6, false},
{8, true},
{12, false},
} {
if got := isPowerOf2(tc.input); got != tc.want {
t.Errorf("isPowerOf2(%v) got = %v, want = %v", tc.input, got, tc.want)
}
}
}

func TestNumSum(t *testing.T) {
for _, tc := range []struct {
input int
want int
}{
{123, 6},
{321, 6},
{3201, 6},
{3210, 6},
{67, 13},
{91, 10},
{12, 3},
} {
if got := numSum(tc.input); got != tc.want {
t.Errorf("numSum(%v) got = %v, want = %v", tc.input, got, tc.want)
}
}
}

func TestMaxNum(t *testing.T) {
for _, tc := range []struct {
input []int
wantMax1 int
wantMax2 int
}{
{[]int{1, 2, 3, 4, 5}, 5, 4},
{[]int{2, 1, 0}, 2, 1},
{[]int{2, 0, 5}, 5, 2},
} {
if max1, max2 := maxNum(tc.input); max1 != tc.wantMax1 || max2 != tc.wantMax2 {
t.Errorf("numSum(%v) got = %v, %v, want = %v, %v", tc.input, max1, max2, tc.wantMax1, tc.wantMax2)
}
}
}

func shallowEqual(a []int, b []int) bool {
m := make(map[int]int)
for _, v := range a {
m[v]++
}
for _, v := range b {
m[v]--
if m[v] == -1 {
return false
}
}
return true
}

func TestRearrange(t *testing.T) {
for _, tc := range []struct {
slice []int
pivot int
wantSlice []int
wantIndex int
}{
{[]int{8, 4, 1, 0, 5, 9, 3, 7, 2, 6}, 4, []int{2, 4, 1, 0, 3, 5, 9, 7, 8, 6}, 5},
{[]int{9, 6, 1, 7, 5, 9, 6, 5, 2, 6}, 9, []int{2, 5, 1, 5, 6, 9, 6, 7, 6, 9}, 4},
{[]int{9, 6, 1, 7, 5, 9, 6, 5, 2, 0}, 9, []int{0, 6, 1, 7, 5, 9, 6, 5, 2, 9}, 0},
} {
Comment on lines +120 to +123
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).

gotSlice := make([]int, len(tc.slice))
copy(gotSlice, tc.slice)
if gotIndex := rearrange(gotSlice, tc.pivot); !shallowEqual(gotSlice[:gotIndex], tc.wantSlice[:tc.wantIndex]) {
t.Errorf("rearange(%v, %v) = %v, %v, want %v, %v", tc.slice, tc.pivot, gotSlice, gotIndex, tc.wantSlice, tc.wantIndex)
}
}
}