Skip to content

Commit 2452572

Browse files
authored
Made the API of RangeQueryStatic consistent (#425)
1 parent 94e96a2 commit 2452572

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

pydatastructs/miscellaneous_data_structures/algorithms.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ class RangeQueryStatic:
5353
>>> from pydatastructs import minimum
5454
>>> arr = OneDimensionalArray(int, [4, 6, 1, 5, 7, 3])
5555
>>> RMQ = RangeQueryStatic(arr, minimum)
56-
>>> RMQ.query(3, 5)
56+
>>> RMQ.query(3, 4)
5757
5
58-
>>> RMQ.query(0, 5)
58+
>>> RMQ.query(0, 4)
5959
1
60-
>>> RMQ.query(0, 3)
60+
>>> RMQ.query(0, 2)
6161
1
6262
6363
Note
@@ -97,9 +97,7 @@ def query(start, end):
9797
start: int
9898
The starting index of the range.
9999
end: int
100-
The index just before which the range ends.
101-
This means that this index will be excluded
102-
from the range for generating results.
100+
The ending index of the range.
103101
"""
104102
raise NotImplementedError(
105103
"This is an abstract method.")
@@ -121,7 +119,7 @@ def methods(cls):
121119
return ['query']
122120

123121
def query(self, start, end):
124-
_check_range_query_inputs((start, end), self.bounds)
122+
_check_range_query_inputs((start, end + 1), self.bounds)
125123
return self.sparse_table.query(start, end)
126124

127125

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

142140
def query(self, start, end):
143-
_check_range_query_inputs((start, end), (0, len(self.array)))
141+
_check_range_query_inputs((start, end + 1), (0, len(self.array)))
144142

145-
rsize = end - start
143+
rsize = end - start + 1
146144

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

150148
query_ans = self.func((self.array[start], self.array[start + 1]))
151-
for i in range(start + 2, end):
149+
for i in range(start + 2, end + 1):
152150
query_ans = self.func((query_ans, self.array[i]))
153151
return query_ans

pydatastructs/miscellaneous_data_structures/sparse_table.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,8 @@ def query(self, start, end):
8686
start: int
8787
The starting index of the range.
8888
end: int
89-
The index just before which the range ends.
90-
This means that this index will be excluded
91-
from the range for generating results.
89+
The ending index of the range.
9290
"""
93-
end -= 1
9491
j = int(math.log2(end - start + 1)) + 1
9592
answer = None
9693
while j >= 0:

pydatastructs/miscellaneous_data_structures/tests/test_range_query_static.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ def _test_RangeQueryStatic_common(func, gen_expected):
1212

1313
array = OneDimensionalArray(int, [1])
1414
rq = RangeQueryStatic(array, func)
15-
assert rq.query(0, 1) == 1
16-
raises(ValueError, lambda: rq.query(0, 0))
17-
raises(IndexError, lambda: rq.query(0, 2))
15+
assert rq.query(0, 0) == 1
16+
raises(ValueError, lambda: rq.query(0, -1))
17+
raises(IndexError, lambda: rq.query(0, 1))
1818

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

4040
def _gen_minimum_expected(data, i, j):
41-
return min(data[i:j])
41+
return min(data[i:j + 1])
4242

4343
_test_RangeQueryStatic_common(minimum, _gen_minimum_expected)
4444

4545
def test_RangeQueryStatic_greatest_common_divisor():
4646

4747
def _gen_gcd_expected(data, i, j):
48-
if j - i == 1:
48+
if j == i:
4949
return data[i]
5050
else:
5151
expected_gcd = math.gcd(data[i], data[i + 1])
52-
for idx in range(i + 2, j):
52+
for idx in range(i + 2, j + 1):
5353
expected_gcd = math.gcd(expected_gcd, data[idx])
5454
return expected_gcd
5555

@@ -58,6 +58,6 @@ def _gen_gcd_expected(data, i, j):
5858
def test_RangeQueryStatic_summation():
5959

6060
def _gen_summation_expected(data, i, j):
61-
return sum(data[i:j])
61+
return sum(data[i:j + 1])
6262

6363
return _test_RangeQueryStatic_common(summation, _gen_summation_expected)

0 commit comments

Comments
 (0)