Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 18th, 2013, 01:27 PM
Registered User

Join Date: Sep 2013
Location: East Coast, USA
Posts: 3
Time spent in forums: 1 h 12 m 58 sec
Reputation 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:

Quote:
 File "", line 1, in 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
September 18th, 2013, 03:16 PM
 dwblas
Contributing User

Join Date: May 2009
Posts: 423
Time spent in forums: 5 Days 8 h 10 m 13 sec
Reputation Power: 32
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.

#3
September 18th, 2013, 04:30 PM
Registered User

Join Date: Sep 2013
Location: East Coast, USA
Posts: 3
Time spent in forums: 1 h 12 m 58 sec
Reputation Power: 0
I tested the functions, and the first one printed and returned an ordered pair. Thanks anyway.

#4
September 18th, 2013, 07:38 PM
 dwblas
Contributing User

Join Date: May 2009
Posts: 423
Time spent in forums: 5 Days 8 h 10 m 13 sec
Reputation Power: 32
One of these two statements is incorrect
Quote:
 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
Quote:
 tested the functions, and the first one printed and returned an ordered pair

#5
September 18th, 2013, 09:17 PM
Registered User

Join Date: Sep 2013
Location: East Coast, USA
Posts: 3
Time spent in forums: 1 h 12 m 58 sec
Reputation 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
Quote:
 [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.

#6
September 20th, 2013, 02:02 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,218
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 51 m 46 sec
Reputation Power: 455
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))```
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : September 20th, 2013 at 02:04 PM. Reason: Changed title, inserted exception.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Difficulty with ordered pairs