Exercise: Vectors
Time to put the special methods together.
Exercise: finish the 2D Vector class.
repr(Vector(1, 2))is"Vector(1, 2)".v + wadds the parts and returns a newVector.v * 3and3 * vscale both parts. Multiplying by anything that isn't anintorfloatshould give aTypeError.v == wisTruewhen both parts match. Comparing with a non-Vector, like a tuple, isFalse.abs(v)is the length.math.hypot(x, y)computes it.
Return NotImplemented for types you don't handle, and let Python raise the
TypeError for you.
Extra, not tested: why can't you put a Vector in a set now? What would you
change to allow it safely?
Press Check to run the tests.