nested lists as arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • naturalborncyborg

    #1

    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(se lf):
    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(sel f, direction):
    slot = self.getEmptySl ot()

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

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

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

    def getPossibleMove s(self):
    emptySlot = self.getEmptySl ot()
    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
  • Fredrik Lundh

    #2
    Re: nested lists as arrays

    "naturalborncyb org" <benjamin.corde s@blawrc.de> wrote:
    [color=blue]
    > 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.[/color]

    what happens, and what do you expect?
    [color=blue]
    > Any help and comments on the code will be appreciated.[/color]

    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>



    Comment

    • Michael Spencer

      #3
      Re: nested lists as arrays

      naturalborncybo rg wrote:[color=blue]
      > 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.[/color]

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

      What should it do instead?

      Michael

      Comment

      • benjamin.cordes@blawrc.de

        #4
        Re: nested lists as arrays

        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.

        Comment

        • benjamin.cordes@blawrc.de

          #5
          Re: nested lists as arrays

          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.

          Comment

          • Fredrik Lundh

            #6
            Re: nested lists as arrays

            benjamin.cordes @blawrc.de wrote:
            [color=blue]
            > Allright. What difference in runtime and space would it make using
            > dictionaries instead?[/color]

            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>



            Comment

            Working...