Skip to content

Commit f5dd538

Browse files
data_structures: add return type annotations (ANN201)
Annotate the unambiguous, primitive/container-returning public methods flagged by ruff ANN201 across data_structures/ (int/bool/float/list/dict and one fluent self-return). Reduces ANN201 in this directory from 58 to 36. Element-typed and sentinel-union returns (e.g. node .data getters, dict|False in the Sudoku solver) are intentionally left for follow-up, since they warrant generics/TypeVar or a type checker rather than a guess. Refs #15296
1 parent 2f4a9fe commit f5dd538

10 files changed

Lines changed: 424 additions & 424 deletions

File tree

data_structures/binary_tree/segment_tree.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(self, a) -> None:
1111
if self.N:
1212
self.build(1, 0, self.N - 1)
1313

14-
def left(self, idx):
14+
def left(self, idx) -> int:
1515
"""
1616
Returns the left child index for a given index in a binary tree.
1717
@@ -23,7 +23,7 @@ def left(self, idx):
2323
"""
2424
return idx * 2
2525

26-
def right(self, idx):
26+
def right(self, idx) -> int:
2727
"""
2828
Returns the right child index for a given index in a binary tree.
2929
@@ -44,7 +44,7 @@ def build(self, idx, left, right) -> None:
4444
self.build(self.right(idx), mid + 1, right)
4545
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
4646

47-
def update(self, a, b, val):
47+
def update(self, a, b, val) -> bool:
4848
"""
4949
Update the values in the segment tree in the range [a,b] with the given value.
5050
@@ -71,7 +71,7 @@ def update_recursive(self, idx, left, right, a, b, val) -> bool:
7171
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
7272
return True
7373

74-
def query(self, a, b):
74+
def query(self, a, b) -> float:
7575
"""
7676
Query the maximum value in the range [a,b].
7777
@@ -83,7 +83,7 @@ def query(self, a, b):
8383
"""
8484
return self.query_recursive(1, 0, self.N - 1, a - 1, b - 1)
8585

86-
def query_recursive(self, idx, left, right, a, b):
86+
def query_recursive(self, idx, left, right, a, b) -> float:
8787
"""
8888
query(1, 1, N, a, b) for query max of [a,b]
8989
"""

data_structures/hashing/hash_table.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(
2222
self.__aux_list: list = []
2323
self._keys: dict = {}
2424

25-
def keys(self):
25+
def keys(self) -> dict:
2626
"""
2727
The keys function returns a dictionary containing the key value pairs.
2828
key being the index number in hash table and value being the data value.
@@ -48,12 +48,12 @@ def keys(self):
4848
"""
4949
return self._keys
5050

51-
def balanced_factor(self):
51+
def balanced_factor(self) -> float:
5252
return sum(1 for slot in self.values if slot is not None) / (
5353
self.size_table * self.charge_factor
5454
)
5555

56-
def hash_function(self, key):
56+
def hash_function(self, key) -> int:
5757
"""
5858
Generates hash for the given key value
5959

data_structures/hashing/hash_table_with_linked_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def _set_value(self, key, data) -> None:
1212
self.values[key].appendleft(data)
1313
self._keys[key] = self.values[key]
1414

15-
def balanced_factor(self):
15+
def balanced_factor(self) -> float:
1616
return (
1717
sum(self.charge_factor - len(slot) for slot in self.values)
1818
/ self.size_table

0 commit comments

Comments
 (0)