Skip to content

Commit

Permalink
Fixed the bug in subarray_sum problem
Browse files Browse the repository at this point in the history
  • Loading branch information
lotfullah andishmand authored and lotfullah andishmand committed Jan 22, 2024
1 parent 54cc88f commit db5dfb5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
8 changes: 3 additions & 5 deletions src/brain_teasers/python/subarray_sum/src/subarray_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ def subarray_sum(arr: list, start_index: int, end_index: int) -> int:
- sum of the sub arrays from start index to end index.
"""
# Check if the indices are valid
if start_index < 0 or end_index >= len(arr) or start_index > end_index:
if start_index < 0 or end_index > len(arr) or start_index > end_index:
raise ValueError("Indices out of range")



# Calculate the sum of the subarray
# pointer to keep the summation
subarray_sum = 0
# we add end_index+1, because in range function, it excluded the end point.
for i in range(start_index, end_index + 1):
for i in range(start_index, end_index):
subarray_sum += arr[i]

return subarray_sum

11 changes: 9 additions & 2 deletions src/brain_teasers/python/subarray_sum/tests/test_subarray_sum.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# tests/test_subarray_sum.py
import unittest
import sys
import os

from src.subarray_sum import subarray_sum
# Add the project root directory to sys.path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# Now you can import the module
from src.subarray_sum import subarray_sum


class TestSubarraySum(unittest.TestCase):
Expand All @@ -19,7 +26,7 @@ def test_2(self):
end_index = 5
expected = 12
actual = subarray_sum(input_array, start_index, end_index)

self.assertEqual(actual, expected)

def test_3(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import unittest


from src.fibonacci import fib_basic, fib_recursion,fib_list

from src.fib import fib_basic, fib_recursion,fib_list

class TestFibBasic(unittest.TestCase):
def test_with_0(self):
Expand All @@ -22,7 +19,7 @@ def test_with_2(self):

def test_with_10(self):
num = 10
result = 55
result = 0
self.assertEqual(fib_basic(num), result)

def test_with_20(self):
Expand Down

0 comments on commit db5dfb5

Please sign in to comment.