Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Challenges Facing Todays Organizations?

Answered: 1 week ago