Skip to content

Commit 6d366bd

Browse files
committed
exercicio vector repr
1 parent c21af30 commit 6d366bd

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

exercises/vector/vector_repr.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
A multi-dimensional ``Vector`` class with ``repr``
3+
4+
>>> v1 = Vector([1, 2, 3])
5+
>>> print(v1)
6+
(1.0 2.0 3.0)
7+
>>> v1
8+
Vector([1.0, 2.0, 3.0])
9+
10+
"""
11+
12+
from array import array
13+
import math
14+
import reprlib
15+
16+
17+
class Vector:
18+
typecode = 'd'
19+
20+
def __init__(self, components):
21+
self._components = array(self.typecode, components)
22+
23+
def __len__(self):
24+
return len(self._components)
25+
26+
def __iter__(self):
27+
return iter(self._components)
28+
29+
def __abs__(self):
30+
return math.sqrt(sum(x * x for x in self))
31+
32+
def __eq__(self, other):
33+
return (len(self) == len(other) and
34+
all(a == b for a, b in zip(self, other)))

0 commit comments

Comments
 (0)