From 8d07b59dae329a18647477ca1fd900040be13bcb Mon Sep 17 00:00:00 2001 From: Vladimir Satinov Date: Wed, 28 Sep 2022 20:49:39 +0300 Subject: [PATCH] done + tests --- exercises/Fibonacci/main.go | 18 ++++++++++++++ exercises/Fibonacci/main_test.go | 31 +++++++++++++++++++++++ exercises/IsPowerOf2/main.go | 19 ++++++++++++++ exercises/IsPowerOf2/main_test.go | 21 ++++++++++++++++ exercises/Maxnumber/main.go | 41 +++++++++++++++++++++++++++++++ exercises/Maxnumber/main_test.go | 38 ++++++++++++++++++++++++++++ exercises/Numbers/main.go | 25 +++++++++++++++++++ exercises/Sumdigits/main.go | 16 ++++++++++++ exercises/Sumdigits/main_test.go | 21 ++++++++++++++++ 9 files changed, 230 insertions(+) create mode 100644 exercises/Fibonacci/main.go create mode 100644 exercises/Fibonacci/main_test.go create mode 100644 exercises/IsPowerOf2/main.go create mode 100644 exercises/IsPowerOf2/main_test.go create mode 100644 exercises/Maxnumber/main.go create mode 100644 exercises/Maxnumber/main_test.go create mode 100644 exercises/Numbers/main.go create mode 100644 exercises/Sumdigits/main.go create mode 100644 exercises/Sumdigits/main_test.go diff --git a/exercises/Fibonacci/main.go b/exercises/Fibonacci/main.go new file mode 100644 index 0000000..3114f57 --- /dev/null +++ b/exercises/Fibonacci/main.go @@ -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)) +} diff --git a/exercises/Fibonacci/main_test.go b/exercises/Fibonacci/main_test.go new file mode 100644 index 0000000..8a71b6b --- /dev/null +++ b/exercises/Fibonacci/main_test.go @@ -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) + } + }) + } +} diff --git a/exercises/IsPowerOf2/main.go b/exercises/IsPowerOf2/main.go new file mode 100644 index 0000000..2b59e8a --- /dev/null +++ b/exercises/IsPowerOf2/main.go @@ -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)) +} diff --git a/exercises/IsPowerOf2/main_test.go b/exercises/IsPowerOf2/main_test.go new file mode 100644 index 0000000..7d5d0dc --- /dev/null +++ b/exercises/IsPowerOf2/main_test.go @@ -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) + } + } +} diff --git a/exercises/Maxnumber/main.go b/exercises/Maxnumber/main.go new file mode 100644 index 0000000..f83ffaf --- /dev/null +++ b/exercises/Maxnumber/main.go @@ -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)) +} diff --git a/exercises/Maxnumber/main_test.go b/exercises/Maxnumber/main_test.go new file mode 100644 index 0000000..959d7b5 --- /dev/null +++ b/exercises/Maxnumber/main_test.go @@ -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) + } + } +} diff --git a/exercises/Numbers/main.go b/exercises/Numbers/main.go new file mode 100644 index 0000000..ee9ade8 --- /dev/null +++ b/exercises/Numbers/main.go @@ -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) +} diff --git a/exercises/Sumdigits/main.go b/exercises/Sumdigits/main.go new file mode 100644 index 0000000..3ad7c43 --- /dev/null +++ b/exercises/Sumdigits/main.go @@ -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)) +} diff --git a/exercises/Sumdigits/main_test.go b/exercises/Sumdigits/main_test.go new file mode 100644 index 0000000..6380703 --- /dev/null +++ b/exercises/Sumdigits/main_test.go @@ -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) + } + } +}