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
31 changes: 11 additions & 20 deletions data_structures/heap/heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@

class Comparable(Protocol):
@abstractmethod
def __lt__(self: T, other: T) -> bool:
pass

@abstractmethod
def __gt__(self: T, other: T) -> bool:
pass

@abstractmethod
def __eq__(self: T, other: object) -> bool:
def __gt__(self: T, other: T, /) -> bool:
pass


Expand Down Expand Up @@ -54,7 +46,7 @@ def __repr__(self) -> str:

def parent_index(self, child_idx: int) -> int | None:
"""
returns the parent index based on the given child index
Returns the parent index based on the given child index

>>> h = Heap()
>>> h.build_max_heap([103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5])
Expand Down Expand Up @@ -90,8 +82,8 @@ def parent_index(self, child_idx: int) -> int | None:

def left_child_idx(self, parent_idx: int) -> int | None:
"""
return the left child index if the left child exists.
if not, return None.
Return the left child index if the left child exists.
If not, return None.
"""
left_child_index = 2 * parent_idx + 1
if left_child_index < self.heap_size:
Expand All @@ -100,8 +92,8 @@ def left_child_idx(self, parent_idx: int) -> int | None:

def right_child_idx(self, parent_idx: int) -> int | None:
"""
return the right child index if the right child exists.
if not, return None.
Return the right child index if the right child exists.
If not, return None.
"""
right_child_index = 2 * parent_idx + 2
if right_child_index < self.heap_size:
Expand All @@ -110,10 +102,10 @@ def right_child_idx(self, parent_idx: int) -> int | None:

def max_heapify(self, index: int) -> None:
"""
correct a single violation of the heap property in a subtree's root.
Correct a single violation of the heap property in a subtree's root.

It is the function that is responsible for restoring the property
of Max heap i.e the maximum element is always at top.
of a max heap, i.e the maximum element is always at the top.
"""
if index < self.heap_size:
violation: int = index
Expand All @@ -133,7 +125,7 @@ def max_heapify(self, index: int) -> None:

def build_max_heap(self, collection: Iterable[T]) -> None:
"""
build max heap from an unsorted array
Build a max heap from an unsorted array

>>> h = Heap()
>>> h.build_max_heap([20,40,50,20,10])
Expand Down Expand Up @@ -164,7 +156,7 @@ def build_max_heap(self, collection: Iterable[T]) -> None:

def extract_max(self) -> T:
"""
get and remove max from heap
Get and remove max from heap

>>> h = Heap()
>>> h.build_max_heap([20,40,50,20,10])
Expand Down Expand Up @@ -195,7 +187,7 @@ def extract_max(self) -> T:

def insert(self, value: T) -> None:
"""
insert a new value into the max heap
Insert a new value into the max heap

>>> h = Heap()
>>> h.insert(10)
Expand Down Expand Up @@ -241,7 +233,6 @@ def heap_sort(self) -> None:
if __name__ == "__main__":
import doctest

# run doc test
doctest.testmod()

# demo
Expand Down
9 changes: 5 additions & 4 deletions machine_learning/k_nearest_neighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
from heapq import nsmallest

import numpy as np
from numpy.typing import NDArray
from sklearn import datasets
from sklearn.model_selection import train_test_split


class KNN:
def __init__(
self,
train_data: np.ndarray[float],
train_target: np.ndarray[int],
train_data: NDArray[np.float64],
train_target: NDArray[np.int64],
class_labels: list[str],
) -> None:
"""
Expand All @@ -34,7 +35,7 @@ def __init__(
self.labels = class_labels

@staticmethod
def _euclidean_distance(a: np.ndarray[float], b: np.ndarray[float]) -> float:
def _euclidean_distance(a: NDArray[np.float64], b: NDArray[np.float64]) -> float:
"""
Calculate the Euclidean distance between two points
>>> KNN._euclidean_distance(np.array([0, 0]), np.array([3, 4]))
Expand All @@ -44,7 +45,7 @@ def _euclidean_distance(a: np.ndarray[float], b: np.ndarray[float]) -> float:
"""
return float(np.linalg.norm(a - b))

def classify(self, pred_point: np.ndarray[float], k: int = 5) -> str:
def classify(self, pred_point: NDArray[np.float64], k: int = 5) -> str:
"""
Classify a given point using the kNN algorithm
>>> train_X = np.array(
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ rules.call-non-callable = "ignore"
rules.deprecated = "ignore"
rules.invalid-argument-type = "ignore"
rules.invalid-return-type = "ignore"
rules.invalid-type-arguments = "ignore"
rules.no-matching-overload = "ignore"
rules.not-iterable = "ignore"
rules.not-subscriptable = "ignore"
Expand Down
Loading