Skip to content

lazy_segment_tree.py-static-type-checking #2303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
24 changes: 14 additions & 10 deletions data_structures/binary_tree/lazy_segment_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@


class SegmentTree:
def __init__(self, N):
def __init__(self, N: int) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is N in uppercase? Uppercase is reserved for constants in Python naming. Could we come up with a more self-documenting name than a single character?

self.N = N
self.st = [
self.st: List[int] = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is st? Why does the reader have to guess? Can't we come up with a more self-documenting name?

0 for i in range(0, 4 * N)
] # approximate the overall size of segment tree with array N
self.lazy = [0 for i in range(0, 4 * N)] # create array to store lazy update
self.flag = [0 for i in range(0, 4 * N)] # flag for lazy update
self.lazy: List[int] = [
0 for i in range(0, 4 * N)
] # create array to store lazy update
self.flag: List[int] = [0 for i in range(0, 4 * N)] # flag for lazy update
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly are we flagging with this variable? This type hint is not required. The typing system can already figure out that a list comprehension creates a list and that 0 is an int. This type hint just creates cognitive clutter for the reader.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, the changes I made in this pr were already included in pr#2329 which was merged recently, so I closed this pr. I'll make these changes separately and send out a new pr. Thanks


def left(self, idx):
def left(self, idx: int) -> int:
return idx * 2

def right(self, idx):
def right(self, idx: int) -> int:
return idx * 2 + 1

def build(self, idx, l, r, A): # noqa: E741
def build(self, idx: int, l: int, r: int, A: List[int]) -> None: # noqa: E741
if l == r: # noqa: E741
self.st[idx] = A[l - 1]
else:
Expand All @@ -27,7 +29,9 @@ def build(self, idx, l, r, A): # noqa: E741

# update with O(lg N) (Normal segment tree without lazy update will take O(Nlg N)
# for each update)
def update(self, idx, l, r, a, b, val): # noqa: E741
def update(
self, idx: int, l: int, r: int, a: int, b: int, val: int
) -> bool: # noqa: E741
"""
update(1, 1, N, a, b, v) for update val v to [a,b]
"""
Expand Down Expand Up @@ -57,7 +61,7 @@ def update(self, idx, l, r, a, b, val): # noqa: E741
return True

# query with O(lg N)
def query(self, idx, l, r, a, b): # noqa: E741
def query(self, idx: int, l: int, r: int, a: int, b: int) -> int: # noqa: E741
"""
query(1, 1, N, a, b) for query max of [a,b]
"""
Expand All @@ -78,7 +82,7 @@ def query(self, idx, l, r, a, b): # noqa: E741
q2 = self.query(self.right(idx), mid + 1, r, a, b)
return max(q1, q2)

def showData(self):
def showData(self) -> None:
showList = []
for i in range(1, N + 1):
showList += [self.query(1, 1, self.N, i, i)]
Expand Down