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

import "fmt"

func fibonacci(n, a, b int, s []int) []int {
if n > 0 {
s = append(s, a)
return fibonacci(n-1, b, a+b, s)
}
return s
}

func main() {
var n int
var s []int
fmt.Scan(&n)
fmt.Println(fibonacci(n, 0, 1, s))
}
31 changes: 31 additions & 0 deletions exercises/Fibonacci/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"reflect"
"testing"
)

func TestFibonacci(t *testing.T) {
for _, tc := range []struct {
name string
n int
a int
b int
s []int
want []int
}{
{"1", 1, 0, 1, []int{}, []int{0, 1}},
{"2", 8, 0, 1, []int{}, []int{0, 1, 1, 2, 3, 5, 8, 13, 21}},
{"3", -1, 0, 1, []int{}, []int{}},
{"4", 5, 0, 1, []int{}, []int{0, 1, 1, 2, 3}},
{"5", 0, 0, 1, []int{}, []int{0}},
{"6", 2, 0, 1, []int{}, []int{0, 1, 1}},
} {
t.Run(tc.name, func(t *testing.T) {
got := fibonacci(tc.n, tc.a, tc.b, tc.s)
if reflect.DeepEqual(got, tc.want) {
t.Errorf("got = %v, want = %v", got, tc.want)
}
})
}
}
19 changes: 19 additions & 0 deletions exercises/IsPowerOf2/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import "fmt"

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

func main() {
var n int
fmt.Scan(&n)
fmt.Println(ispowerof2(n))
}
21 changes: 21 additions & 0 deletions exercises/IsPowerOf2/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import "testing"

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 := ispowerof2(tc.n); got != tc.want {
t.Errorf("powerof2(%v) got = %v, want = %v", tc.n, got, tc.want)
}
}
}
41 changes: 41 additions & 0 deletions exercises/Maxnumber/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import "fmt"

func maxnumber(s []int, max, a int) int {
if len(s) == 1 {
return max
}
if a < len(s) {
if max < s[a] {
max = s[a]
}
return maxnumber(s, max, a+1)
}
return max
}

func maxnumber2nd(s []int, max, max2, a int) int {
if len(s) == 1 {
return max
}
if max < max2 {
max, max2 = max2, max
}
if a < len(s) {
if max < s[a] {
max2 = max
max = s[a]
} else if max2 < s[a] {
max2 = s[a]
}
return maxnumber2nd(s, max, max2, a+1)
}
return max2
}

func main() {
s := []int{1000, 1001, -100, 1}
fmt.Println(maxnumber(s, s[0], 1))
fmt.Println(maxnumber2nd(s, s[0], s[1], 2))
}
38 changes: 38 additions & 0 deletions exercises/Maxnumber/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import "testing"

func TestMaxnumber(t *testing.T) {
for _, tc := range []struct {
n []int
max int
i int
want int
}{
{[]int{1000, 1001, -100, 1}, 1000, 1, 1001},
{[]int{1, -3, 99, 675}, 1, 1, 675},
{[]int{10, 11, -100, 1}, 10, 1, 11},
} {
if max := maxnumber(tc.n, tc.max, tc.i); max != tc.want {
t.Errorf("numSum(%v) got = %v, want = %v", tc.n, max, tc.want)
}
}
}

func TestMaxnumber2nd(t *testing.T) {
for _, tc := range []struct {
n []int
max int
max2 int
i int
want int
}{
{[]int{1000, 1001, -100, 1}, 1000, 1001, 2, 1000},
{[]int{1, -3, 99, 675}, 1, -3, 2, 99},
{[]int{10, 11, -100, 1}, 10, 11, 2, 10},
} {
if max := maxnumber2nd(tc.n, tc.max, tc.max2, tc.i); max != tc.want {
t.Errorf("numSum(%v) got = %v, want = %v", tc.n, max, tc.want)
}
}
}
25 changes: 25 additions & 0 deletions exercises/Numbers/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import "fmt"

func numbers(n, a int) {
if n == 0 {
return
}
fmt.Println(a)
numbers(n-1, a+1)
}
func numbers2(n int) {
if n == 0 {
return
}
fmt.Println(n)
numbers2(n - 1)
}

func main() {
var n, a int
fmt.Scan(&n)
numbers(n, a)
numbers2(n)
}
16 changes: 16 additions & 0 deletions exercises/Sumdigits/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import "fmt"

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

func main() {
var n int
fmt.Scan(&n)
fmt.Println(sumdigits(n))
}
21 changes: 21 additions & 0 deletions exercises/Sumdigits/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import "testing"

func TestSumdigits(t *testing.T) {
for _, tc := range []struct {
n int
want int
}{
{333, 9},
{321, 6},
{21, 3},
{10000, 1},
{420, 6},
{228, 12},
} {
if got := sumdigits(tc.n); got != tc.want {
t.Errorf("numSum(%v) got = %v, want = %v", tc.n, got, tc.want)
}
}
}