mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 08:17:01 +00:00
compare-method added to Vector class in lib.py (#12448)
* compare-method added to Vector class in lib.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Updated lib.py with suggestions * Updated lib.py with suggestions * Updated lib.py with __eq__ method --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
1909f2272f
commit
2b58ab0402
|
@ -46,7 +46,6 @@ class Vector:
|
|||
change_component(pos: int, value: float): changes specified component
|
||||
euclidean_length(): returns the euclidean length of the vector
|
||||
angle(other: Vector, deg: bool): returns the angle between two vectors
|
||||
TODO: compare-operator
|
||||
"""
|
||||
|
||||
def __init__(self, components: Collection[float] | None = None) -> None:
|
||||
|
@ -96,6 +95,16 @@ class Vector:
|
|||
else: # error case
|
||||
raise Exception("must have the same size")
|
||||
|
||||
def __eq__(self, other: object) -> bool:
|
||||
"""
|
||||
performs the comparison between two vectors
|
||||
"""
|
||||
if not isinstance(other, Vector):
|
||||
return NotImplemented
|
||||
if len(self) != len(other):
|
||||
return False
|
||||
return all(self.component(i) == other.component(i) for i in range(len(self)))
|
||||
|
||||
@overload
|
||||
def __mul__(self, other: float) -> Vector: ...
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user