I want to test a 2x2 matrix of [[5,6],[7,8]] to see if it's a square.
I run my code and I'm supposed to get True but I got False instead...
def square(sq):
for element in sq:
if element:
return False
return True
given m
is a numpy
matrix and you have imported numpy
def square(m):
return m.shape[0] == m.shape[1]
If you want to check whether a matrix is NxN, you can use:
def isSquare (m): return all (len (row) == len (m) for row in m)
As you said in your comment: if the length of all rows equals the number of rows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With