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

Lab 1: Fibonacci #514

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions 01_fib/nicholsk/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

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

var out io.Writer = os.Stdout

func main() {
fib(7)
}

func fib(n int) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary blank line.
No need for blank lines at beginning or end of block.
Use blank lines between type, method and function definitions and within a block for logical separation.
Please fix throughout codebase.

if n < 0 {
fmt.Fprintln(out, "fib(n) doesn't accept negative integers")
return
}

if n == 0 {
fmt.Fprintln(out, n)
return
}

current, previous := 1, 0

// print first value which is always 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No really needed if you rearrange printing within your loop:

	n1, n2 := 1, 1
	for i := 0; i < n; i++ {
		fmt.Println(n1)
		n1, n2 = n2, n1+n2
	}

fmt.Fprintln(out, current)

for i := 1; i < n; i++ {

// calculate next number in sequence and print out
sum := previous + current
fmt.Fprintln(out, sum)

// update variables for next iteration
previous, current = current, sum

}

}
64 changes: 64 additions & 0 deletions 01_fib/nicholsk/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"bytes"
"strconv"
"testing"
)

func TestMainFunction(t *testing.T) {
var buf bytes.Buffer
out = &buf

main()

expected := strconv.Quote("1\n1\n2\n3\n5\n8\n13\n")
actual := strconv.Quote(buf.String())

if expected != actual {
t.Errorf("Unexpected output in main()")
}
}

func TestFib(t *testing.T) {
var buf bytes.Buffer
out = &buf

fib(4)

expected := strconv.Quote("1\n1\n2\n3\n")
actual := strconv.Quote(buf.String())

if expected != actual {
t.Errorf("Unexpected output in fib(4)")
}

}

func TestFibWithZero(t *testing.T) {
var buf bytes.Buffer
out = &buf

fib(0)

expected := strconv.Quote("0\n")
actual := strconv.Quote(buf.String())

if expected != actual {
t.Errorf("Unexpected output in fib(0)")
}
}

func TestFibWithNegative(t *testing.T) {
var buf bytes.Buffer
out = &buf

fib(-1)

expected := strconv.Quote("fib(n) doesn't accept negative integers\n")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for testing your implementation.

actual := strconv.Quote(buf.String())

if expected != actual {
t.Errorf("Unexpected output in fib(0)")
}
}