-
Notifications
You must be signed in to change notification settings - Fork 164
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
Closed
Lab 1: Fibonacci #514
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
057f5d1
add fibonacci main and test
krnicolson cb25a33
run goimports
krnicolson c520aca
Merge branch 'master' into lab01
krnicolson 8e1071b
address PR feedback
krnicolson bc8ad01
Merge branch 'lab01' of https://github.com/krnicolson/go-course into …
krnicolson 4604d91
Merge branch 'master' into lab01
krnicolson 9834b4b
Merge branch 'master' into lab01
krnicolson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,43 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
var out io.Writer = os.Stdout | ||
|
||
func main() { | ||
fib(7) | ||
} | ||
|
||
func fib(n int) { | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
} | ||
|
||
} |
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,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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)") | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.