From 6f4eb06a43ccd5902bab8b665aa6bc58b5c28112 Mon Sep 17 00:00:00 2001 From: Dylan Buchi Date: Wed, 27 Oct 2021 14:41:32 -0300 Subject: [PATCH 1/4] Add abstract base class Comparable --- data_structures/heap/skew_heap.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/data_structures/heap/skew_heap.py b/data_structures/heap/skew_heap.py index b59441389a91..63b570624bd0 100644 --- a/data_structures/heap/skew_heap.py +++ b/data_structures/heap/skew_heap.py @@ -2,9 +2,17 @@ from __future__ import annotations +from abc import ABCMeta, abstractmethod from typing import Generic, Iterable, Iterator, TypeVar -T = TypeVar("T") + +class Comparable(metaclass=ABCMeta): + @abstractmethod + def __gt__(self, other: object) -> bool: + ... + + +T = TypeVar("T", bound=Comparable) class SkewNode(Generic[T]): From 890fd33ddaa9c7104a76e9862bfd83d3437b1fd0 Mon Sep 17 00:00:00 2001 From: Dylan Buchi Date: Wed, 27 Oct 2021 15:01:13 -0300 Subject: [PATCH 2/4] [mypy] Fix type annotations (strict mode) --- data_structures/heap/skew_heap.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/data_structures/heap/skew_heap.py b/data_structures/heap/skew_heap.py index 63b570624bd0..dc880ee9583f 100644 --- a/data_structures/heap/skew_heap.py +++ b/data_structures/heap/skew_heap.py @@ -3,7 +3,7 @@ from __future__ import annotations from abc import ABCMeta, abstractmethod -from typing import Generic, Iterable, Iterator, TypeVar +from typing import Any, Generic, Iterable, Iterator, TypeVar class Comparable(metaclass=ABCMeta): @@ -78,14 +78,16 @@ class SkewHeap(Generic[T]): """ def __init__(self, data: Iterable[T] | None = ()) -> None: + """ >>> sh = SkewHeap([3, 1, 3, 7]) >>> list(sh) [1, 3, 3, 7] """ self._root: SkewNode[T] | None = None - for item in data: - self.insert(item) + if data: + for item in data: + self.insert(item) def __bool__(self) -> bool: """ @@ -111,7 +113,7 @@ def __iter__(self) -> Iterator[T]: >>> list(sh) [1, 3, 3, 7] """ - result = [] + result: list[Any] = [] while self: result.append(self.pop()) @@ -135,7 +137,7 @@ def insert(self, value: T) -> None: """ self._root = SkewNode.merge(self._root, SkewNode(value)) - def pop(self) -> T: + def pop(self) -> T | None: """ Pop the smallest value from the heap and return it. @@ -154,7 +156,9 @@ def pop(self) -> T: IndexError: Can't get top element for the empty heap. """ result = self.top() - self._root = SkewNode.merge(self._root.left, self._root.right) + self._root = ( + SkewNode.merge(self._root.left, self._root.right) if self._root else None + ) return result @@ -180,7 +184,7 @@ def top(self) -> T: raise IndexError("Can't get top element for the empty heap.") return self._root.value - def clear(self): + def clear(self) -> None: """ Clear the heap. From 927f7094643f19559828b81d2d97dfaf5c3e873b Mon Sep 17 00:00:00 2001 From: Dylan Buchi Date: Wed, 27 Oct 2021 15:01:48 -0300 Subject: [PATCH 3/4] Fix a typo --- data_structures/heap/skew_heap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/heap/skew_heap.py b/data_structures/heap/skew_heap.py index dc880ee9583f..9a6e4bb36352 100644 --- a/data_structures/heap/skew_heap.py +++ b/data_structures/heap/skew_heap.py @@ -59,7 +59,7 @@ class SkewHeap(Generic[T]): values. Both operations take O(logN) time where N is the size of the structure. Wiki: https://en.wikipedia.org/wiki/Skew_heap - Visualisation: https://www.cs.usfca.edu/~galles/visualization/SkewHeap.html + Visualization: https://www.cs.usfca.edu/~galles/visualization/SkewHeap.html >>> list(SkewHeap([2, 3, 1, 5, 1, 7])) [1, 1, 2, 3, 5, 7] From 4b43e4533ce73a29dfced1d65d70d9fed4237877 Mon Sep 17 00:00:00 2001 From: Dylan Buchi Date: Sat, 30 Oct 2021 17:35:28 -0300 Subject: [PATCH 4/4] Remove Comparable class and set bound to bool --- data_structures/heap/skew_heap.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/data_structures/heap/skew_heap.py b/data_structures/heap/skew_heap.py index 9a6e4bb36352..16ddc5545e36 100644 --- a/data_structures/heap/skew_heap.py +++ b/data_structures/heap/skew_heap.py @@ -2,17 +2,9 @@ from __future__ import annotations -from abc import ABCMeta, abstractmethod from typing import Any, Generic, Iterable, Iterator, TypeVar - -class Comparable(metaclass=ABCMeta): - @abstractmethod - def __gt__(self, other: object) -> bool: - ... - - -T = TypeVar("T", bound=Comparable) +T = TypeVar("T", bound=bool) class SkewNode(Generic[T]):