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:
Andrwaa 2024-12-28 12:17:48 +01:00 committed by GitHub
parent 1909f2272f
commit 2b58ab0402
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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: ...