# Compute the Perimeter of a Polygon – HackerRank Solution

In this post, we will solve Compute the Perimeter of a Polygon HackerRank Solution. This problem (Compute the Perimeter of a Polygon) is a part of HackerRank Functional Programming series.

You are given the cartesian coordinates of a set of points in aÂ 2DÂ plane. When traversed sequentially, these points form a Polygon,Â P, which is not self-intersecting in nature. Can you compute the perimeter of polygonÂ P?

## Input Format

The first line contains an integer,Â N, denoting the number of points.
TheÂ NÂ subsequent lines each containÂ 2Â space-separated integers denoting the respectiveÂ xÂ andÂ yÂ coordinates of a point.

## Constraints

• NoÂ 2Â points areÂ coincident, and polygonÂ PÂ is obtained by traversing the points in a clockwise direction.
• 3 <= N <= 1000
• 0 <= x, y <= 1000

## Output Format

For each test case, print the perimeter ofÂ PÂ (correct to a scale of one decimal place).

Sample Input

``````4
0 0
0 1
1 1
1 0``````

Sample Output

``4``

Explanation

The given polygon is a square, and each of its sides areÂ 1Â unit in length.Â perimeter(P) = 1 + 1 + 1 + 1 = 4, so we printÂ 4Â on a new line.

## Solution – Compute the Perimeter of a Polygon – HackerRank Solution

```d [x,y,x1,y1] = sqrt \$ (x1 - x)^2 + (y1 - y)^2
d _ = 0

p [] acc = acc
p points acc = p (drop 2 points) (acc + (d (take 4 points)))

perimeter points = p (points ++ (take 2 points)) 0

main :: IO ()
main = do
_ <- getLine
x <- getContents
let f = map read \$ words x
print \$ perimeter f
```

Note: This problem (Compute the Perimeter of a Polygon) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.