from math import sqrt # needed for computing distances class Point: def __init__(self, dimension, coords=None): if coords is None: self._coords = [0] * dimension # all zeros else: self._coords = list(coords) def __getitem__(self, index): return self._coords[index] def __setitem__(self, index, value): self._coords[index] = value def __len__(self): return len(self._coords) def distance(self, other): total = 0 for k in range(len(self._coords)): d = self._coords[k] - other._coords[k] total += d*d return sqrt(total) def normalize(self): mag = self.distance( Point(len(self._coords)) ) if mag > 0: self.scale(1/mag) def __str__(self): return '< ' + ','.join([str(c) for c in self._coords]) + ' >' def __mul__(self, operand): dim = len(self._coords) if isinstance(operand, (int,float)): # multiply by constant duplicate = Point(dim, self._coords) duplicate.scale(operand) return duplicate elif isinstance(operand, Point): # dot product return sum([self._coords[k] * operand._coords[k] for k in range(dim)]) def __rmul__(self, operand): return self * operand #----------- your part follows ------------ def scale(self, factor): # not yet implemented... pass def __add__(self, other): # not yet implemented pass