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

Choose a reason for hiding this comment

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

critical

This line will cause a NameError because the variable m is not defined in this scope. Based on the function's logic and the comment on the preceding line, you should be returning n here.

Suggested change
return m
return n

Comment on lines 5 to +6
Copy link

Choose a reason for hiding this comment

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

Variable Name Error

Variable 'm' is referenced but not defined, causing NameError at runtime. This will crash the program when nth_fibonacci_util is called with n <= 1.

Suggested change
if n <= 1:
return n
return m
if n <= 1:
return n
Standards
  • ISO-IEC-25010-Reliability-Fault-Tolerance
  • ISO-IEC-25010-Functional-Correctness-Appropriateness


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

Choose a reason for hiding this comment

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

critical

The nth_fibonacci_util function is called with only one argument, but it is defined to accept two: n and memo. This will result in a TypeError. You need to create the memo list and pass it to the utility function. The logic for creating the memoization table seems to have been accidentally removed.

Suggested change
return nth_fibonacci_util(n)
memo = [-1] * (n + 1)
return nth_fibonacci_util(n, memo)

Copy link

Choose a reason for hiding this comment

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

Missing Parameter Argument

Function call to nth_fibonacci_util is missing the required 'memo' parameter. This will cause TypeError at runtime, as the function expects two arguments but receives only one.

Suggested change
return nth_fibonacci_util(n)
# Create a memoization table and initialize with -1
memo = [-1] * (n + 1)
return nth_fibonacci_util(n, memo)
Standards
  • ISO-IEC-25010-Reliability-Maturity
  • ISO-IEC-25010-Functional-Correctness-Completeness

Comment on lines 19 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

Function will be overwritten by duplicate definition

This function definition at lines 19-37 will be immediately overwritten by the second definition at lines 41-59. In Python, the second definition replaces the first one entirely, making this implementation with memoization and input validation unreachable.

Consider either:

  1. Removing this first definition if the iterative approach is preferred
  2. Removing the second definition if the memoization approach with validation is preferred
  3. Renaming one of the functions if both implementations are needed
🤖 Prompt for AI Agents
In main.py around lines 19-37 (the first nth_fibonacci definition) this function
is overshadowed by a second nth_fibonacci defined at lines 41-59, making the
first implementation unreachable; to fix, either delete the first block if you
want the memoized version, delete the second block if you prefer the current
iterative/utility-based version, or rename one function (and update all call
sites) so both implementations can coexist; after changing, run tests and ensure
any helper called (e.g., nth_fibonacci_util) still exists or is removed/adjusted
accordingly.




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

Choose a reason for hiding this comment

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

medium

This comment appears to be a leftover instruction from editing. It should be removed to keep the code clean.


Comment on lines 58 to +62
Copy link

Choose a reason for hiding this comment

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

Conflicting Implementation Approaches

The code contains a comment suggesting removal of lines, but the lines remain in the code. This creates confusion about which implementation is intended and leaves dead code in the codebase.

Suggested change
# Return the nth Fibonacci number
return dp[n]
n = 5
result = nth_fibonacci(n)
print(result)
# Remove lines 57-59 entirely
# Return the nth Fibonacci number
return dp[n]
Standards
  • Clean-Code-Comments
  • Clean-Code-Dead-Code

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