def test_simple_add(): v1 = Vector([0, 0, 1]) v2 = Vector([1, 0, 0]) assert v1 + v2 == Vector([1, 0, 1]) def test_simple_mul(): v1 = Vector([0, 0, 1]) assert v1 * 5 == Vector([0, 0, 5]) assert 5 * v1 == Vector([0, 0, 5]) class Vector(object): def __init__(self, components): self.components = components def _check_lengths_compatible(self, other): if len(self.components) != len(other.components): raise ValueError("incompatible lenghts") def __add__(self, other): self._check_lengths_compatible(other) result = [] for a, b in zip(self.components, other.components): result.append(a + b) return Vector(result) def __mul__(self, number): result = [] for a in self.components: result.append(a * number) return Vector(result) __rmul__ = __mul__ def __eq__(self, other): self._check_lengths_compatible(other) return self.components == other.components def __ne__(self, other): return not (self == other) def __repr__(self): return "Vector(%s)" % (self.components, )