David Holland
David Holland

Reputation: 7

Write a function that takes as input a 2D array and RETURNS 'Square' , 'Rectangular' or 'Invalid'

Write a 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. Rectangular matrices can have different numbers of rows and columns, but each row is the same length. Invalid matrices have inconsistent length in their rows. Now all square matrices are also rectangles so I know this should be a subset.

I can tell essentially if my matrix is a square or invalid. My code returns "Invalid" for rectangles as well. I'm not sure how to define a column and how to calculate whether something is a square or a rectangle.

def describe(matrix):
        if all([len(i)==len(matrix) for i in matrix]):
            return "Square"
        else:
            return "Invalid"
>>> describe([[0]])  
'Square'  
>>> describe([[0, 1, 2], [2, 1, 0], [3, 3, 3]])  
'Square'  
>>> describe([[0, 1, 2], [2, 1], [3, 3, 3]])  
'Invalid'  
>>> describe([[0, 1], [1, 0], [3, 3]])  
'Rectangular'  
>>> describe([[0, 1], [1, 0], [3, 3]])  
'Rectangular'  
>>> describe([[0, 1, 2, 3, 4, 5]])  
'Rectangular'  
>>> describe([[0, 1, 2, 3, 4, 5], [5, 4, 3, 2, 1, 0]])  
'Rectangular'  

Upvotes: 0

Views: 935

Answers (4)

Mihir Patel
Mihir Patel

Reputation: 11

You can first check whether all rows are of same length or not and then decide whether it is square or rectangle.

def describe(matrix):
    len_row = len(matrix[0])
    if all([len(i) == len_row for i in matrix]):
       if all([len(i)==len(matrix) for i in matrix]):
          return "Square"
       else:
          return "Rectangle"
    else:
        return "Invalid"

Upvotes: 0

ngShravil.py
ngShravil.py

Reputation: 5048

The following code could help:

def describe(matrix):
    if all([len(i)==len(matrix) for i in matrix]):
        return "Square"
    elif len(matrix)!=len(matrix[0]) and all([len(i)==len(matrix[0]) for i in matrix]):
        return "Rectangle"
    else:
        return "Invalid"

Upvotes: 0

meTchaikovsky
meTchaikovsky

Reputation: 7666

You can try this code

def describe(tmp):
    lengths = [len(item) for item in tmp]

    if len(set(lengths)) > 1: 
        return 'Invalid'
    else:
        if len(lengths) == lengths[0]:
            return 'Squrare'
        else:
            return 'Rectangular'  

Upvotes: 0

Serge Ballesta
Serge Ballesta

Reputation: 148880

You should reverse your thoughts. Instead of comparing the length of each row to the number of rows, you should compare the length of all rows but the first to the length of the first row. If all match, then you have at least a Rectangle else Invalid. And if furthemore the length of the first row (which is here the length of the other rows) is equal to the number of rows, then you have a Square.

Just beware with the corner cases (empty array and single row)...

The python code is left as an exercise :-)

Upvotes: 2

Related Questions