mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
[mypy] Fix and add type annotations (#5618)
This commit is contained in:
parent
4eb5c12727
commit
8285913e81
|
@ -1,6 +1,8 @@
|
||||||
"""
|
"""
|
||||||
Implementation of double ended queue.
|
Implementation of double ended queue.
|
||||||
"""
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Any, Iterable
|
from typing import Any, Iterable
|
||||||
|
|
||||||
|
@ -8,35 +10,23 @@ from typing import Any, Iterable
|
||||||
class Deque:
|
class Deque:
|
||||||
"""
|
"""
|
||||||
Deque data structure.
|
Deque data structure.
|
||||||
|
|
||||||
Operations
|
Operations
|
||||||
----------
|
----------
|
||||||
append(val: Any) -> None
|
append(val: Any) -> None
|
||||||
|
|
||||||
appendleft(val: Any) -> None
|
appendleft(val: Any) -> None
|
||||||
|
|
||||||
extend(iter: Iterable) -> None
|
extend(iter: Iterable) -> None
|
||||||
|
|
||||||
extendleft(iter: Iterable) -> None
|
extendleft(iter: Iterable) -> None
|
||||||
|
|
||||||
pop() -> Any
|
pop() -> Any
|
||||||
|
|
||||||
popleft() -> Any
|
popleft() -> Any
|
||||||
|
|
||||||
|
|
||||||
Observers
|
Observers
|
||||||
---------
|
---------
|
||||||
is_empty() -> bool
|
is_empty() -> bool
|
||||||
|
|
||||||
|
|
||||||
Attributes
|
Attributes
|
||||||
----------
|
----------
|
||||||
_front: _Node
|
_front: _Node
|
||||||
front of the deque a.k.a. the first element
|
front of the deque a.k.a. the first element
|
||||||
|
|
||||||
_back: _Node
|
_back: _Node
|
||||||
back of the element a.k.a. the last element
|
back of the element a.k.a. the last element
|
||||||
|
|
||||||
_len: int
|
_len: int
|
||||||
the number of nodes
|
the number of nodes
|
||||||
"""
|
"""
|
||||||
|
@ -51,13 +41,12 @@ class Deque:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
val: Any = None
|
val: Any = None
|
||||||
next: "Deque._Node" = None
|
next: Deque._Node | None = None
|
||||||
prev: "Deque._Node" = None
|
prev: Deque._Node | None = None
|
||||||
|
|
||||||
class _Iterator:
|
class _Iterator:
|
||||||
"""
|
"""
|
||||||
Helper class for iteration. Will be used to implement iteration.
|
Helper class for iteration. Will be used to implement iteration.
|
||||||
|
|
||||||
Attributes
|
Attributes
|
||||||
----------
|
----------
|
||||||
_cur: _Node
|
_cur: _Node
|
||||||
|
@ -66,10 +55,10 @@ class Deque:
|
||||||
|
|
||||||
__slots__ = ["_cur"]
|
__slots__ = ["_cur"]
|
||||||
|
|
||||||
def __init__(self, cur: "Deque._Node") -> None:
|
def __init__(self, cur: Deque._Node | None) -> None:
|
||||||
self._cur = cur
|
self._cur = cur
|
||||||
|
|
||||||
def __iter__(self) -> "Deque._Iterator":
|
def __iter__(self) -> Deque._Iterator:
|
||||||
"""
|
"""
|
||||||
>>> our_deque = Deque([1, 2, 3])
|
>>> our_deque = Deque([1, 2, 3])
|
||||||
>>> iterator = iter(our_deque)
|
>>> iterator = iter(our_deque)
|
||||||
|
@ -95,9 +84,10 @@ class Deque:
|
||||||
|
|
||||||
return val
|
return val
|
||||||
|
|
||||||
def __init__(self, iterable: Iterable = None) -> None:
|
def __init__(self, iterable: Iterable[Any] | None = None) -> None:
|
||||||
self._front = self._back = None
|
self._front: Any = None
|
||||||
self._len = 0
|
self._back: Any = None
|
||||||
|
self._len: int = 0
|
||||||
|
|
||||||
if iterable is not None:
|
if iterable is not None:
|
||||||
# append every value to the deque
|
# append every value to the deque
|
||||||
|
@ -108,7 +98,6 @@ class Deque:
|
||||||
"""
|
"""
|
||||||
Adds val to the end of the deque.
|
Adds val to the end of the deque.
|
||||||
Time complexity: O(1)
|
Time complexity: O(1)
|
||||||
|
|
||||||
>>> our_deque_1 = Deque([1, 2, 3])
|
>>> our_deque_1 = Deque([1, 2, 3])
|
||||||
>>> our_deque_1.append(4)
|
>>> our_deque_1.append(4)
|
||||||
>>> our_deque_1
|
>>> our_deque_1
|
||||||
|
@ -117,7 +106,6 @@ class Deque:
|
||||||
>>> our_deque_2.append('c')
|
>>> our_deque_2.append('c')
|
||||||
>>> our_deque_2
|
>>> our_deque_2
|
||||||
['a', 'b', 'c']
|
['a', 'b', 'c']
|
||||||
|
|
||||||
>>> from collections import deque
|
>>> from collections import deque
|
||||||
>>> deque_collections_1 = deque([1, 2, 3])
|
>>> deque_collections_1 = deque([1, 2, 3])
|
||||||
>>> deque_collections_1.append(4)
|
>>> deque_collections_1.append(4)
|
||||||
|
@ -127,7 +115,6 @@ class Deque:
|
||||||
>>> deque_collections_2.append('c')
|
>>> deque_collections_2.append('c')
|
||||||
>>> deque_collections_2
|
>>> deque_collections_2
|
||||||
deque(['a', 'b', 'c'])
|
deque(['a', 'b', 'c'])
|
||||||
|
|
||||||
>>> list(our_deque_1) == list(deque_collections_1)
|
>>> list(our_deque_1) == list(deque_collections_1)
|
||||||
True
|
True
|
||||||
>>> list(our_deque_2) == list(deque_collections_2)
|
>>> list(our_deque_2) == list(deque_collections_2)
|
||||||
|
@ -153,7 +140,6 @@ class Deque:
|
||||||
"""
|
"""
|
||||||
Adds val to the beginning of the deque.
|
Adds val to the beginning of the deque.
|
||||||
Time complexity: O(1)
|
Time complexity: O(1)
|
||||||
|
|
||||||
>>> our_deque_1 = Deque([2, 3])
|
>>> our_deque_1 = Deque([2, 3])
|
||||||
>>> our_deque_1.appendleft(1)
|
>>> our_deque_1.appendleft(1)
|
||||||
>>> our_deque_1
|
>>> our_deque_1
|
||||||
|
@ -162,7 +148,6 @@ class Deque:
|
||||||
>>> our_deque_2.appendleft('a')
|
>>> our_deque_2.appendleft('a')
|
||||||
>>> our_deque_2
|
>>> our_deque_2
|
||||||
['a', 'b', 'c']
|
['a', 'b', 'c']
|
||||||
|
|
||||||
>>> from collections import deque
|
>>> from collections import deque
|
||||||
>>> deque_collections_1 = deque([2, 3])
|
>>> deque_collections_1 = deque([2, 3])
|
||||||
>>> deque_collections_1.appendleft(1)
|
>>> deque_collections_1.appendleft(1)
|
||||||
|
@ -172,7 +157,6 @@ class Deque:
|
||||||
>>> deque_collections_2.appendleft('a')
|
>>> deque_collections_2.appendleft('a')
|
||||||
>>> deque_collections_2
|
>>> deque_collections_2
|
||||||
deque(['a', 'b', 'c'])
|
deque(['a', 'b', 'c'])
|
||||||
|
|
||||||
>>> list(our_deque_1) == list(deque_collections_1)
|
>>> list(our_deque_1) == list(deque_collections_1)
|
||||||
True
|
True
|
||||||
>>> list(our_deque_2) == list(deque_collections_2)
|
>>> list(our_deque_2) == list(deque_collections_2)
|
||||||
|
@ -194,11 +178,10 @@ class Deque:
|
||||||
# make sure there were no errors
|
# make sure there were no errors
|
||||||
assert not self.is_empty(), "Error on appending value."
|
assert not self.is_empty(), "Error on appending value."
|
||||||
|
|
||||||
def extend(self, iter: Iterable) -> None:
|
def extend(self, iter: Iterable[Any]) -> None:
|
||||||
"""
|
"""
|
||||||
Appends every value of iter to the end of the deque.
|
Appends every value of iter to the end of the deque.
|
||||||
Time complexity: O(n)
|
Time complexity: O(n)
|
||||||
|
|
||||||
>>> our_deque_1 = Deque([1, 2, 3])
|
>>> our_deque_1 = Deque([1, 2, 3])
|
||||||
>>> our_deque_1.extend([4, 5])
|
>>> our_deque_1.extend([4, 5])
|
||||||
>>> our_deque_1
|
>>> our_deque_1
|
||||||
|
@ -207,7 +190,6 @@ class Deque:
|
||||||
>>> our_deque_2.extend('cd')
|
>>> our_deque_2.extend('cd')
|
||||||
>>> our_deque_2
|
>>> our_deque_2
|
||||||
['a', 'b', 'c', 'd']
|
['a', 'b', 'c', 'd']
|
||||||
|
|
||||||
>>> from collections import deque
|
>>> from collections import deque
|
||||||
>>> deque_collections_1 = deque([1, 2, 3])
|
>>> deque_collections_1 = deque([1, 2, 3])
|
||||||
>>> deque_collections_1.extend([4, 5])
|
>>> deque_collections_1.extend([4, 5])
|
||||||
|
@ -217,7 +199,6 @@ class Deque:
|
||||||
>>> deque_collections_2.extend('cd')
|
>>> deque_collections_2.extend('cd')
|
||||||
>>> deque_collections_2
|
>>> deque_collections_2
|
||||||
deque(['a', 'b', 'c', 'd'])
|
deque(['a', 'b', 'c', 'd'])
|
||||||
|
|
||||||
>>> list(our_deque_1) == list(deque_collections_1)
|
>>> list(our_deque_1) == list(deque_collections_1)
|
||||||
True
|
True
|
||||||
>>> list(our_deque_2) == list(deque_collections_2)
|
>>> list(our_deque_2) == list(deque_collections_2)
|
||||||
|
@ -226,11 +207,10 @@ class Deque:
|
||||||
for val in iter:
|
for val in iter:
|
||||||
self.append(val)
|
self.append(val)
|
||||||
|
|
||||||
def extendleft(self, iter: Iterable) -> None:
|
def extendleft(self, iter: Iterable[Any]) -> None:
|
||||||
"""
|
"""
|
||||||
Appends every value of iter to the beginning of the deque.
|
Appends every value of iter to the beginning of the deque.
|
||||||
Time complexity: O(n)
|
Time complexity: O(n)
|
||||||
|
|
||||||
>>> our_deque_1 = Deque([1, 2, 3])
|
>>> our_deque_1 = Deque([1, 2, 3])
|
||||||
>>> our_deque_1.extendleft([0, -1])
|
>>> our_deque_1.extendleft([0, -1])
|
||||||
>>> our_deque_1
|
>>> our_deque_1
|
||||||
|
@ -239,7 +219,6 @@ class Deque:
|
||||||
>>> our_deque_2.extendleft('ba')
|
>>> our_deque_2.extendleft('ba')
|
||||||
>>> our_deque_2
|
>>> our_deque_2
|
||||||
['a', 'b', 'c', 'd']
|
['a', 'b', 'c', 'd']
|
||||||
|
|
||||||
>>> from collections import deque
|
>>> from collections import deque
|
||||||
>>> deque_collections_1 = deque([1, 2, 3])
|
>>> deque_collections_1 = deque([1, 2, 3])
|
||||||
>>> deque_collections_1.extendleft([0, -1])
|
>>> deque_collections_1.extendleft([0, -1])
|
||||||
|
@ -249,7 +228,6 @@ class Deque:
|
||||||
>>> deque_collections_2.extendleft('ba')
|
>>> deque_collections_2.extendleft('ba')
|
||||||
>>> deque_collections_2
|
>>> deque_collections_2
|
||||||
deque(['a', 'b', 'c', 'd'])
|
deque(['a', 'b', 'c', 'd'])
|
||||||
|
|
||||||
>>> list(our_deque_1) == list(deque_collections_1)
|
>>> list(our_deque_1) == list(deque_collections_1)
|
||||||
True
|
True
|
||||||
>>> list(our_deque_2) == list(deque_collections_2)
|
>>> list(our_deque_2) == list(deque_collections_2)
|
||||||
|
@ -262,16 +240,13 @@ class Deque:
|
||||||
"""
|
"""
|
||||||
Removes the last element of the deque and returns it.
|
Removes the last element of the deque and returns it.
|
||||||
Time complexity: O(1)
|
Time complexity: O(1)
|
||||||
|
|
||||||
@returns topop.val: the value of the node to pop.
|
@returns topop.val: the value of the node to pop.
|
||||||
|
|
||||||
>>> our_deque = Deque([1, 2, 3, 15182])
|
>>> our_deque = Deque([1, 2, 3, 15182])
|
||||||
>>> our_popped = our_deque.pop()
|
>>> our_popped = our_deque.pop()
|
||||||
>>> our_popped
|
>>> our_popped
|
||||||
15182
|
15182
|
||||||
>>> our_deque
|
>>> our_deque
|
||||||
[1, 2, 3]
|
[1, 2, 3]
|
||||||
|
|
||||||
>>> from collections import deque
|
>>> from collections import deque
|
||||||
>>> deque_collections = deque([1, 2, 3, 15182])
|
>>> deque_collections = deque([1, 2, 3, 15182])
|
||||||
>>> collections_popped = deque_collections.pop()
|
>>> collections_popped = deque_collections.pop()
|
||||||
|
@ -279,7 +254,6 @@ class Deque:
|
||||||
15182
|
15182
|
||||||
>>> deque_collections
|
>>> deque_collections
|
||||||
deque([1, 2, 3])
|
deque([1, 2, 3])
|
||||||
|
|
||||||
>>> list(our_deque) == list(deque_collections)
|
>>> list(our_deque) == list(deque_collections)
|
||||||
True
|
True
|
||||||
>>> our_popped == collections_popped
|
>>> our_popped == collections_popped
|
||||||
|
@ -302,16 +276,13 @@ class Deque:
|
||||||
"""
|
"""
|
||||||
Removes the first element of the deque and returns it.
|
Removes the first element of the deque and returns it.
|
||||||
Time complexity: O(1)
|
Time complexity: O(1)
|
||||||
|
|
||||||
@returns topop.val: the value of the node to pop.
|
@returns topop.val: the value of the node to pop.
|
||||||
|
|
||||||
>>> our_deque = Deque([15182, 1, 2, 3])
|
>>> our_deque = Deque([15182, 1, 2, 3])
|
||||||
>>> our_popped = our_deque.popleft()
|
>>> our_popped = our_deque.popleft()
|
||||||
>>> our_popped
|
>>> our_popped
|
||||||
15182
|
15182
|
||||||
>>> our_deque
|
>>> our_deque
|
||||||
[1, 2, 3]
|
[1, 2, 3]
|
||||||
|
|
||||||
>>> from collections import deque
|
>>> from collections import deque
|
||||||
>>> deque_collections = deque([15182, 1, 2, 3])
|
>>> deque_collections = deque([15182, 1, 2, 3])
|
||||||
>>> collections_popped = deque_collections.popleft()
|
>>> collections_popped = deque_collections.popleft()
|
||||||
|
@ -319,7 +290,6 @@ class Deque:
|
||||||
15182
|
15182
|
||||||
>>> deque_collections
|
>>> deque_collections
|
||||||
deque([1, 2, 3])
|
deque([1, 2, 3])
|
||||||
|
|
||||||
>>> list(our_deque) == list(deque_collections)
|
>>> list(our_deque) == list(deque_collections)
|
||||||
True
|
True
|
||||||
>>> our_popped == collections_popped
|
>>> our_popped == collections_popped
|
||||||
|
@ -340,14 +310,12 @@ class Deque:
|
||||||
"""
|
"""
|
||||||
Checks if the deque is empty.
|
Checks if the deque is empty.
|
||||||
Time complexity: O(1)
|
Time complexity: O(1)
|
||||||
|
|
||||||
>>> our_deque = Deque([1, 2, 3])
|
>>> our_deque = Deque([1, 2, 3])
|
||||||
>>> our_deque.is_empty()
|
>>> our_deque.is_empty()
|
||||||
False
|
False
|
||||||
>>> our_empty_deque = Deque()
|
>>> our_empty_deque = Deque()
|
||||||
>>> our_empty_deque.is_empty()
|
>>> our_empty_deque.is_empty()
|
||||||
True
|
True
|
||||||
|
|
||||||
>>> from collections import deque
|
>>> from collections import deque
|
||||||
>>> empty_deque_collections = deque()
|
>>> empty_deque_collections = deque()
|
||||||
>>> list(our_empty_deque) == list(empty_deque_collections)
|
>>> list(our_empty_deque) == list(empty_deque_collections)
|
||||||
|
@ -359,14 +327,12 @@ class Deque:
|
||||||
"""
|
"""
|
||||||
Implements len() function. Returns the length of the deque.
|
Implements len() function. Returns the length of the deque.
|
||||||
Time complexity: O(1)
|
Time complexity: O(1)
|
||||||
|
|
||||||
>>> our_deque = Deque([1, 2, 3])
|
>>> our_deque = Deque([1, 2, 3])
|
||||||
>>> len(our_deque)
|
>>> len(our_deque)
|
||||||
3
|
3
|
||||||
>>> our_empty_deque = Deque()
|
>>> our_empty_deque = Deque()
|
||||||
>>> len(our_empty_deque)
|
>>> len(our_empty_deque)
|
||||||
0
|
0
|
||||||
|
|
||||||
>>> from collections import deque
|
>>> from collections import deque
|
||||||
>>> deque_collections = deque([1, 2, 3])
|
>>> deque_collections = deque([1, 2, 3])
|
||||||
>>> len(deque_collections)
|
>>> len(deque_collections)
|
||||||
|
@ -379,11 +345,10 @@ class Deque:
|
||||||
"""
|
"""
|
||||||
return self._len
|
return self._len
|
||||||
|
|
||||||
def __eq__(self, other: "Deque") -> bool:
|
def __eq__(self, other: object) -> bool:
|
||||||
"""
|
"""
|
||||||
Implements "==" operator. Returns if *self* is equal to *other*.
|
Implements "==" operator. Returns if *self* is equal to *other*.
|
||||||
Time complexity: O(n)
|
Time complexity: O(n)
|
||||||
|
|
||||||
>>> our_deque_1 = Deque([1, 2, 3])
|
>>> our_deque_1 = Deque([1, 2, 3])
|
||||||
>>> our_deque_2 = Deque([1, 2, 3])
|
>>> our_deque_2 = Deque([1, 2, 3])
|
||||||
>>> our_deque_1 == our_deque_2
|
>>> our_deque_1 == our_deque_2
|
||||||
|
@ -391,7 +356,6 @@ class Deque:
|
||||||
>>> our_deque_3 = Deque([1, 2])
|
>>> our_deque_3 = Deque([1, 2])
|
||||||
>>> our_deque_1 == our_deque_3
|
>>> our_deque_1 == our_deque_3
|
||||||
False
|
False
|
||||||
|
|
||||||
>>> from collections import deque
|
>>> from collections import deque
|
||||||
>>> deque_collections_1 = deque([1, 2, 3])
|
>>> deque_collections_1 = deque([1, 2, 3])
|
||||||
>>> deque_collections_2 = deque([1, 2, 3])
|
>>> deque_collections_2 = deque([1, 2, 3])
|
||||||
|
@ -400,12 +364,15 @@ class Deque:
|
||||||
>>> deque_collections_3 = deque([1, 2])
|
>>> deque_collections_3 = deque([1, 2])
|
||||||
>>> deque_collections_1 == deque_collections_3
|
>>> deque_collections_1 == deque_collections_3
|
||||||
False
|
False
|
||||||
|
|
||||||
>>> (our_deque_1 == our_deque_2) == (deque_collections_1 == deque_collections_2)
|
>>> (our_deque_1 == our_deque_2) == (deque_collections_1 == deque_collections_2)
|
||||||
True
|
True
|
||||||
>>> (our_deque_1 == our_deque_3) == (deque_collections_1 == deque_collections_3)
|
>>> (our_deque_1 == our_deque_3) == (deque_collections_1 == deque_collections_3)
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if not isinstance(other, Deque):
|
||||||
|
return NotImplemented
|
||||||
|
|
||||||
me = self._front
|
me = self._front
|
||||||
oth = other._front
|
oth = other._front
|
||||||
|
|
||||||
|
@ -422,18 +389,16 @@ class Deque:
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def __iter__(self) -> "_Iterator":
|
def __iter__(self) -> Deque._Iterator:
|
||||||
"""
|
"""
|
||||||
Implements iteration.
|
Implements iteration.
|
||||||
Time complexity: O(1)
|
Time complexity: O(1)
|
||||||
|
|
||||||
>>> our_deque = Deque([1, 2, 3])
|
>>> our_deque = Deque([1, 2, 3])
|
||||||
>>> for v in our_deque:
|
>>> for v in our_deque:
|
||||||
... print(v)
|
... print(v)
|
||||||
1
|
1
|
||||||
2
|
2
|
||||||
3
|
3
|
||||||
|
|
||||||
>>> from collections import deque
|
>>> from collections import deque
|
||||||
>>> deque_collections = deque([1, 2, 3])
|
>>> deque_collections = deque([1, 2, 3])
|
||||||
>>> for v in deque_collections:
|
>>> for v in deque_collections:
|
||||||
|
@ -449,7 +414,6 @@ class Deque:
|
||||||
Implements representation of the deque.
|
Implements representation of the deque.
|
||||||
Represents it as a list, with its values between '[' and ']'.
|
Represents it as a list, with its values between '[' and ']'.
|
||||||
Time complexity: O(n)
|
Time complexity: O(n)
|
||||||
|
|
||||||
>>> our_deque = Deque([1, 2, 3])
|
>>> our_deque = Deque([1, 2, 3])
|
||||||
>>> our_deque
|
>>> our_deque
|
||||||
[1, 2, 3]
|
[1, 2, 3]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user