128
5 Some More Python Essentials
Filename: total_volume_boxes.py.
Exercise 5.6: Area of a Polygon
One of the most important mathematical problems through all times has been to
find the area of a polygon, especially because real estate areas often had the shape
of polygons, and it was necessary to pay tax for the area. We have a polygon as
depicted below.
The vertices (“corners”) of the polygon have coordinates (x 1 , y 1 ), (x 2 , y 2 ), . . .,
(x n , y n ), numbered either in a clockwise or counter clockwise fashion. The area A of
the polygon can amazingly be computed by just knowing the boundary coordinates:
A =
1
2
(x 1 y 2 + x 2 y 3 + · · · + x n−1 y n + x n y 1 ) − (y 1 x 2 + y 2 x 3 + · · · + y n−1 x n + y n x 1 )
.
Write a function polyarea(x, y) that takes two coordinate arrays with the vertices
as arguments and returns the area.
Test the function on a triangle, a quadrilateral, and a pentagon where you can
calculate the area by alternative methods for comparison.
Hint Since Python lists and arrays have 0 as their first index, it is wise to rewrite the
mathematical formula in terms of vertex coordinates numbered as x 0 , x 1 , . . . , x n−1
and y 0 , y 1 , . . . , y n−1 .
Filename: polyarea.py.
Exercise 5.7: Count Occurrences of a String in a String
In the analysis of genes one encounters many problem settings involving searching
for certain combinations of letters in a long string. For example, we may have a
string like
gene = ’AGTCAATGGAATAGGCCAAGCGAATATTTGGGCTACCA’
We may traverse this string, letter by letter, by the for loop for letter in gene.
The length of the string is given by len(gene), so an alternative traversal over
an index i is for i in range(len(gene)). Letter number i is reached through
5 Some More Python Essentials
Filename: total_volume_boxes.py.
Exercise 5.6: Area of a Polygon
One of the most important mathematical problems through all times has been to
find the area of a polygon, especially because real estate areas often had the shape
of polygons, and it was necessary to pay tax for the area. We have a polygon as
depicted below.
The vertices (“corners”) of the polygon have coordinates (x 1 , y 1 ), (x 2 , y 2 ), . . .,
(x n , y n ), numbered either in a clockwise or counter clockwise fashion. The area A of
the polygon can amazingly be computed by just knowing the boundary coordinates:
A =
1
2
(x 1 y 2 + x 2 y 3 + · · · + x n−1 y n + x n y 1 ) − (y 1 x 2 + y 2 x 3 + · · · + y n−1 x n + y n x 1 )
.
Write a function polyarea(x, y) that takes two coordinate arrays with the vertices
as arguments and returns the area.
Test the function on a triangle, a quadrilateral, and a pentagon where you can
calculate the area by alternative methods for comparison.
Hint Since Python lists and arrays have 0 as their first index, it is wise to rewrite the
mathematical formula in terms of vertex coordinates numbered as x 0 , x 1 , . . . , x n−1
and y 0 , y 1 , . . . , y n−1 .
Filename: polyarea.py.
Exercise 5.7: Count Occurrences of a String in a String
In the analysis of genes one encounters many problem settings involving searching
for certain combinations of letters in a long string. For example, we may have a
string like
gene = ’AGTCAATGGAATAGGCCAAGCGAATATTTGGGCTACCA’
We may traverse this string, letter by letter, by the for loop for letter in gene.
The length of the string is given by len(gene), so an alternative traversal over
an index i is for i in range(len(gene)). Letter number i is reached through
