-
Notifications
You must be signed in to change notification settings - Fork 0
Update main.py #10
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?
Update main.py #10
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): | |
|
|
||
| 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) | ||
|
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. Validate input: reject negative or non-integer n Currently, passing a negative n returns a negative value (Line 4) which is incorrect for Fibonacci. Add input validation to ensure n is a non-negative integer. Suggested replacement (outside the changed hunk): def nth_fibonacci(n):
# Validate input early
if not isinstance(n, int) or n < 0:
raise ValueError("n must be a non-negative integer")
if n <= 1:
return n
memo = [-1] * (n + 1)
return nth_fibonacci_util(n, memo)🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| n = 5 | ||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate Function RemovalThe PR removes a duplicate iterative Fibonacci implementation. Having multiple implementations of the same function with different algorithms creates maintenance issues and confusion about which one to use. Standards
|
||
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.
Redundant Function Definition
The PR removes an inefficient recursive Fibonacci implementation without memoization. This is good as it had exponential O(2^n) time complexity which would cause severe performance issues for larger inputs.
Standards