﻿ Python: Read the mass data and find the number of islands - w3resource # Python: Read the mass data and find the number of islands

## Python Basic - 1: Exercise-57 with Solution

There are 10 vertical and horizontal squares on a plane. Each square is painted blue and green. Blue represents the sea, and green represents the land. When two green squares are in contact with the top and bottom, or right and left, they are said to be ground. The area created by only one green square is called "island". For example, there are five islands in the figure below.
Write a Python program to read the mass data and find the number of islands.

Input:
A single data set is represented by 10 rows of 10 numbers representing green squares as 1 and blue squares as zeros.
Output:
Output AB and CD are not orthogonal! or AB and CD are orthogonal!.

Pictorial Presentation: Sample Solution:

Python Code:

``````c=0
def f(x,y,z):
if 0<=y<10 and 0<=z<10 and x[z][y]=='1':
x[z][y]='0'
for dy,dz in [[-1,0],[1,0],[0,-1],[0,1]]:f(x,y+dy,z+dz)
print("Input 10 rows of 10 numbers representing green squares (island) as 1 and blue squares (sea) as zeros")
while 1:
try:
if c:input()
except:break
x = [list(input()) for _ in *10]
c=1;b=0
for i in range(10):
for j in range(10):
if x[j][i]=='1':
b+=1;f(x,i,j)
print("Number of islands:")
print(b)
``````

Sample Output:

```Input 10 rows of 10 numbers representing green squares (island) as 1 and blue squares (sea) as zeros
1100000111
1000000111
0000000111
0010001000
0000011100
0000111110
0001111111
1000111110
1100011100
1110001000
Number of islands:
5
```

Flowchart: Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Returns the symmetric difference between two lists, after applying the provided function to each list element of both

Example:

```def tips_symmetric_difference_by(p, q, fn):
_p, _q = set(map(fn, p)), set(map(fn, q))
return [item for item in p if fn(item) not in _q] + [item for item in q if fn(item) not in _p]
from math import floor
print(tips_symmetric_difference_by([4.2, 2.4], [4.6, 6.8],floor))
```

Output:

```[2.4, 6.8]
```