In this session, we try to understand sequence types in python such as lists, tuples, sets, string etc. and also understand custorm sequence stypes
All sequences are iterables but not all iterables are not sequences in python, for example we cannot access elements by index for a set in python
Sequence types support operators like in, not in, range etc. and also support comcatenate operations.
A regular strictly convex polygon is a polygon that has the following characteristics:
- all interior angles are less than 180
- all sides have equal length
Objective 1:
- Create a Polygon Class:
- where initializer takes in:
- number of edges/vertices
- circumradius
- that can provide these properties:
- # edges
- # vertices
- interior angle
- edge length
- apothem
- area
- perimeter
- that has these functionalities:
- a proper
__repr__
function - implements equality (==) based on # vertices and circumradius (
__eq__
) - implements > based on number of vertices only (
__gt__
)
- a proper
- where initializer takes in:
Objective 2:
- Implement a Custom Polygon sequence type:
- where initializer takes in:
- number of vertices for largest polygon in the sequence
- common circumradius for all polygons
- that can provide these properties:
- max efficiency polygon: returns the Polygon with the highest area: perimeter ratio for edges(n) = 25
- that has these functionalities:
- functions as a sequence type (
__getitem__
) - supports the len() function (
__len__
) - has a proper representation (
__repr__
)
- functions as a sequence type (
- where initializer takes in:
The class is the implementation of the required polygon class. The usage is demonstrated below.
from polygon import Polygon
p1 = Polygon(12, 20) # No. of vertices/edges: 12, circumradius: 20
p2 = Polygon(12, 20) # No. of vertices/edges: 12, circumradius: 20
p3 = Polygon(15, 25) # No. of vertices/edges: 15, circumradius: 25
The class is the implementation of a custom polygon sequence according to what is described in the objective. The usage is as follows:
from polysequence import PolySeq
seq = PolySeq(25, 10) # Max edge count: 25, common circumradius: 10
The proper demonstration of the working and features can be found in the jupyter notebook.