-
Notifications
You must be signed in to change notification settings - Fork 0
Improve fibonacchi example #11
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
base: main
Are you sure you want to change the base?
Changes from all commits
af16f1d
d02da11
7487071
45ef703
af234d5
ba24b39
1009848
396c569
8554325
744f1c1
06b510c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,23 +1,9 @@ | ||||||||||||||||||
| def nth_fibonacci(n): | ||||||||||||||||||
|
|
||||||||||||||||||
| # Base case: if n is 0 or 1, return n | ||||||||||||||||||
| if n <= 1: | ||||||||||||||||||
| return n | ||||||||||||||||||
|
|
||||||||||||||||||
| # Recursive case: sum of the two preceding Fibonacci numbers | ||||||||||||||||||
| return nth_fibonacci(n - 1) + nth_fibonacci(n - 2) | ||||||||||||||||||
|
|
||||||||||||||||||
| n = 5 | ||||||||||||||||||
| result = nth_fibonacci(n) | ||||||||||||||||||
| print(result) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| # Function to calculate the nth Fibonacci number using memoization | ||||||||||||||||||
| def nth_fibonacci_util(n, memo): | ||||||||||||||||||
|
|
||||||||||||||||||
| # Base case: if n is 0 or 1, return n | ||||||||||||||||||
| if n <= 1: | ||||||||||||||||||
| return n | ||||||||||||||||||
| return m | ||||||||||||||||||
|
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. Undefined Variable ReferenceVariable 'm' is referenced but not defined, causing NameError at runtime. This will crash the application when nth_fibonacci_util is called with n <= 1.
Suggested change
Standards
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. Undefined Variable ReferenceVariable 'm' is referenced but not defined, causing NameError at runtime. This will crash the application when nth_fibonacci_util is called with n <= 1.
Suggested change
Standards
|
||||||||||||||||||
|
|
||||||||||||||||||
| # Check if the result is already in the memo table | ||||||||||||||||||
| if memo[n] != -1: | ||||||||||||||||||
|
|
@@ -30,21 +16,25 @@ def nth_fibonacci_util(n, memo): | |||||||||||||||||
| return memo[n] | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| # Wrapper function that handles both initialization | ||||||||||||||||||
| # and Fibonacci calculation | ||||||||||||||||||
| def nth_fibonacci(n): | ||||||||||||||||||
|
|
||||||||||||||||||
| # Create a memoization table and initialize with -1 | ||||||||||||||||||
| memo = [-1] * (n + 1) | ||||||||||||||||||
|
|
||||||||||||||||||
| # Call the utility function | ||||||||||||||||||
| return nth_fibonacci_util(n, memo) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||
| n = 5 | ||||||||||||||||||
| result = nth_fibonacci(n) | ||||||||||||||||||
| print(result) | ||||||||||||||||||
| """ | ||||||||||||||||||
| Calculate the nth Fibonacci number. | ||||||||||||||||||
|
|
||||||||||||||||||
| Args: | ||||||||||||||||||
| n: A non-negative integer | ||||||||||||||||||
|
|
||||||||||||||||||
| Returns: | ||||||||||||||||||
| The nth number in the Fibonacci sequence | ||||||||||||||||||
|
|
||||||||||||||||||
| Raises: | ||||||||||||||||||
| TypeError: If n is not an integer | ||||||||||||||||||
| ValueError: If n is negative | ||||||||||||||||||
| """ | ||||||||||||||||||
| if not isinstance(n, int): | ||||||||||||||||||
| raise TypeError("Input must be an integer") | ||||||||||||||||||
| if n < 0: | ||||||||||||||||||
| raise ValueError("Input must be non-negative") | ||||||||||||||||||
| return nth_fibonacci_util(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. The call to
Suggested change
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. Missing Parameter ReferenceFunction call to nth_fibonacci_util is missing the required memo parameter. This will cause TypeError at runtime when the function is called, breaking application functionality.
Suggested change
Standards
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -68,6 +58,5 @@ def nth_fibonacci(n): | |||||||||||||||||
| # Return the nth Fibonacci number | ||||||||||||||||||
| return dp[n] | ||||||||||||||||||
|
|
||||||||||||||||||
| n = 5 | ||||||||||||||||||
| result = nth_fibonacci(n) | ||||||||||||||||||
| print(result) | ||||||||||||||||||
| # Remove lines 57-59 entirely | ||||||||||||||||||
|
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. |
||||||||||||||||||
|
|
||||||||||||||||||
|
Comment on lines
+61
to
+62
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. Unused Code BlockComment indicates code removal but actual code remains. This creates confusion about what code should be removed and leaves potentially dead code in the codebase.
Suggested change
Standards
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import unittest | ||
| from main import nth_fibonacci | ||
|
|
||
| class TestFibonacci(unittest.TestCase): | ||
| def test_base_cases(self): | ||
| self.assertEqual(nth_fibonacci(0), 0) | ||
| self.assertEqual(nth_fibonacci(1), 1) | ||
|
|
||
| def test_positive_values(self): | ||
| self.assertEqual(nth_fibonacci(2), 1) | ||
| self.assertEqual(nth_fibonacci(5), 5) | ||
| self.assertEqual(nth_fibonacci(10), 55) | ||
|
|
||
| def test_input_validation(self): | ||
| with self.assertRaises(TypeError): | ||
| nth_fibonacci('5') | ||
| with self.assertRaises(ValueError): | ||
| nth_fibonacci(-1) | ||
|
Comment on lines
+14
to
+18
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. These tests for input validation are a great addition, but they will fail with the current |
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
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.
There's a typo here. The variable
mis not defined. This will cause aNameErrorat runtime. It should ben.