[Tutor] Life Grid Implementation

Alan Gauld alan.gauld at btinternet.com
Mon Apr 20 09:39:50 CEST 2015


On 20/04/15 01:36, niy mor wrote:
> Is my code completing the assignment???

No.

> I need some *guidance* on
> completing my assignment.

Try running your code.
Then fix the errors.
If you can't fix them come back to us and ask for help,
including the error message and a description of what
happened and what you expected.

> My Attempt Code:
> class SparseLifeGrid :
>
>      Cell = ["row", "col"]

What is this for?

>      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)

This just tells you which half the item is in.
Is that what you wanted?

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

This returns the same value twice.

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

You don;t need the line continuation character (/) if you are inside parens.

def f():
    return (expression1,
            expression2)

>       # 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)

How do you expect to iterate over self? self is your class instance
but it has nothing to iterate over?

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

Here you create a new Cell instance then try to assign a value to it, 
then throw it away. I'm pretty sure thats not what you want.

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

Again how are you testing 'in' self. There is nothing in
your class to support that.

>
>      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

How many neighbours does a cell have in a Life grid?
Is it only 4?


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list