Find perimeter of a matrix
Input1:
1 2 3
4 5 6
7 8 9
Output: 40
Input2:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output: 102
import numpy as np
def GetPerimeter(I):
Input = np.array(I)
row, col = Input.shape
P=0
for r in range(0,row): P = P + Input[r][0]
for c in range(1,col): P = P + Input[row-1][c]
for c in range(1,col): P = P + Input[0][c]
for r in range(1,row-1): P = P + Input[r][col-1]
return(P)
#Find perimeter of a matrix
Input = [
[1,2,3],
[4,5,6],
[7,8,9]
]
Perim = GetPerimeter(Input)
print("Input-1",Input)
print("Perimeter = ",Perim)
Input = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]
]
Perim = GetPerimeter(Input)
print("Input-1",Input)
print("Perimeter = ",Perim)
Comments
Leave a comment