1

I am new to python. My code runs into infinite loop, keeps adding & printing numberCycles. My logic in C++ works fine. Would you please help to identify why?

  • The first line of input is the number of simulations.
  • The next line is the number of minutes for a single reproductive cycle.
  • The next line is the number of rows (x), followed by a single space, and followed by number of columns (y).
  • The next group of y lines will have x number of characters, with a single period (.) representing a blank space and a single capitol B representing a starting Bunny tries to reproduce to up, right, down, left direction. If there is a existing bunny, then it goes to sleep.

Input.txt

2     # 2 simulations
5     # 5 minutes/cycle
3 3   # 3*3 map
...
.B.
...
1
4 4
B.BB
..B.
...
B.B

Spot.py

#!/usr/bin/env python

class Spot(object):
    isBunny = bool()
    nextCycle = 0
    UP = 0
    RIGHT = 1
    DOWN = 2
    LEFT = 3
    SLEEP = 4

    def __init__(self, newIsBunny):
        self.isBunny = newIsBunny
        self.nextCycle = self.UP

    def setNextCycle(self):
        if (self.nextCycle != self.SLEEP):
            self.nextCycle += 1

    def getNextCycle(self):
        return self.nextCycle

    def getIsBunny(self):
        return self.isBunny

    def makeBunny(self):
        if not self.isBunny:
            self.nextCycle = self.UP
        self.isBunny = True

Bunny.py

#!/usr/bin/env python
from Spot import Spot
import os

class Bunny(object):    
    @classmethod
    def main(cls, args):
        with open(os.path.expanduser('~/Desktop/input.txt')) as f: 
            numSims = int(f.readline()) 
            myMap = []  
            print numSims
            minPerCycle= int(f.readline()) 
            print minPerCycle
            for k in xrange(numSims): 
                xyLine= f.readline()
                row = int(xyLine.split()[0]) 
                col = int(xyLine.split()[1])  
                print row,col
                for i in range(0,row): 
                    myLine = f.readline() 
                    myMap.append([]) 
                    for j in range(0,col): 
                        myMap[i].append(Spot(myLine[j] == 'B')) 

                numCycles = 1

                if cls.isFilled(row,col,myMap):
                    numCycles = 0

                while not cls.isFilled(row,col,myMap):
                    numCycles += 1
                    print numCycles
                    for m in range(0,row): 
                        for n in range(0,col): 
                            if myMap[m][n].getIsBunny():
                                if myMap[m][n].getNextCycle() == Spot.UP:
                                    if m>0:
                                        myMap[m-1][n].makeBunny()
                                    break
                                elif myMap[m][n].getNextCycle() == Spot.RIGHT:
                                    if n<col-1:
                                        myMap[m][n+1].makeBunny()
                                    break
                                elif myMap[m][n].getNextCycle() == Spot.DOWN:
                                    if m<row-1:
                                        myMap[m+ 1][n].makeBunny()
                                    break
                                elif myMap[m][n].getNextCycle() == Spot.SLEEP:
                                    if n>0:
                                        myMap[m][n- 1].makeBunny()
                                    break
                            myMap[m][n].setNextCycle() 
                time = numCycles * minPerCycle
                print "It took " , time , " minutes for the bunnies to take over the world!\n"
                del myMap[:]
            f.close()

    @classmethod
    def isFilled(cls,row,col,myMap):
        for a in range(0,row): 
            for b in range(0,col): 
                if not myMap[a][b].getIsBunny():
                    return False
        return True


if __name__ == '__main__':
    import sys
    Bunny.main(sys.argv)
gpoo
  • 8,408
  • 3
  • 38
  • 53
Mike
  • 497
  • 1
  • 5
  • 7
  • If the code is producing an infinite loop, you know that somehow the grid is never being filled. Make sure that your `Spot` class is working properly and `isFilled` works as well. – Blender Sep 27 '12 at 07:08
  • thanks, I am new to python. I checked my logic in C++. – Mike Sep 27 '12 at 07:12

1 Answers1

1

It never goes to the left. Also, you set a break for the inner loop, so, myMap[m][n].setNextCycle() is never called. I just saw your C++ code and you translated it to Python with errors (using Spot.SLEEP instead of Spot.LEFT).

The break statement makes sense in C++ because you want to break the switch. Here, you are using if..else.

It should be something like:

while not cls.isFilled(row, col, myMap):
    numCycles += 1
    print numCycles
    for m in range(0,row): 
        for n in range(0,col): 
            if myMap[m][n].getIsBunny():
                if myMap[m][n].getNextCycle() == Spot.UP:
                    if m>0:
                        myMap[m-1][n].makeBunny()
                elif myMap[m][n].getNextCycle() == Spot.RIGHT:
                    if n<col-1:
                        myMap[m][n+1].makeBunny()
                elif myMap[m][n].getNextCycle() == Spot.DOWN:
                    if m<row-1:
                        myMap[m+ 1][n].makeBunny()
                elif myMap[m][n].getNextCycle() == Spot.LEFT:
                    if n>0:
                        myMap[m][n-1].makeBunny()

            myMap[m][n].setNextCycle()
Community
  • 1
  • 1
gpoo
  • 8,408
  • 3
  • 38
  • 53
  • Thanks for looking into this. The first 3 * 3 matrix is as shown above, only 1 Bunny in the middle. Until the cycle when reaching the first Bunny, it starts to fill the above spot with a Bunny. As long as there is empty spot in the matrix, isFilled() returns false. Until all spots are filled with bunny, isFilled() return true. – Mike Sep 27 '12 at 07:58
  • But, in your code it never goes to the left. If it never goes to the left, the first element in the Matrix (0,0) is always False => Infinite loop. – gpoo Sep 27 '12 at 08:00