5 Replies - 2692 Views - Last Post: 22 February 2012 - 04:55 AM Rate Topic: -----

#1 jone kim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 07-January 10

returning the values of row & column from list

Posted 19 February 2012 - 12:45 AM

Suppose I give user i/p as 22:

"""checking for an user input if it is present in the 4*4 matrixM"""

def checkvalue(number,matrixM):
		
	for row in range(3):
		print('\n')
		if (matrixM[row+1][0] == number):
			print(matrixM[0][3])
			return row,0
			
		
			
		if(matrixM[row+1][0] < number):
			
			for column in range(3):
				if(matrixM[row][column] == number):
					return row, column 
					
				
def main():
	matrix = [[2,4,6,8],[10,12,14,16],[20,22,24,26],[28,30,32,34]]
	row1=0
	column1=0	
	n = int(input(('provide the value of n: ')))
	#checkvalue = n,matrix
	row1,column1 = checkvalue(n,matrix) ###line 49
	print('\n')
	print(row1,column1)
	return 0

if __name__ == '__main__':
	main()     ##line 55




The errors are:

Quote

provide the value of n: 22

Traceback (most recent call last):
File "pp_Search In An Ascending Matrix.py", line 55, in <module>
main()
File "pp_Search In An Ascending Matrix.py", line 49, in main
row1,column1 = checkvalue(n,matrix)
TypeError: 'NoneType' object is not iterable


Why I am getting this errors? How can I solve this?

Is This A Good Question/Topic? 0
  • +

Replies To: returning the values of row & column from list

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5746
  • View blog
  • Posts: 12,554
  • Joined: 16-October 07

Re: returning the values of row & column from list

Posted 19 February 2012 - 05:21 AM

Your checkvalue has failed to address all return paths. What if you get to the end without returning anything?

So:
def checkvalue(number,matrixM):
	for row in range(3):
		# do all your stuff
	# you didn't expect to get here, but you did
	# return something
	return -1, -1



Now, to the code itself. Why have you hard coded range(3) when you are supposed to be able to handle matrixM? Out of curiosity, what is checkvalue supposed to do?
Was This Post Helpful? 1
  • +
  • -

#3 jone kim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 07-January 10

Re: returning the values of row & column from list

Posted 19 February 2012 - 09:47 PM

Thanks baavgai for the reply.

I want to explain my code to you.

1. this code is to search for the given number whether it is present in the list or not.
2. first if statement returns the value of row if the i/p number is in the first column

3. if the given number is not in the first column, the second if statement compares the i/p number with the values in the first column of different rows.

4. if the i/p number is less than the value of matrixM[row+1][0], it goes to
matrixM[row][0] i.e. previous row to search the i/p number and return index.

Suppose the i/p number is 22, since it is not in the first column, the second if statement is executed.
when row = 2, row+1 = 3 =>matrixM[3][0] == 28, since 22<28, it should go to the third row and search for 22 & return the value of row and column.

I've modified the code but did not get the result. Can you help me to solve this problem.
def checkvalue(number,matrixM):
	
	for row in range(len(matrixM)):
		if (matrixM[row][0] == number):
			return row,0
		
		elif(matrixM[row+1][0] < number):
			for column in range(len(matrixM)):
				if(matrixM[row][column] == number):
					return row, column
		
def main():
	matrix = [[2,4,6,8],[10,12,14,16],[20,22,24,26],[28,30,32,34]]
	row1=0
	column1=0	
	n = int(input(('provide the value of n: ')))
	row1,column1 = checkvalue(n,matrix)
	print('\n')
	print(row1,column1)
	return 0

if __name__ == '__main__':
	main()



Why this code is showing error in main()?
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5746
  • View blog
  • Posts: 12,554
  • Joined: 16-October 07

Re: returning the values of row & column from list

Posted 20 February 2012 - 06:59 AM

Thank you for the explanation. Frankly, I'm still not sure what you're thinking, but it's interesting.

I ran your code and got:
Result:
Traceback (most recent call last):
  File "t.py", line 25, in <module>
    main()
  File "t.py", line 22, in main
    test(22)
  File "t.py", line 17, in test
    row1,column1 = checkvalue(n,matrix)
  File "t.py", line 6, in checkvalue
    if(matrixM[row+1][0] < number):
IndexError: list index out of range
>Exit code: 1



This makes sense, looking at the code... Sorry, rather than focus there, let's look at what you want.

It sounds like you're just looking for the location of the value. ( I don't get that from your code. )

If that is the case, I'd write the code like so:
def checkvalue(number,m):
	for row in range(len(m)):
		for col in range(len(m[row])):
			if(m[row][col] == number):
				return row, col
	# you got here, not found
	return -1, -1




Wait! Are you trying to take advantage of the order of the list??? Ooo, fun. In that case...

Ok, your code makes more sense now. You're still getting hung up on the row +1 -1 thing. I'd do it like this:
def checkvalue(number,rows):
	row = 0
	# we skip the first row here
	# if it is the very first number
	# we'll find it in the search pass
	for i in range(1, len(rows)):
		if number==rows[i][0]:
			return i, 0
		
		if number<rows[i][0]:
			# it must be in the prior row
			row = i-1 # record prior row value
			break # leave the loop
		
		# record the current row we're on
		# mostly useful for the last row
		row = i 
	
	# if we got here, we have a row to search
	for col in range(len(rows[row])):
		if(rows[row][col] == number):
			return row, col
	
	# if we got here, the search didn't find it
	# return a sign the search failed
	return -1, -1



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 jone kim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 07-January 10

Re: returning the values of row & column from list

Posted 22 February 2012 - 04:34 AM

Thanks baavgai very very much for the reply and for the great guide via your code.

I am new in programming.

I've solved the problem. Here is the code:

def checkvalue(number,matrixM):
	for row in range(len(matrixM)):
		print(len(matrixM))
		if (matrixM[row][0] == number):
			return row,0
		
		
	for row in range(len(matrixM)):
		if(matrixM[row][0] >= number):
			for column in range(len(matrixM)):
				if(matrixM[row-1][column] == number):
					return row-1, column 
		
		if(number > matrixM[3][0]):
			for column in range(len(matrixM)):
				if(matrixM[3][column] == number):
					return 3, column
				
	return -1,-1
		
def main():
	matrix = [[2,4,6,8],[10,12,14,16],[20,22,24,26],[28,30,32,34]]
	n = int(input(('provide the value of n: ')))
	row1,column1 = checkvalue(n,matrix)
	print('\n')
	print(row1,column1)
	return 0

if __name__ == '__main__':
	main()



Now how to check the time taken by this code to search for a particular value?

If I had used the general searching algorithm i.e. search every index one by one it had taken m*n time.

Now how to write the code to check the time taken by this code to search for a particular value?
Was This Post Helpful? 1
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5746
  • View blog
  • Posts: 12,554
  • Joined: 16-October 07

Re: returning the values of row & column from list

Posted 22 February 2012 - 04:55 AM

Excellent. I'm not real clear on this bit:
if(number > matrixM[3][0]):



If you really meant to do that, there's no need to have it in the loop. Or to hard code 3.

There should be a number of examples of people timing things lurking around this forum. I know I've done a few. However, your list is very, very short. The difference between a linear search and any optimization may not be apparent. You could generate a larger list...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1