A classic compass rose is divided into 360°. The four cardinal directions are on the main axes, and are respectively called north, east, south and west (see figure).
These cardinal directions cover areas of ±45° around the main axes (see tabel). They are simply used to indicate a well-defined direction anywhere in the world. North and south face their respective poles near the ends of the Earth axis. The rotation of the Earth around that axis is used to define the eastward and westward directions.
direction | area |
---|---|
north | [0°,45°[ and [315°,360°[ |
east | [45°,135°[ |
south | [135°,225°[ |
west | [225°,315°[ |
Define a class Compass that can be used to create instances of compasses. The needle of the compass gives the angle $$a \in {\mathbb{Z}}$$ (expressed in degrees), where $$0 \leq a < 360$$. The objects of the class Compass must at least support the following methods:
An initialization method that has an optional parameter that takes an angle $$a \in {\mathbb{Z}}$$ (expressed in degrees). If no angle is passed, it is assumed that the compass is aligned so that its needle points to the north.
A method __repr__ that returns a string representation of the object, corresponding to the Python expression that can be used to create a similar object. The string representation always needs to explicitly mention the angle $$a \in {\mathbb{N}}$$, and it must hold that $$0 \leq a < 360$$.
A method __str__ that returns a string representation of the object in the format a° (direction). In this, $$a \in {\mathbb{N}}$$ indicates the angle the compass points to, where $$0 \leq a < 360$$, and direction is the corresponding cardinal direction.
A method turn that takes an angle $$a \in {\mathbb{Z}}$$ (expressed in degrees). If this method is called, the compass must be rotated over the given angle.
A method direction that returns a string corresponding to the cardinal direction pointed to by the compass.
>>> compass = Compass()
>>> compass
Compass(0)
>>> compass.direction()
'north'
>>> print(compass)
0° (north)e
>>> compass.turn(222)
>>> compass
Compass(222)
>>> compass.direction()
'south'
>>> print(compass)
222° (south)
>>> compass.turn(157)
>>> compass
Compass(19)
>>> compass.direction()
'north'
>>> print(compass)
19° (north)