0

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.

1
Contributor
2
Replies
3
Views
5 Years
Discussion Span
Last Post by pwolf
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?

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'
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.