#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    East Coast, USA
    Posts
    3
    Rep Power
    0

    Difficulty with ordered pairs


    I am very new to Python. I am trying to write a function that will calculate the manhattan distance for an 8 puzzle. I keep getting an error message and I am not sure how to remedy the problem. I am using Python version 2.7.5 on a MacBook running Mac OS X 10.5.8. I am using IDLE to run the module and the Shell to test the functions ( e.g. row_col_Of(4,init) ).

    When I test the first function, it does fine. When I test the second function I get the following error:

    File "<pyshhell#1>", line 1, in <module> manh(4, goal, init)
    File "/Users/badkaykay/Documents/testing.py", line 15, in manh row1, col1 = row_col_Of(tile, state1) ValueError: need more than 1 value to unpack
    Code:
    init = [[4,1,5],[7,3,2],[8,0,6]]
    goal = [[1,2,3],[4,5,6],[7,8,0]]
    
    ##returns list of ordered pairs where tile is found
    def row_col_Of(tile, cur_cnfg):
    	return [(row, col) for row in range(len(cur_cnfg)) for col in range(len(cur_cnfg[0])) if cur_cnfg [row][col]==[tile][0]]
    
    ##will calculate the manhattan distance and returns it as ordered pairs
    ##will add the sums as it goes along
    def manh(tile, state1, state2):
    	distance = 0
    	for tile in range(9):
    		row1, col1 = row_col_Of(tile, state1)
    		row2, col2 = row_col_Of(tile, state2)
    		distance += abs(row1 - row2) + abs(col1 - col2)
    	return distance
    Thank you in advance for determining what it is trying to tell me.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    530
    Rep Power
    34
    We can not tell as we do not know what title, state1 and state2 are. However printing the return from row_col_Of should reveal the problem. You should also consider some print statements elsewhere as I don't think
    if cur_cnfg [row][col]==[tile][0] does what you might expect
    Code:
    tile = [[1, 2, 3], [4,5,6]]
    print [tile][0]
    The problem is not that you are getting an error message. The problem is that you have untested code, some of which you might not understand, so test all of your code and convert anything you don't understand to a series of for() statements or whatever.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    East Coast, USA
    Posts
    3
    Rep Power
    0
    I tested the functions, and the first one printed and returned an ordered pair. Thanks anyway.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    530
    Rep Power
    34
    One of these two statements is incorrect
    File "/Users/badkaykay/Documents/CSC447/testing.py", line 15, in manh row1, col1 = row_col_Of(tile, state1) ValueError: need more than 1 value to unpack
    tested the functions, and the first one printed and returned an ordered pair
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    East Coast, USA
    Posts
    3
    Rep Power
    0
    Sorry, not trying to be obtuse, just not sure what you are trying to tell me. I will try to be clearer because I think when I initially posted, I was a bit frazzled.

    The first function row_col_Of(): I passed in the value of the tile I wanted to find in the 8 puzzle, which was 4. I also passed in the state of the 8 puzzle that I wanted to look in, which was init. When I ran it in the shell, it returned
    [0,0]
    . Indeed, the 4 is located in the zero row, and the zero column of the 8 puzzle, init.

    I tried to test the manh() function and it threw the original error I posted. I am not sure why the function row_col_Of() is working fine when I test it, but it throws an error in the manh() function.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481

    rewrite row_col_Of


    Code:
    init = [[4,1,5],[7,3,2],[8,0,6]]
    goal = [[1,2,3],[4,5,6],[7,8,0]]
    
    ##returns ordered pair where tile is found
    def row_col_Of(tile, cur_cnfg):
        for (row, row_data) in enumerate(cur_cnfg):
    	for (col, datum) in enumerate(row_data):
    	    if datum == tile:
    		return (row, col)
        raise ValueError('lost tile')
    
    ##will calculate the manhattan distance and returns it as ordered pairs
    ##will add the sums as it goes along
    def manh(tile, state1, state2):
        distance = 0
        for tile in range(9):
            row1, col1 = row_col_Of(tile, state1)
            row2, col2 = row_col_Of(tile, state2)
            distance += abs(row1 - row2) + abs(col1 - col2)
        return distance
    
    print(manh(4, goal, init))
    Last edited by b49P23TIvg; September 20th, 2013 at 02:04 PM. Reason: Changed title, inserted exception.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo