# Number of Islands — Day 5(Python)

Today we would be looking at one of the interviewers’ favorite questions. A frequently asked question in technical coding interviews. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid ...

Today we would be looking at one of the interviewers’ favorite questions. A frequently asked question in technical coding interviews. The question tests your understanding of Breadth-First-Search and Depth-First-Search and, I will solve the problem using both methods.

200. Number of Islands

Given a 2d grid map of `'1'`s (land) and `'0'`s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

``````Input: grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
Output: 1``````

Example 2:

``````Input: grid = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
Output: 3``````

An island is formed by connecting adjacent lands horizontally or vertically and is surrounded by water along the borders. According to the question, “0” signifies water, while the land signifies “1”.

If we consider the matrix as a collection of connected graphs, we might be able to come up with a solution. Let “1” be a node in the graph, now count the number of connected graphs. The resulting number will be our answer.

Let me convert the matrix to a graph.

``````Input: grid = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
Output: 3``````

Graph representation of the matrix

Now, count the number of connected graphs.

To find the number of islands, how about we use the same logic here?

Depth-First-Search

Depth-First-Search is a graph traversing algorithm that starts from the root and traverses as far as possible. Some of the application DFS can be to check the presence of a node in a graph, check for cycles in the graph. Let us see how DFS can be used to solve the given problem.

## Python Tricks Every Developer Should Know

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

## How to Remove all Duplicate Files on your Drive via Python

Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.

## Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

## How To Compare Tesla and Ford Company By Using Magic Methods in Python

Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc.. You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like __init__, __call__, __str__ etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).

## Consistent Custom Exception Classes in Python

Having to handle exceptions is common in Python and so is having to define your own. Yet, I have seen competing ways of doing so in various projects. The inconsistency comes from Exceptions being something that can easily be subclassed and extended, but also something that can be easily instantiated and used in their base form.