Answer to Question #302153 in Python for sai

Question #302153

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

1
Expert's answer
2022-02-24T08:06:40-0500
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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS