Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions data_structures/binary_tree/segment_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, a) -> None:
if self.N:
self.build(1, 0, self.N - 1)

def left(self, idx):
def left(self, idx) -> int:
"""
Returns the left child index for a given index in a binary tree.

Expand All @@ -23,7 +23,7 @@ def left(self, idx):
"""
return idx * 2

def right(self, idx):
def right(self, idx) -> int:
"""
Returns the right child index for a given index in a binary tree.

Expand All @@ -44,7 +44,7 @@ def build(self, idx, left, right) -> None:
self.build(self.right(idx), mid + 1, right)
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])

def update(self, a, b, val):
def update(self, a, b, val) -> bool:
"""
Update the values in the segment tree in the range [a,b] with the given value.

Expand All @@ -71,7 +71,7 @@ def update_recursive(self, idx, left, right, a, b, val) -> bool:
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
return True

def query(self, a, b):
def query(self, a, b) -> float:
"""
Query the maximum value in the range [a,b].

Expand All @@ -83,7 +83,7 @@ def query(self, a, b):
"""
return self.query_recursive(1, 0, self.N - 1, a - 1, b - 1)

def query_recursive(self, idx, left, right, a, b):
def query_recursive(self, idx, left, right, a, b) -> float:
"""
query(1, 1, N, a, b) for query max of [a,b]
"""
Expand Down
6 changes: 3 additions & 3 deletions data_structures/hashing/hash_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(
self.__aux_list: list = []
self._keys: dict = {}

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

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

def hash_function(self, key):
def hash_function(self, key) -> int:
"""
Generates hash for the given key value

Expand Down
2 changes: 1 addition & 1 deletion data_structures/hashing/hash_table_with_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def _set_value(self, key, data) -> None:
self.values[key].appendleft(data)
self._keys[key] = self.values[key]

def balanced_factor(self):
def balanced_factor(self) -> float:
return (
sum(self.charge_factor - len(slot) for slot in self.values)
/ self.size_table
Expand Down
2 changes: 1 addition & 1 deletion data_structures/heap/binomial_heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def peek(self):
"""
return self.min_node.val

def is_empty(self):
def is_empty(self) -> bool:
return self.size == 0

def delete_min(self):
Expand Down
2 changes: 1 addition & 1 deletion data_structures/heap/max_heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def pop(self) -> int:
return max_value

@property
def get_list(self):
def get_list(self) -> list:
return self.__heap[1:]

def __len__(self) -> int:
Expand Down
10 changes: 5 additions & 5 deletions data_structures/heap/min_heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ def __init__(self, array) -> None:
def __getitem__(self, key):
return self.get_value(key)

def get_parent_idx(self, idx):
def get_parent_idx(self, idx) -> int:
return (idx - 1) // 2

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

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

def get_value(self, key):
return self.heap_dict[key]

def build_heap(self, array):
def build_heap(self, array) -> list:
last_idx = len(array) - 1
start_from = self.get_parent_idx(last_idx)

Expand Down Expand Up @@ -120,7 +120,7 @@ def insert(self, node) -> None:
self.heap_dict[node.name] = node.val
self.sift_up(len(self.heap) - 1)

def is_empty(self):
def is_empty(self) -> bool:
return len(self.heap) == 0

def decrease_key(self, node, new_value) -> None:
Expand Down
2 changes: 1 addition & 1 deletion data_structures/linked_list/doubly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def delete(self, data) -> str:
current.next.previous = current.previous # 1 <--> 3
return data

def is_empty(self):
def is_empty(self) -> bool:
"""
>>> linked_list = DoublyLinkedList()
>>> linked_list.is_empty()
Expand Down
2 changes: 1 addition & 1 deletion data_structures/linked_list/doubly_linked_list_two.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def remove_node_pointers(node: Node) -> None:
node.next = None
node.previous = None

def is_empty(self):
def is_empty(self) -> bool:
return self.head is None


Expand Down
2 changes: 1 addition & 1 deletion data_structures/queues/circular_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def first(self):
"""
return False if self.is_empty() else self.array[self.front]

def enqueue(self, data):
def enqueue(self, data) -> "CircularQueue":
"""
This function inserts an element at the end of the queue using self.rear value
as an index.
Expand Down
6 changes: 3 additions & 3 deletions data_structures/stacks/prefix_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}


def is_operand(c):
def is_operand(c) -> bool:
"""
Return True if the given char c is an operand, e.g. it is a number

Expand All @@ -23,7 +23,7 @@ def is_operand(c):
return c.isdigit()


def evaluate(expression):
def evaluate(expression) -> float:
"""
Evaluate a given expression in prefix notation.
Asserts that the given expression is valid.
Expand Down Expand Up @@ -55,7 +55,7 @@ def evaluate(expression):
return stack.pop()


def evaluate_recursive(expression: list[str]):
def evaluate_recursive(expression: list[str]) -> float:
"""
Alternative recursive implementation

Expand Down
Loading