From af16f1dc647b7c25e3756a47ab3ea7dd4a35c3e3 Mon Sep 17 00:00:00 2001 From: frodo-repo <134262496+frodo-repo@users.noreply.github.com> Date: Thu, 20 Feb 2025 14:04:01 +0530 Subject: [PATCH 01/11] Update main.py --- main.py | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 9c5fb5d..30a2205 100644 --- a/main.py +++ b/main.py @@ -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) From d02da11b16647e9a6ffc2fd80726c770a83852f0 Mon Sep 17 00:00:00 2001 From: frodo-repo <134262496+frodo-repo@users.noreply.github.com> Date: Thu, 20 Feb 2025 14:44:07 +0530 Subject: [PATCH 02/11] Create test.py --- test.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 0000000..2be7c65 --- /dev/null +++ b/test.py @@ -0,0 +1 @@ +# hello world From 7487071770fbd35dcc3d8d465f85a729bc1ad608 Mon Sep 17 00:00:00 2001 From: frodo-repo <134262496+frodo-repo@users.noreply.github.com> Date: Thu, 20 Feb 2025 23:12:40 +0530 Subject: [PATCH 03/11] Update main.py From 45ef7032d24a0034dac74d695d9be4f16b045e5e Mon Sep 17 00:00:00 2001 From: frodo-repo <134262496+frodo-repo@users.noreply.github.com> Date: Thu, 20 Feb 2025 23:16:14 +0530 Subject: [PATCH 04/11] Update main.py --- main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/main.py b/main.py index 30a2205..9df2350 100644 --- a/main.py +++ b/main.py @@ -87,3 +87,4 @@ def nth_fibonacci(n): n = 5 result = nth_fibonacci(n) print(result) + From af234d54dbcfd368eefb5ebaa605f9ac52b45329 Mon Sep 17 00:00:00 2001 From: frodo-repo <134262496+frodo-repo@users.noreply.github.com> Date: Thu, 20 Feb 2025 23:21:08 +0530 Subject: [PATCH 05/11] Update test.py --- test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test.py b/test.py index 2be7c65..2e8edf5 100644 --- a/test.py +++ b/test.py @@ -1 +1,2 @@ + # hello world From ba24b394cff8aee1e6834cd2809bdc8046a41030 Mon Sep 17 00:00:00 2001 From: frodo-repo <134262496+frodo-repo@users.noreply.github.com> Date: Mon, 24 Feb 2025 13:30:59 +0530 Subject: [PATCH 06/11] Update test.py --- test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test.py b/test.py index 2e8edf5..b0bf757 100644 --- a/test.py +++ b/test.py @@ -1,2 +1,3 @@ + # hello world From 1009848f5659d560838313948a78a12bf3bde91c Mon Sep 17 00:00:00 2001 From: Arvind Shelke Date: Wed, 6 Aug 2025 14:33:37 +0530 Subject: [PATCH 07/11] Update main.py Co-authored-by: qodo-merge-pro[bot] <151058649+qodo-merge-pro[bot]@users.noreply.github.com> --- main.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main.py b/main.py index 9df2350..c27e16e 100644 --- a/main.py +++ b/main.py @@ -60,6 +60,10 @@ def nth_fibonacci(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 From 396c569bc4c9365d6bfd686cc8f3e6ffcaaa403e Mon Sep 17 00:00:00 2001 From: Arvind Shelke Date: Tue, 12 Aug 2025 11:59:58 +0530 Subject: [PATCH 08/11] Update test.py Co-authored-by: refacto-test[bot] <217047508+refacto-test[bot]@users.noreply.github.com> --- test.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/test.py b/test.py index b0bf757..10977f2 100644 --- a/test.py +++ b/test.py @@ -1,3 +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) -# hello world + 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() From 8554325c75bd8a251c4ac92c28f0f539b89dd8b5 Mon Sep 17 00:00:00 2001 From: Arvind Shelke Date: Tue, 12 Aug 2025 14:07:29 +0530 Subject: [PATCH 09/11] Update main.py Co-authored-by: refacto-test[bot] <217047508+refacto-test[bot]@users.noreply.github.com> --- main.py | 38 +------------------------------------- 1 file changed, 1 insertion(+), 37 deletions(-) diff --git a/main.py b/main.py index c27e16e..b3e8fad 100644 --- a/main.py +++ b/main.py @@ -54,41 +54,5 @@ def nth_fibonacci(n): # Return the nth Fibonacci number return dp[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 - - # 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) +# Remove lines 57-59 entirely From 744f1c1932c7a8627acb9f3b809c1afd230b47f8 Mon Sep 17 00:00:00 2001 From: Arvind Shelke Date: Tue, 12 Aug 2025 15:56:43 +0530 Subject: [PATCH 10/11] Update main.py --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index b3e8fad..75e342c 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ def nth_fibonacci_util(n, memo): # Base case: if n is 0 or 1, return n if n <= 1: - return n + return m # Check if the result is already in the memo table if memo[n] != -1: From 06b510c9485bf307fc69be71a2e3cd451b2fb226 Mon Sep 17 00:00:00 2001 From: Arvind Shelke Date: Tue, 12 Aug 2025 16:21:33 +0530 Subject: [PATCH 11/11] Update main.py --- main.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 75e342c..deaf7e9 100644 --- a/main.py +++ b/main.py @@ -16,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)