Skip to content

Commit

Permalink
Add task6 in go
Browse files Browse the repository at this point in the history
  • Loading branch information
Holger Lösken committed Mar 11, 2021
1 parent ed8ef8f commit 7ce13d9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/go/t6/s1.go
Original file line number Diff line number Diff line change
@@ -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)
}
29 changes: 29 additions & 0 deletions src/go/t6/s1_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 7ce13d9

Please sign in to comment.