Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 commits
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
49 changes: 35 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,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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Error Handling in TestContainers Database Type Resolution

The DatabaseImagesEnum.fromImageName method throws an IllegalArgumentException when an unknown database image name is provided, but there's no error handling in the JdbcCommonCollectE2eTest that uses this method with a hardcoded value. If the database type is changed or removed in the future, tests will fail with an uncaught exception rather than gracefully handling the error. This violates error handling best practices and could lead to difficult-to-diagnose test failures.

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


# To store the curr Fibonacci number
curr = 0
Copy link

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.

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


# 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


21 changes: 21 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest
from main import nth_fibonacci

class TestFibonacci(unittest.TestCase):
def test_base_cases(self):
self.assertEqual(nth_fibonacci(0), 0)
self.assertEqual(nth_fibonacci(1), 1)

def test_positive_values(self):
self.assertEqual(nth_fibonacci(2), 1)
self.assertEqual(nth_fibonacci(5), 5)
self.assertEqual(nth_fibonacci(10), 55)

def test_input_validation(self):
with self.assertRaises(TypeError):
nth_fibonacci('5')
with self.assertRaises(ValueError):
nth_fibonacci(-1)

if __name__ == '__main__':
unittest.main()