An island is a piece of land that is surrounded on all sides by water. When a piece of land is not an island, because it is joined to the continent (small area compared to the size of the land), it is called a peninsula. In all other cases it is said that the land is part of the mainland.
We'll work with text files, each containing a detail of a map. This detailed map is described on the basis of a number of text lines that all have the same length. The map contains a continental landmass of which pieces are marked with a hash (#) and a second land mass of which the parts are designated by the letter S. Pieces of sea are indicated by a space. Asked is to determine whether the land mass that is designated by the letters S is an island, a peninsula or part of the continent. To do this, just follow these steps:
Write a function landmass that takes the location (str) of a text file containing a detailed map as described above. The function must return a tuple $$(r,o)$$, where $$r$$ (int) represents the number of touch points the mainland has with the land mass designated by the letters S. This is calculated as the number of pieces marked with a hash that touch at least one piece marked with the letter S above, below, left or right. The value $$o$$ (int) represents the surface of the landmass that is designated by the letters S, expressed as the total number of pieces that are marked with the letter S.
Use the function landmass to write a function landtype that can be used to determine if a land mass is an island, a peninsula or a part of the mainland. This function takes the location (str) of a text file containing a detailed map as described above. The function also has a second optional parameter ratio that may take a number (float; default value: 0.05). If the landmass designated by the letters S has no points of contact with the mainland, the function must return a string (str) with the text island. Otherwise, it must determine whether the ratio of the number of touch points divided by the area of the land is less than or equal to the value of the parameter ratio. If that is the case, a string (str) with the text peninsula must be returned. Otherwise, a string (str) with the text mainland must be returned.
In the following interactive session we assume that the files landmass1.txt1, landmass2.txt2 and landmass3.txt3 are in the current directory.
>>> landmass('landmass1.txt4')
(0, 196)
>>> landmass('landmass2.txt5')
(6, 169)
>>> landmass('landmass3.txt6')
(8, 45)
>>> landtype('landmass1.txt7')
'island'
>>> landtype('landmass2.txt8')
'peninsula'
>>> landtype('landmass3.txt9')
'mainland'
>>> landtype('landmass3.txt10', ratio=0.2)
'peninsula'