Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab1 - Generate Fibonacci Series #625

Merged
merged 14 commits into from
Sep 24, 2019
11 changes: 11 additions & 0 deletions 01_fib/jimbotech/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Fibonacci Number Generator
## Overview
As part of the ANZ "go" course the first lab is to produce a piece of software
that will produce the Fibonacci series for fib(7).
It was also an exercise in the tour.golang.org.

Generating the series was implemented as "closure" which was a requirement in
the tour-golang . There are other ways to solve this problem,
but I kept it as an example of closure for later reference.

The function maintains state and hence has to be called sequentially.
54 changes: 54 additions & 0 deletions 01_fib/jimbotech/ex_fibonacci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"fmt"
"io"
"os"
)

var out io.Writer = os.Stdout

// fibonacci returns a function that returns
// an number in the fibonacci sequence.
// This is an implmentation of "closure".
// The inside function returns the current number
// in the fibonnacci sequence, starting at 1
// and sets up the next which will be returned
// in the subsequent call
func fibonacci() func() int {
secondLast, last := 0, 1

return func() int {
result := last
secondLast, last = last, secondLast+last
return result
}
}

func fibSeries(n int) []int {
counter := n
if n < 0 {
counter = -n
}
var fibSerial []int
f := fibonacci()
for i := 0; i < counter; i++ {
factor := 1
if n < 0 && i%2 != 0 {
factor = -1
}
fibSerial = append(fibSerial, f()*factor)
}
return fibSerial
}

func fib(n int) {
fibSer := fibSeries(n)
for _, v := range fibSer {
fmt.Fprintln(out, v)
}
}

func main() {
fib(7)
}
70 changes: 70 additions & 0 deletions 01_fib/jimbotech/ex_fibonacci_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"bytes"
"testing"
)

func TestFib(t *testing.T) {

fibResults := []int{1, 1, 2, 3, 5, 8, 13, 21, 34}

f := fibonacci()

for _, val := range fibResults {
if r := f(); r != val {
t.Errorf("returned value %v does not match %v", r, val)
}
}
}

func TestMain(t *testing.T) {

want := "1\n1\n2\n3\n5\n8\n13\n"
var buf bytes.Buffer
out = &buf
main()
result := buf.String()

if result != want {
t.Errorf("expected %v, got %v", want, result)
}
}

func TestNeg(t *testing.T) {

want := "1\n-1\n2\n-3\n5\n-8\n13\n"
var buf bytes.Buffer
out = &buf
fib(-7)
result := buf.String()

if result != want {
t.Errorf("expected %v, got %v", want, result)
}
}

func TestZero(t *testing.T) {

var buf bytes.Buffer
out = &buf
fib(0)
result := buf.String()

if len(result) > 0 {
t.Errorf("expected nothing to be printed, got %v", result)
}
}

func TestOne(t *testing.T) {

want := "1\n"
var buf bytes.Buffer
out = &buf
fib(1)
result := buf.String()

if result != want {
t.Errorf("expected %v, got %v", want, result)
}
}