avatar Bite 370. Getting started with vectors

In the previous two Bites you have annotated small pieces of code with type hints. In this Bite, instead of dealing with isolated and (sometimes) less meaningful chunks of code, you will start working on a real, functional code base for some cool linear algebra (don't show your excitement that much!).

If you are new to typing / type annotations, maybe you want to read the Pybites article series about typing first or in an addition to this learning path. Of course, you can always ask the official documentation for help, though. There is the documentation of the typing module (be aware of the correct Python version that you are using!) as well as a lot of Python Enhancement Proposals (PEP), also linked in the module's documentation. Given that the official documentation, and PEPs in particular, are often not the easiest reading material, I found the mypy documentation a very good starting place.

The Vector class

You do not have to understand every implementation detail! However, if you want to refresh your knowledge about linear algebra, have a look at Basics of Linear Algebra. There you can learn about how to do things properly with numpy, too.

The following REPL example will show you examples of how you can use the Vector class that you are expected to annotate.

v = Vector(1, 1, 1)
print(v)
>>> [1 1 1]
print(v.__repr__())
>>> Vector(x=1, y=1, z=1)
print(len(v))
>>> 3
print(v.x, v.y, v.z)
>>> 1 1 1 
print(v[0], v[1], v[2])
>>> 1 1 1
print(v())
>>> (1, 1, 1)
print(v.norm(1), v.norm(2), v.norm(3))
>>> 3.0 1.7320508075688772 1.4422495703074083

w = Vector.from_list([1, 2, 3])
print(w)
>>> [1 2 3]

print(v + w)
>>> [2 3 4]
print(w + v)
>>> [2 3 4]
print(w - v)
>>> [0 1 2]

print(v * 2)
>>> [2.0 2.0 2.0]
print(2 * v)
>>> [2.0 2.0 2.0]
print(v * 0.5)
>>> [0.5 0.5 0.5]
print(v * w)
>>> 6
print(w * v)
>>> 6

print(v.iscollinear(w))
>>> False
print(v.iscollinear(2 * v))
>>> True

print(Vector(10, 9, 3).angle(Vector(2, 5, 12)))
>>> 0.979924710443726

v = Vector(0, 3, 2)
w = Vector(4, 1, 1)
u = Vector(0, -2, 0)
x = 3 * v - 2 * w + 4 * u
print(x)
>>> [-8.0 -1.0 4.0]

Your Task

In this Bite, you have to add type hints to all methods of the Vector class. Here are a few marks:

- When you need the type of the class itself, you can use a quoted type, see Forward references. There are other possibilities (for example Self with 3.11), but we are just getting started, aren't we.

- The Vector class will work both with integers and floats. Does that mean you always have to annotate both types? You don't, thanks to the numeric tower. In short: Although int is not a subclass of float, the type float also accepts the type int. So just stick with float for now.

- Please annotate all methods, dunder methods included.

- The dunder method __mul__ can return two different things, depending on the passed attribute's type. You will learn about a better way to do this, but for now, stick with typing.Union.

Noteplease be patient if the platform does not instantly show the test results, the last final test (test_mypy_report) takes at least 10 seconds to run. However, in return we are not limited to checking concrete types, allowing multiple options in your code submissions. Thanks. 

Login and get coding
go back Intermediate level
Bitecoin 3X

15 out of 16 users completed this Bite.
Will you be Pythonista #16 to crack this Bite?
Resolution time: ~44 min. (avg. submissions of 5-240 min.)

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help