From 34296986a15b1db663d931d5ea032bcbd63b5841 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Fri, 12 Apr 2019 10:10:13 -0600 Subject: [PATCH 1/3] day 233: simple fibonacci O(n) time and O(1) space --- day233/problem.go | 13 +++++++++++++ day233/problem_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 day233/problem.go create mode 100644 day233/problem_test.go diff --git a/day233/problem.go b/day233/problem.go new file mode 100644 index 0000000..b002c65 --- /dev/null +++ b/day233/problem.go @@ -0,0 +1,13 @@ +package day233 + +// FastFibonnaci runs in O(N) time with O(1) space. +func FastFibonnaci(n uint) uint { + if n < 2 { + return n + } + first, second := uint(0), uint(1) + for i := uint(1); i < n; i++ { + first, second = second, first+second + } + return second +} diff --git a/day233/problem_test.go b/day233/problem_test.go new file mode 100644 index 0000000..40e051a --- /dev/null +++ b/day233/problem_test.go @@ -0,0 +1,32 @@ +package day233 + +import "testing" + +var testcases = []struct { + n, fib uint +}{ + {0, 0}, + {1, 1}, + {2, 1}, + {12, 144}, + {28, 317811}, + {44, 701408733}, + {87, 679891637638612258}, +} + +func TestFastFibonnaci(t *testing.T) { + t.Parallel() + for _, tc := range testcases { + if result := FastFibonnaci(tc.n); result != tc.fib { + t.Errorf("Given n=%d, expected %d, got %d", tc.n, tc.fib, result) + } + } +} + +func BenchmarkFastFibonnaci(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, tc := range testcases { + FastFibonnaci(tc.n) + } + } +} From 022432a81b2be50cb5493bfefd0578fe749b4d59 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Fri, 12 Apr 2019 10:10:40 -0600 Subject: [PATCH 2/3] add day 233 to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 05a4d52..72fdbc9 100644 --- a/README.md +++ b/README.md @@ -241,3 +241,4 @@ problems from * [Day 230](https://github.com/vaskoz/dailycodingproblem-go/issues/473) * [Day 231](https://github.com/vaskoz/dailycodingproblem-go/issues/475) * [Day 232](https://github.com/vaskoz/dailycodingproblem-go/issues/477) +* [Day 233](https://github.com/vaskoz/dailycodingproblem-go/issues/479) From ded312742ab17372f46109f376d6921e2c635245 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Fri, 12 Apr 2019 10:15:36 -0600 Subject: [PATCH 3/3] day 233: add overflow detection --- day233/problem.go | 20 +++++++++++++++++--- day233/problem_test.go | 22 ++++++++++++---------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/day233/problem.go b/day233/problem.go index b002c65..528e223 100644 --- a/day233/problem.go +++ b/day233/problem.go @@ -1,13 +1,27 @@ package day233 +import "errors" + +var errOverflow = errors.New("overflow occurred") + +// ErrOverflow represents an overflow error. +func ErrOverflow() error { + return errOverflow +} + // FastFibonnaci runs in O(N) time with O(1) space. -func FastFibonnaci(n uint) uint { +// Returns an error if overflow occurs. +func FastFibonnaci(n uint) (uint, error) { if n < 2 { - return n + return n, nil } first, second := uint(0), uint(1) + var err error for i := uint(1); i < n; i++ { first, second = second, first+second + if second < first { + return 0, errOverflow + } } - return second + return second, err } diff --git a/day233/problem_test.go b/day233/problem_test.go index 40e051a..9d05d0f 100644 --- a/day233/problem_test.go +++ b/day233/problem_test.go @@ -4,21 +4,23 @@ import "testing" var testcases = []struct { n, fib uint + err error }{ - {0, 0}, - {1, 1}, - {2, 1}, - {12, 144}, - {28, 317811}, - {44, 701408733}, - {87, 679891637638612258}, + {0, 0, nil}, + {1, 1, nil}, + {2, 1, nil}, + {12, 144, nil}, + {28, 317811, nil}, + {44, 701408733, nil}, + {87, 679891637638612258, nil}, + {100, 0, ErrOverflow()}, } func TestFastFibonnaci(t *testing.T) { t.Parallel() for _, tc := range testcases { - if result := FastFibonnaci(tc.n); result != tc.fib { - t.Errorf("Given n=%d, expected %d, got %d", tc.n, tc.fib, result) + if result, err := FastFibonnaci(tc.n); result != tc.fib || err != tc.err { + t.Errorf("Given n=%d, expected (%d,%v), got (%d,%v)", tc.n, tc.fib, tc.err, result, err) } } } @@ -26,7 +28,7 @@ func TestFastFibonnaci(t *testing.T) { func BenchmarkFastFibonnaci(b *testing.B) { for i := 0; i < b.N; i++ { for _, tc := range testcases { - FastFibonnaci(tc.n) + FastFibonnaci(tc.n) //nolint } } }