Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions main.py
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):

Expand Down Expand Up @@ -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)
Copy link

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.

Suggested change
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.

Copy link

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.

Suggested change
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