-
Notifications
You must be signed in to change notification settings - Fork 0
improve fibonacchi examples [change in title] #1
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 7 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,17 +1,3 @@ | ||||||||||||||||||||||||||||||
| 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): | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
@@ -71,3 +57,38 @@ def nth_fibonacci(n): | |||||||||||||||||||||||||||||
| n = 5 | ||||||||||||||||||||||||||||||
| result = nth_fibonacci(n) | ||||||||||||||||||||||||||||||
| print(result) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def nth_fibonacci(n): | ||||||||||||||||||||||||||||||
| if not isinstance(n, int): | ||||||||||||||||||||||||||||||
| raise TypeError("Input must be an integer") | ||||||||||||||||||||||||||||||
| if n < 0: | ||||||||||||||||||||||||||||||
| raise ValueError("Input must be non-negative") | ||||||||||||||||||||||||||||||
| if n <= 1: | ||||||||||||||||||||||||||||||
| return n | ||||||||||||||||||||||||||||||
arvi18 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
arvi18 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
arvi18 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
arvi18 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
arvi18 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
| def nth_fibonacci(n): | |
| if not isinstance(n, int): | |
| raise TypeError("Input must be an integer") | |
| if n < 0: | |
| raise ValueError("Input must be non-negative") | |
| if n <= 1: | |
| return n | |
| def nth_fibonacci(n): | |
| if not isinstance(n, int): | |
| raise TypeError("Input must be an integer") | |
| if n < 0: | |
| raise ValueError("Input must be non-negative") | |
| if n <= 1: | |
| return n |
Standards
- Design by Contract - Precondition Validation
Outdated
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.
Duplicate Function Definition
Function 'nth_fibonacci' is defined twice in the same file. The second definition at line 62 shadows the first definition, causing the first implementation to be unreachable. This creates duplicate code and execution logic.
| def nth_fibonacci(n): | |
| if not isinstance(n, int): | |
| raise TypeError("Input must be an integer") | |
| if n < 0: | |
| raise ValueError("Input must be non-negative") | |
| if n <= 1: | |
| return n | |
| # To store the curr Fibonacci number | |
| curr = 0 | |
| # Remove lines 62-94 entirely as they duplicate functionality |
Standards
- Logic-Verification-Code-Organization
- Algorithm-Correctness-Function-Definition
Outdated
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.
🛠️ Refactor suggestion
Move execution code to proper guard block.
The execution code should be moved inside the if __name__ == "__main__": guard to prevent unintended execution when the module is imported.
-n = 5
-result = nth_fibonacci(n)
-print(result)
+if __name__ == "__main__":
+ n = 5
+ result = nth_fibonacci(n)
+ print(result)📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| n = 5 | |
| result = nth_fibonacci(n) | |
| print(result) | |
| if __name__ == "__main__": | |
| n = 5 | |
| result = nth_fibonacci(n) | |
| print(result) |
🤖 Prompt for AI Agents
In main.py around lines 91 to 93, the top-level execution code (n = 5; result =
nth_fibonacci(n); print(result)) is not protected by the module guard; move
these three statements into an if __name__ == "__main__": block, indent them one
level under that guard, so the Fibonacci call and print only run when the file
is executed as a script, not when imported.
Outdated
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.
Execution Code Not Protected by Module Guard
The execution code at the end of the file is not protected by an if name == "main": guard. This means the code will execute when imported as a module, causing unintended side effects and violating Python best practices.
| n = 5 | |
| result = nth_fibonacci(n) | |
| print(result) | |
| if __name__ == "__main__": | |
| n = 5 | |
| result = nth_fibonacci(n) | |
| print(result) |
Standards
- Python Best Practices
- Module Design
arvi18 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
|
|
||
|
|
||
| # hello world | ||
arvi18 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.