# 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.

1100000111

1000000111

0000000111

0010001000

0000011100

0000111110

0001111111

1000111110

1100011100

1110001000

Number of islands:

5

**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 [0]*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.**

**Previous:** Write a Python program to sum of all numerical values (positive integers) embedded in a sentence.

**Next:** Write a Python program to restore the original string by entering the compressed string with this rule. However, the # character does not appear in the restored character string.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

**Weekly Trends**- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join