Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit 7ce13d9

Browse files
author
Holger Lösken
committed
Add task6 in go
1 parent ed8ef8f commit 7ce13d9

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/go/t6/s1.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import "strconv"
4+
5+
func Fib(number int) int {
6+
if number < 0 {
7+
return -1
8+
}
9+
10+
if number <= 1 {
11+
return number
12+
}
13+
14+
return Fib(number-1) + Fib(number-2)
15+
}
16+
17+
func Fibonacci(number int) (int, error) {
18+
result := Fib(number)
19+
s := strconv.Itoa(result)
20+
21+
if len(s) > 6 {
22+
return strconv.Atoi(s[len(s)-6:])
23+
}
24+
25+
return strconv.Atoi(s)
26+
}

src/go/t6/s1_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestFibonacci(t *testing.T) {
9+
type test struct {
10+
input int
11+
want int
12+
}
13+
14+
tests := []test{
15+
{input: 0, want: 0},
16+
{input: 1, want: 1},
17+
{input: 2, want: 1},
18+
{input: 3, want: 2},
19+
{input: 8, want: 21},
20+
{input: 38, want: 88169},
21+
}
22+
23+
for _, tc := range tests {
24+
got, err := Fibonacci(tc.input)
25+
if !reflect.DeepEqual(tc.want, got) {
26+
t.Fatalf("input: %v, expected: %v, got: %v, err: %v", tc.input, tc.want, got, err)
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)