[Tutor] Life Grid Implementation

niy mor niyniyniym at gmail.com
Mon Apr 20 02:36:59 CEST 2015


Is my code completing the assignment??? I need some *guidance* on
completing my assignment.

My Assignment:
A sparse life grid is used to store and represent the area in the game of
Life that contains organisms. The grid contains a rectangular grouping of
cells divided into an infinite number of rows and columns. The individual
cells, which can be alive or dead, are referenced by integer row and column
indices. The operations defined for the ADT are as follows:
• SparseLifeGrid() Creates a new infinite-sized game grid with all cells
initially dead.
 • minRange() Returns a 2-tuple (minrow, mincol) that contains the minimum
row index and the minimum column index that is currently occupied by a live
cell. The tuple (0, 0) is returned if there are no live cells.
 • maxRange() Returns a 2-tuple (maxrow, maxcol) that contains the maximum
row index and the maximum column index that is currently occupied by a live
cell. The tuple (0, 0) is returned if there are no live cells.
 • clearCell(row, col) Clears the individual cell (row, col) and sets it to
dead. If the cell is already dead, no action is taken.
 • setCell(row, col) Sets the indicated cell (row, col) to be alive. If the
cell is already alive, no action is taken.
 • isLiveCell(row, col) Returns a Boolean value indicating if the given
cell (row, col) contains a live organism.
• numLiveNeighbors(row, col) Returns the number of live neighbors for the
given cell (row, col). The neighbors of a cell include all of the cells
immediately surrounding it in all directions.

My Attempt Code:
class SparseLifeGrid :

    Cell = ["row", "col"]

    def __init__( self ):
        self._rowList = []
        self._colList = []
        self._cellSet = set()

    def _binarySearch( self, target ) :
        low = 0
        high = len(self._cellSet - 1)
        while low <= high :
            mid = (high + low) // 2
        if theList[mid] == target :
            return (True, mid)
        elif target < theList[mid] :
            high = mid - 1
        else :
                low = mid + 1

        return (False, low)


    def minRange( self ):
        return (sorted(self._rowList)[0], sorted(self._rowList)[0])

    def maxRange( self ):
        return (sorted(self._rowList, reverse = True)[0],\
                sorted(self._colList, reverse = True)[0])


     # Clears the individual cell (row, col) and sets it to dead. If the
cell is
     # already dead, no action is taken
    def clearCell( self, row, col ):
        for item in self :
            if item == Cell(row, col) :
                self.remove(item)

    def setCell( self, row, col ):
        LIVE_CELL = 1
        Cell[row, col] = LIVE_CELL

    def isLiveCell( self, row, col):
        return Cell(row,col) in self

    def numLiveNeighbors( self, row, col ):
        surround = 0
        if self.isLiveCell(row + 1, col) :
            surround += 1
        if self.isLiveCell(row - 1, col) :
            surround += 1
        if self.isLiveCell(row, col + 1) :
            surround += 1
        if self.isLiveCell(row, col - 1) :
            surround += 1


More information about the Tutor mailing list