From 7ce13d99a031806d3208b2b8d89e6b73dabb4b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Fri, 12 Mar 2021 00:30:02 +0100 Subject: [PATCH] Add task6 in go --- src/go/t6/s1.go | 26 ++++++++++++++++++++++++++ src/go/t6/s1_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/go/t6/s1.go create mode 100644 src/go/t6/s1_test.go diff --git a/src/go/t6/s1.go b/src/go/t6/s1.go new file mode 100644 index 0000000..103984f --- /dev/null +++ b/src/go/t6/s1.go @@ -0,0 +1,26 @@ +package main + +import "strconv" + +func Fib(number int) int { + if number < 0 { + return -1 + } + + if number <= 1 { + return number + } + + return Fib(number-1) + Fib(number-2) +} + +func Fibonacci(number int) (int, error) { + result := Fib(number) + s := strconv.Itoa(result) + + if len(s) > 6 { + return strconv.Atoi(s[len(s)-6:]) + } + + return strconv.Atoi(s) +} \ No newline at end of file diff --git a/src/go/t6/s1_test.go b/src/go/t6/s1_test.go new file mode 100644 index 0000000..8e9788e --- /dev/null +++ b/src/go/t6/s1_test.go @@ -0,0 +1,29 @@ +package main + +import ( + "reflect" + "testing" +) + +func TestFibonacci(t *testing.T) { + type test struct { + input int + want int + } + + tests := []test{ + {input: 0, want: 0}, + {input: 1, want: 1}, + {input: 2, want: 1}, + {input: 3, want: 2}, + {input: 8, want: 21}, + {input: 38, want: 88169}, + } + + for _, tc := range tests { + got, err := Fibonacci(tc.input) + if !reflect.DeepEqual(tc.want, got) { + t.Fatalf("input: %v, expected: %v, got: %v, err: %v", tc.input, tc.want, got, err) + } + } +} \ No newline at end of file