-
Notifications
You must be signed in to change notification settings - Fork 0
Update main.py #4
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
Changes from all commits
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,21 +1,5 @@ | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||
|
|
@@ -30,46 +14,16 @@ def nth_fibonacci_util(n, memo): | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
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 Documentation: Consider adding docstrings to both |
||||||||||||||||||||||||||||||||
| return memo[n] | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Wrapper function that handles both initialization | ||||||||||||||||||||||||||||||||
| # and Fibonacci calculation | ||||||||||||||||||||||||||||||||
| def nth_fibonacci(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. Input Validation: Consider adding input validation to handle negative values of n or non-integer inputs, which aren't currently addressed and could cause unexpected behavior. |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Create a memoization table and initialize with -1 | ||||||||||||||||||||||||||||||||
| memo = [-1] * (n + 1) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Call the utility function | ||||||||||||||||||||||||||||||||
| return nth_fibonacci_util(n, memo) | ||||||||||||||||||||||||||||||||
|
Comment on lines
19
to
24
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. 🛠️ Refactor suggestion Add input validation for edge cases. The wrapper function logic is correct, but consider adding validation for negative inputs to prevent potential issues with array creation. def nth_fibonacci(n):
+ if n < 0:
+ raise ValueError("Fibonacci number is not defined for negative integers")
# Create a memoization table and initialize with -1
memo = [-1] * (n + 1)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||||||||||||||
| n = 5 | ||||||||||||||||||||||||||||||||
|
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. Misleading Comment: The comment |
||||||||||||||||||||||||||||||||
| result = nth_fibonacci(n) | ||||||||||||||||||||||||||||||||
| print(result) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def nth_fibonacci(n): | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Handle the edge cases | ||||||||||||||||||||||||||||||||
| if n <= 1: | ||||||||||||||||||||||||||||||||
| return n | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Create a list to store Fibonacci numbers | ||||||||||||||||||||||||||||||||
| dp = [0] * (n + 1) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Initialize the first two Fibonacci numbers | ||||||||||||||||||||||||||||||||
| dp[0] = 0 | ||||||||||||||||||||||||||||||||
| dp[1] = 1 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Fill the list iteratively | ||||||||||||||||||||||||||||||||
| for i in range(2, n + 1): | ||||||||||||||||||||||||||||||||
| dp[i] = dp[i - 1] + dp[i - 2] | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Return the nth Fibonacci number | ||||||||||||||||||||||||||||||||
| return dp[n] | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| n = 5 | ||||||||||||||||||||||||||||||||
| result = nth_fibonacci(n) | ||||||||||||||||||||||||||||||||
| print(result) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| print(result) # Output: 5 | ||||||||||||||||||||||||||||||||
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.
Performance Improvement: Good implementation of memoization for the Fibonacci function, which improves the time complexity from O(2^n) to O(n). This addresses the exponential growth problem in the original implementation and prevents potential stack overflow for large inputs.