-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1305c98
commit 894fd52
Showing
5 changed files
with
83 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.git | ||
.travis.yml | ||
Makefile | ||
README.md | ||
Dockerfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package internal | ||
|
||
type FibonacciNumber struct { | ||
Number int | ||
} | ||
|
||
type FibonacciNumberSequence struct { | ||
Numbers []int | ||
} | ||
|
||
type EchoError struct { | ||
Message string | ||
Code int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
package internal | ||
|
||
import ( | ||
"github.com/labstack/echo/v4" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func logManageableError(message string, code int, c echo.Context) error { | ||
func logManageableError(message error, code int, c echo.Context) error { | ||
log.Println(message) | ||
return c.String(code, message) | ||
return c.JSON(code, &EchoError{fmt.Sprintf(`%v`, message), code}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,54 @@ | ||
package fibonnaci | ||
package fibonacci | ||
|
||
func Fibonacci(n int) int { | ||
f := make([]int, n+1, n+2) | ||
import "fmt" | ||
|
||
if n < 2 { | ||
f = f[0:2] | ||
var cache = make(map[int]int) | ||
|
||
func Fibonacci(n int) (int, error) { | ||
if n < 0 { | ||
return 0, fmt.Errorf("Got '%d' but expected a number bigger than 0.", n) | ||
} | ||
|
||
f[0] = 0 | ||
f[1] = 1 | ||
// If already in cache, don't operate again | ||
v, exits := cache[n] | ||
|
||
for i := 2; i <= n; i++ { | ||
f[i] = f[i-1] + f[i-2] | ||
if exits { | ||
return v, nil | ||
} | ||
|
||
return f[n] | ||
// Equal | ||
if n < 2 { | ||
cache[n] = n | ||
return n, nil | ||
} | ||
|
||
// TODO: Use a loop instead of recursion here | ||
// I'dont check the error because I alredy know that is bigger than 0 :D | ||
vOne, _ := Fibonacci(n - 2) | ||
|
||
vTwo, _ := Fibonacci(n - 1) | ||
|
||
v = vOne + vTwo | ||
|
||
// Save to cache | ||
cache[n] = v | ||
|
||
return v, nil | ||
} | ||
|
||
func FibonacciSequence(n int) []int { | ||
var sequence []int | ||
func FibonacciSequence(length int) ([]int, error) { | ||
sequence := make([]int, length) | ||
|
||
for i := 0; i <= n; i++ { | ||
sequence = append(sequence, Fibonacci(i)) | ||
} | ||
for i := 0; i < length; i++ { | ||
fibo, err := Fibonacci(i) | ||
|
||
return sequence | ||
if err != nil { | ||
return make([]int, 0), err | ||
|
||
} | ||
|
||
sequence[i] = fibo | ||
} | ||
|
||
return sequence, nil | ||
} |