Skip to content

Commit

Permalink
refactor(fibonacci): clean up typos and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
eng618 committed Dec 16, 2023
1 parent c7cdf03 commit 5983dce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
8 changes: 5 additions & 3 deletions algo/fibonacci/fibonacci.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// []int{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233...}
package fibonacci

// Dynamic utilizes memoization and dynamic programming principle,
// to calculate the the fibonacci number for a given index.
// Dynamic utilizes memoization and dynamic programming principles,
// to calculate the fibonacci number for a given index.
func Dynamic() func(n int) int {
var fib func(int) int

Expand All @@ -30,6 +30,7 @@ func Dynamic() func(n int) int {
}

// BottomUp is a bottom up approach to finding the number.
// Runtime complexity O(n).
func BottomUp(n int) int {
answer := []int{0, 1}
for i := 2; i <= n; i++ {
Expand All @@ -39,7 +40,8 @@ func BottomUp(n int) int {
return answer[len(answer)-1]
}

// Basic function is simple implementation.
// Basic function is simple implementation, that uses recursion.
// Runtime complexity O(n^2).
func Basic(n int) int {
if n < 2 {
return n
Expand Down
24 changes: 16 additions & 8 deletions algo/fibonacci/fibonacci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ import (
"github.com/eng618/go-eng/algo/fibonacci"
)

func Example() {
// Fibonacci example
fibO := fibonacci.Dynamic()
fmt.Println(fibO(10))

// FibonacciBU example
fmt.Println(fibonacci.BottomUp(10))

func ExampleBasic() {
// Fib example
fmt.Println(fibonacci.Basic(10))

// Output:
// 55
}

func ExampleBottomUp() {
// FibonacciBU example
fmt.Println(fibonacci.BottomUp(10))

// Output:
// 55
}

func ExampleDynamic() {
// Fibonacci example
fibO := fibonacci.Dynamic()
fmt.Println(fibO(10))

// Output:
// 55
}

0 comments on commit 5983dce

Please sign in to comment.