Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
53 changes: 21 additions & 32 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
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
return m
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix NameError in base case: use n, not m.

Ruff correctly flags m as undefined. This will raise at runtime and break the script under the module guard.

Apply this fix:

-        return m
+        return n
📝 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
return m
return n
🧰 Tools
🪛 Ruff (0.12.2)

6-6: Undefined name m

(F821)

🤖 Prompt for AI Agents
In main.py around line 6, the base-case return uses an undefined variable `m`;
replace `m` with the correct parameter `n` so the function returns the intended
value and avoids the NameError at runtime.


Comment on lines +6 to 7
Copy link

Choose a reason for hiding this comment

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

NameError: Undefined Variable 'm' in Base Case

The base case of nth_fibonacci_util references variable 'm' which is undefined, causing a runtime NameError. This prevents the function from working for any input.

Suggested change
return m
if n <= 1:
return n
Standards
  • Python Variable Scope Rules

# Check if the result is already in the memo table
if memo[n] != -1:
Expand All @@ -30,21 +16,25 @@ 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)


if __name__ == "__main__":
n = 5
result = nth_fibonacci(n)
print(result)
"""
Calculate the nth Fibonacci number.

Args:
n: A non-negative integer

Returns:
The nth number in the Fibonacci sequence

Raises:
TypeError: If n is not an integer
ValueError: If n is negative
"""
if not isinstance(n, int):
raise TypeError("Input must be an integer")
if n < 0:
raise ValueError("Input must be non-negative")
return nth_fibonacci_util(n)
Comment on lines +33 to +37
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Incorrect helper invocation and missing memo initialization.

nth_fibonacci_util requires a memo parameter. As written, this will fail with a missing-argument TypeError.

Apply this diff:

-    return nth_fibonacci_util(n)
+    # Use memoization helper
+    memo = [-1] * (n + 1)
+    return nth_fibonacci_util(n, memo)
📝 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
if not isinstance(n, int):
raise TypeError("Input must be an integer")
if n < 0:
raise ValueError("Input must be non-negative")
return nth_fibonacci_util(n)
if not isinstance(n, int):
raise TypeError("Input must be an integer")
if n < 0:
raise ValueError("Input must be non-negative")
# Use memoization helper
memo = [-1] * (n + 1)
return nth_fibonacci_util(n, memo)
🤖 Prompt for AI Agents
In main.py around lines 33 to 37, the call to nth_fibonacci_util omits its
required memo argument causing a TypeError and there is no memo initialized; add
a memo (e.g., an empty dict) and pass it into nth_fibonacci_util (for example
create memo = {} after validating n and then return nth_fibonacci_util(n, memo))
so the helper has the memo parameter it expects.

Copy link

Choose a reason for hiding this comment

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

Missing Required Parameter in Function Call

The function nth_fibonacci_util is called with only one argument, but its definition requires two parameters (n, memo). This will cause a runtime error when the function is called.

Suggested change
return nth_fibonacci_util(n)
memo = [-1] * (n + 1)
return nth_fibonacci_util(n, memo)
Standards
  • Function Parameter Consistency




Expand All @@ -68,6 +58,5 @@ def nth_fibonacci(n):
# Return the nth Fibonacci number
return dp[n]

n = 5
result = nth_fibonacci(n)
print(result)
# Remove lines 57-59 entirely

Comment on lines +61 to +62
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Remove stray review artifact from source.

The line “Remove lines 57-59 entirely” appears to be a leftover review note and should not be in code.

Apply this diff:

-# Remove lines 57-59 entirely
📝 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
# Remove lines 57-59 entirely
🤖 Prompt for AI Agents
In main.py around lines 61-62, remove the stray review artifact line "Remove
lines 57-59 entirely" from the source; delete that exact text so the file
contains only intended code/comments and then save the file (run linter/tests to
confirm no syntax remains).

Comment on lines +61 to +62
Copy link

Choose a reason for hiding this comment

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

Commented Instruction to Remove Code Remains in Codebase

The comment indicates that lines 57-59 should be removed, but both the comment and the referenced code remain in the codebase. This creates confusion about the intended implementation.

Suggested change
# Remove lines 57-59 entirely
Standards
  • Clean Code Principles

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()