-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector.py
More file actions
38 lines (29 loc) · 989 Bytes
/
vector.py
File metadata and controls
38 lines (29 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from decimal import Decimal
class Vector(object):
def __init__(self, coordinates):
try:
if not coordinates:
raise ValueError
self.coordinates = tuple(coordinates)
self.dimension = len(coordinates)
self.idx = 0
except ValueError:
raise ValueError('The coordinates must be nonempty')
except TypeError:
raise TypeError('The coordinates must be an iterable')
def __iter__(self):
self.idx = 0
return self
def __next__(self):
self.idx += 1
try:
return Decimal(self.coordinates[self.idx-1])
except IndexError:
self.idx = 0
raise StopIteration # Done iterating.
def __getitem__(self,index):
return Decimal(self.coordinates[index])
def __str__(self):
return 'Vector: {}'.format(self.coordinates)
def __eq__(self, v):
return self.coordinates == v.coordinates