Question: please modify to accommodate Write a python function describe(matrix) that takes as input a 2D array and RETURNS 'Square' if the matrix is square, 'Rectangular'
please modify to accommodate
Write a python function describe(matrix) that takes as input a 2D array and RETURNS 'Square' if the matrix is square, 'Rectangular' if the matrix is rectangular, and 'Invalid' if neither.
Square matrices have the same number of rows and columns. This is a 5x5 square matrix:
Rectangular matrices can have different numbers of rows and columns, but each row is the same length. This is a 2x5 rectangular matrix:
Invalid matrices have an inconsistent length in their rows. This is an invalid matrix:
1 2 3 4 5
5 4 3 2 1 0
8 6 2 9 1
0 8 4 2 4 7
NOTE: All square matrices are also rectangular -- only RETURN 'Square' if it is square.
def describe(matrix):
rows = len(matrix)
cols = len(matrix[0]) #check for square matrix
if rows == cols:
Rectangular matrices can have different numbers of rows and columns, but each row is the same length. This is a 2x5 rectangular matrix:
return "Square"
#check for invalid matrix
for i in range(len(matrix)):
if cols != len(matrix[i]):
return "Invalid"
#if it is not square and not invalid, then it is rectangle
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
