-
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 1 commit
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,33 @@ def nth_fibonacci(n): | |||||||||||||||||||||||||||||
| n = 5 | ||||||||||||||||||||||||||||||
| result = nth_fibonacci(n) | ||||||||||||||||||||||||||||||
| print(result) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def nth_fibonacci(n): | ||||||||||||||||||||||||||||||
| if n <= 1: | ||||||||||||||||||||||||||||||
| return n | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # To store the curr Fibonacci number | ||||||||||||||||||||||||||||||
| curr = 0 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # To store the previous Fibonacci numbers | ||||||||||||||||||||||||||||||
| prev1 = 1 | ||||||||||||||||||||||||||||||
| prev2 = 0 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Loop to calculate Fibonacci numbers from 2 to n | ||||||||||||||||||||||||||||||
| for i in range(2, n + 1): | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Calculate the curr Fibonacci number | ||||||||||||||||||||||||||||||
| curr = prev1 + prev2 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Update prev2 to the last Fibonacci number | ||||||||||||||||||||||||||||||
| prev2 = prev1 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Update prev1 to the curr Fibonacci number | ||||||||||||||||||||||||||||||
| prev1 = curr | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return curr | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| n = 5 | ||||||||||||||||||||||||||||||
| result = nth_fibonacci(n) | ||||||||||||||||||||||||||||||
| print(result) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| 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
Uh oh!
There was an error while loading. Please reload this page.