function to determine matrix dimensions, not sure if i made an error..
Is there something wrong with this code i wrote? i get the feeling i made a mistake and cant quite recall what, its quite frustrating.
The function is supposed to determine the dimensions of a matrix and if it is, or is not, a valid matrix ( for example, if a row in the matrix isn't equal to another, and such)
def matrixDimensions(m):
z = []
ms = len(str(m).split('[')) - 2
for x in range(ms):
z.append(len(m[x]))
if z[0] == z[1]:
return "This is a %dx%d matrix." % ((len(z)),z[0])
else:
return "This is not a valid matrix."
im sure i spotted an error but i cant recall it. It works when i tested it but i think there is an error the tests i used wouldn't catch.
Please help, would me much appreciated.
pwolf
Junior Poster in Training
71 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
i think i spotted the error, or at least, an error.
z could be any length, so i am working to change the code, so far i got
def ma(m):
z = []
test = []
ms = len(str(m).split('[')) - 2
for x in range(ms):
z.append(len(m[x]))
for y in range(len(z)):
test.append(len(m[y]))
print test
now im trying to think of a way to compare each value in test.im thinking another for loop.
Is there an easier and cleaner way to do this?
pwolf
Junior Poster in Training
71 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
okay, i think i got it, but its really long. Is there a better way to do this?
def ma(m):
z = []
test = []
last = []
ms = len(str(m).split('[')) - 2
for x in range(ms):
z.append(len(m[x]))
for y in range(len(z)):
test.append(len(m[y]))
for t in range(len(test)):
try:
if test[t] == test[t+1]:
last.append('yes')
else:
last.append('no')
except IndexError:
pass
if 'no' in last:
print 'nope'
else:
print 'yeah'
pwolf
Junior Poster in Training
71 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0