472,953 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,953 software developers and data experts.

nested lists as arrays

Hi, I'm using nested lists as arrays and having some problems with
that approach. In my puzzle class there is a swapelement method which
doesn't work out. Any help and comments on the code will be
appreciated. Thanks.

# Puzzle.py
# class for a sliding block puzzle

# an starting state of a 8-puzzle could look like the following:
# where X is an empty space and the move up in the example would move
6 to X
# X is represented as -1 in the array
# ------------
# | 7 2 4 |
# | |
# | 5 X 6 |
# | |
# | 8 3 1 |
# ------------

# the goal is to reach this state:
# ------------
# | X 1 2 |
# | |
# | 3 4 5 |
# | |
# | 6 7 8 |
# ------------

import copy

class Puzzle:

def __init__(self, dim):
self.dim = dim
self.elements = [[0 for column in range(dim)] for row in
range(dim) ]

def getEmptySlot(self):
i = 0
j = 0
while i <= self.dim-1:
while j <= self.dim-1:
if self.elements[j][i] == -1:
return [j, i]
j = j+1
j = 0
i = i + 1

def performMove(self, direction):
slot = self.getEmptySlot()

if (direction == "up"):
self.swapElements(slot[1], slot[0], slot[1]+1, slot[0])
elif (direction == "down"):
self.swapElements(slot[1], slot[0], slot[1]-1, slot[0])
elif direction == "left":
self.swapElements(slot[1], slot[0], slot[1], slot[0]-1)
elif (direction == "right"):
self.swapElements(slot[1], slot[0], slot[1], slot[0]+1)

def swapElements(self, fromx, fromy, tox, toy):
dummy = self.elements[toy][tox]

self.elements[toy][tox] = self.elements[fromy][fromx]
self.elements[fromy][fromx] = dummy

def getPossibleMoves(self):
emptySlot = self.getEmptySlot()
y = emptySlot[1]
x = emptySlot[0]
north = (y == 0)
south = (y == (self.dim-1))
west = (x == 0)
east = (x == (self.dim-1))

middle = not(north or south or west or east)

northwest = north and west
northeast = north and east
southwest = south and west
southeast = south and east
# orientation has to be distinct
# save original values
orignorth = north
origsouth = south
# original north or south
orignors = north or south

north = north and not (west or east)
south = south and not (west or east)
west = west and not (orignors)
east = east and not (orignors)

if middle:
return ["up", "down", "left", "right"]
elif north:
return ["up", "left", "right"]
elif south:
return ["down", "left", "right"]
elif west:
return ["up", "down", "left"]
elif east:
return ["up", "down", "right"]
elif northwest:
return ["up", "left"]
elif northeast:
return ["up", "right"]
elif southwest:
return ["down", "left"]
elif southeast:
return ["down", "right"]

# ~Puzzle.py
Jul 18 '05 #1
5 1602
"naturalborncyborg" <be*************@blawrc.de> wrote:
Hi, I'm using nested lists as arrays and having some problems with
that approach. In my puzzle class there is a swapelement method which
doesn't work out.
what happens, and what do you expect?
Any help and comments on the code will be appreciated.


is it elements[y][x] or elements[x][y]? both variants seem to be used
in your code.

(fwiw, in this case, using a dictionary might be a lot easier. store cells
as elements[x, y], use elements.get((x, y)) is None to check for empty
cells, use elements[x, y] = None or del elements[x, y] to empty a cell,
use elements = {} to clear all cells, etc).

</F>

Jul 18 '05 #2
naturalborncyborg wrote:
Hi, I'm using nested lists as arrays and having some problems with
that approach. In my puzzle class there is a swapelement method which
doesn't work out.


What "doesn't work out"? On casual inspection that method seems to "work":
p = Puzzle(2)
p.elements[0][0] = 1
p.elements[1][1] = 2
p.elements [[1, 0], [0, 2]] p.swapElements(0,0,1,1)
p.elements [[2, 0], [0, 1]]


What should it do instead?

Michael

Jul 18 '05 #3
Allright. What difference in runtime and space would it make using
dictionaries instead?

Do you have a pointer for me concerning runtime of standard
manipulations in Pythons?

Thanks for tip.

Jul 18 '05 #4
Allright. What difference in runtime and space would it make using
dictionaries instead?

Do you have a pointer for me concerning runtime of standard
manipulations in Pythons?

Thanks for the tip.

Jul 18 '05 #5
be*************@blawrc.de wrote:
Allright. What difference in runtime and space would it make using
dictionaries instead?


is this for an interactive game? if so, the answer is "none at all".

(on my machine, you can make about 6000000 dict[x,y] accesses per
second, compared to 7500000 list[x][y] accesses... assuming that your
algorithm needs to check each cell on the board 10 times per move, and
you want the moves to appear "instantly", the slower solution should be
fast enough for a 250x250 board, compared to a 270x270 board for the
faster solution...).

</F>

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Xah Lee | last post by:
Here's the belated Java solution. import java.util.List; import java.util.ArrayList; import java.lang.Math; class math { public static List range(double n) { return range(1,n,1); }
5
by: John Topley | last post by:
Hi, I'm doing some work in an intranet environment where I'm forced to use IE 6.0. Is it possible to style (unordered) nested lists so that the inner list items have a different appearance to...
4
by: steven | last post by:
Hi, I want to make nested lists like <ul id="nested"> <li>level1 item</li> <ul> <li>level2 item</li> <li>level2 item</li> </ul> <li>level1 item</li>
6
by: Licheng Fang | last post by:
I wanna use nested lists as an array, but here's the problem: >>> a = *3]*3 >>> a , , ] >>> a = 1 >>> a , , ] Could anybody please explain to me why three values were change? I'm
4
by: morrhamen | last post by:
Hello! This is actually a reply to post: http://www.thescripts.com/forum/threadnav100612-1-10.html from July 2005, to which I can't reply. The following code does "Automated numbering of...
0
by: TAL651 | last post by:
Hello, I am trying to get an effect where I use nested lists, but they are arranged horizontally, like so: Item 1......1.a ...............1.b Item 2......2.a ...............2.b
7
by: Zethex | last post by:
Im a bit new to python. Anyway working on a little project of mine and i have nested lists ie Answer = , ] and so forth.., Anyway the amount of ] do increase over time. Im just wondering...
3
by: Robert Mark Bram | last post by:
Hi all, I have some code to iterate through nested associative arrays. Can anyone please tell me what I am doing wrong? :) var dealers = new Array(); var dealer1 = new Array(); dealer1 =...
0
Norris
by: Norris | last post by:
Hi everyone ! In first sorry for my bad english, I'm french. I try to make a custom TreeView and a custom TreeNode. For storing child nodes in my customTreeNode, I use a List(Of MyTreeNode). I...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.