Skip to content

Commit 2b58ab0

Browse files
compare-method added to Vector class in lib.py (TheAlgorithms#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>
1 parent 1909f22 commit 2b58ab0

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

linear_algebra/src/lib.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class Vector:
4646
change_component(pos: int, value: float): changes specified component
4747
euclidean_length(): returns the euclidean length of the vector
4848
angle(other: Vector, deg: bool): returns the angle between two vectors
49-
TODO: compare-operator
5049
"""
5150

5251
def __init__(self, components: Collection[float] | None = None) -> None:
@@ -96,6 +95,16 @@ def __sub__(self, other: Vector) -> Vector:
9695
else: # error case
9796
raise Exception("must have the same size")
9897

98+
def __eq__(self, other: object) -> bool:
99+
"""
100+
performs the comparison between two vectors
101+
"""
102+
if not isinstance(other, Vector):
103+
return NotImplemented
104+
if len(self) != len(other):
105+
return False
106+
return all(self.component(i) == other.component(i) for i in range(len(self)))
107+
99108
@overload
100109
def __mul__(self, other: float) -> Vector: ...
101110

0 commit comments

Comments
 (0)