Skip to content

Commit

Permalink
Made the API of RangeQueryStatic consistent (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
czgdp1807 authored Oct 31, 2021
1 parent 94e96a2 commit 2452572
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
18 changes: 8 additions & 10 deletions pydatastructs/miscellaneous_data_structures/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ class RangeQueryStatic:
>>> from pydatastructs import minimum
>>> arr = OneDimensionalArray(int, [4, 6, 1, 5, 7, 3])
>>> RMQ = RangeQueryStatic(arr, minimum)
>>> RMQ.query(3, 5)
>>> RMQ.query(3, 4)
5
>>> RMQ.query(0, 5)
>>> RMQ.query(0, 4)
1
>>> RMQ.query(0, 3)
>>> RMQ.query(0, 2)
1
Note
Expand Down Expand Up @@ -97,9 +97,7 @@ def query(start, end):
start: int
The starting index of the range.
end: int
The index just before which the range ends.
This means that this index will be excluded
from the range for generating results.
The ending index of the range.
"""
raise NotImplementedError(
"This is an abstract method.")
Expand All @@ -121,7 +119,7 @@ def methods(cls):
return ['query']

def query(self, start, end):
_check_range_query_inputs((start, end), self.bounds)
_check_range_query_inputs((start, end + 1), self.bounds)
return self.sparse_table.query(start, end)


Expand All @@ -140,14 +138,14 @@ def methods(cls):
return ['query']

def query(self, start, end):
_check_range_query_inputs((start, end), (0, len(self.array)))
_check_range_query_inputs((start, end + 1), (0, len(self.array)))

rsize = end - start
rsize = end - start + 1

if rsize == 1:
return self.func((self.array[start],))

query_ans = self.func((self.array[start], self.array[start + 1]))
for i in range(start + 2, end):
for i in range(start + 2, end + 1):
query_ans = self.func((query_ans, self.array[i]))
return query_ans
5 changes: 1 addition & 4 deletions pydatastructs/miscellaneous_data_structures/sparse_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,8 @@ def query(self, start, end):
start: int
The starting index of the range.
end: int
The index just before which the range ends.
This means that this index will be excluded
from the range for generating results.
The ending index of the range.
"""
end -= 1
j = int(math.log2(end - start + 1)) + 1
answer = None
while j >= 0:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def _test_RangeQueryStatic_common(func, gen_expected):

array = OneDimensionalArray(int, [1])
rq = RangeQueryStatic(array, func)
assert rq.query(0, 1) == 1
raises(ValueError, lambda: rq.query(0, 0))
raises(IndexError, lambda: rq.query(0, 2))
assert rq.query(0, 0) == 1
raises(ValueError, lambda: rq.query(0, -1))
raises(IndexError, lambda: rq.query(0, 1))

array_sizes = [3, 6, 12, 24, 48, 96]
random.seed(0)
Expand All @@ -38,18 +38,18 @@ def _test_RangeQueryStatic_common(func, gen_expected):
def test_RangeQueryStatic_minimum():

def _gen_minimum_expected(data, i, j):
return min(data[i:j])
return min(data[i:j + 1])

_test_RangeQueryStatic_common(minimum, _gen_minimum_expected)

def test_RangeQueryStatic_greatest_common_divisor():

def _gen_gcd_expected(data, i, j):
if j - i == 1:
if j == i:
return data[i]
else:
expected_gcd = math.gcd(data[i], data[i + 1])
for idx in range(i + 2, j):
for idx in range(i + 2, j + 1):
expected_gcd = math.gcd(expected_gcd, data[idx])
return expected_gcd

Expand All @@ -58,6 +58,6 @@ def _gen_gcd_expected(data, i, j):
def test_RangeQueryStatic_summation():

def _gen_summation_expected(data, i, j):
return sum(data[i:j])
return sum(data[i:j + 1])

return _test_RangeQueryStatic_common(summation, _gen_summation_expected)

0 comments on commit 2452572

Please sign in to comment.