Skip to content

Commit e232e37

Browse files
authored
Descriptive names for variables
1 parent 25d453e commit e232e37

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

data_structures/arrays/sparse_table.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"""
1313

1414

15-
def build_sparse_table(arr: list[int], n: int) -> list[int]:
15+
def build_sparse_table(arr: list[int], arr_length: int) -> list[int]:
1616
"""
1717
Precompute range minimum queries with power of two length
1818
and store the precomputed values in a table.
@@ -32,18 +32,18 @@ def build_sparse_table(arr: list[int], n: int) -> list[int]:
3232
raise ValueError("math domain error")
3333

3434
# Initialise lookup table
35-
k = int(math.log2(n)) + 1
36-
lookup = [[0 for i in range(n)] for j in range(k)]
35+
k = int(math.log2(arr_length)) + 1
36+
lookup = [[0 for i in range(arr_length)] for j in range(k)]
3737

38-
for i in range(0, n):
38+
for i in range(0, arr_length):
3939
lookup[0][i] = arr[i]
4040

4141
j = 1
4242

43-
while (1 << j) <= n:
43+
while (1 << j) <= arr_length:
4444
# Compute the minimum value for all intervals with size (2 ** j)
4545
i = 0
46-
while (i + (1 << j) - 1) < n:
46+
while (i + (1 << j) - 1) < arr_length:
4747
lookup[j][i] = min(lookup[j - 1][i + (1 << (j - 1))], lookup[j - 1][i])
4848
i += 1
4949

@@ -52,7 +52,7 @@ def build_sparse_table(arr: list[int], n: int) -> list[int]:
5252
return lookup
5353

5454

55-
def query(lookup: list[int], L: int, R: int) -> int:
55+
def query(lookup: list[int], left_bound: int, right_bound: int) -> int:
5656
"""
5757
>>> query(build_sparse_table([8, 1, 0, 3, 4, 9, 3], 7), 0, 4)
5858
0
@@ -77,15 +77,15 @@ def query(lookup: list[int], L: int, R: int) -> int:
7777

7878
if lookup == []:
7979
raise ValueError("math domain error")
80-
if L < 0 or R >= len(lookup[0]):
80+
if left_bound < 0 or right_bound >= len(lookup[0]):
8181
raise IndexError("list index out of range")
8282

8383
"""
8484
Find the highest power of 2
8585
that is at least the number of elements in a given range.
8686
"""
87-
j = int(math.log2(R - L + 1))
88-
return min(lookup[j][R - (1 << j) + 1], lookup[j][L])
87+
j = int(math.log2(right_bound - left_bound + 1))
88+
return min(lookup[j][right_bound - (1 << j) + 1], lookup[j][left_bound])
8989

9090

9191
if __name__ == "__main__":

0 commit comments

Comments
 (0)