We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c21af30 commit 6d366bdCopy full SHA for 6d366bd
exercises/vector/vector_repr.py
@@ -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