edx分级员不接受代码

时间:2014-04-03 08:17:55

标签: python debugging python-2.7

有时,评分者会正确执行代码,但有时会出现以下错误

Your program timed out.  Check for very slow code or infinite loops.

We couldn't run your solution (l376).

我有6.00.2x的以下代码。

class RectangularRoom(object):
    """
    A RectangularRoom represents a rectangular region containing clean or dirty
    tiles.

    A room has a width and a height and contains (width * height) tiles. At any
    particular time, each of these tiles is either clean or dirty.
    """
    def __init__(self, width, height):
        """
        Initializes a rectangular room with the specified width and height.

        Initially, no tiles in the room have been cleaned.

        width: an integer > 0
        height: an integer > 0
        """
        self.width = width; self.height = height
        self.room = {}
        for x in range(self.width):
            for y in range(self.height):
                self.room[(x,y)] = False


    def cleanTileAtPosition(self, pos):
        """
        Mark the tile under the position POS as cleaned.

        Assumes that POS represents a valid position inside this room.

        pos: a Position
        """
        self.room[(int(math.floor(pos.getX())),int(math.floor(pos.getY())))] = True

    def isTileCleaned(self, m, n):
        """
        Return True if the tile (m, n) has been cleaned.

        Assumes that (m, n) represents a valid tile inside the room.

        m: an integer
        n: an integer
        returns: True if (m, n) is cleaned, False otherwise
        """
        return self.room[(m,n)]

    def getNumTiles(self):
        """
        Return the total number of tiles in the room.

        returns: an integer
        """
        return len(self.room)

    def getNumCleanedTiles(self):
        """
        Return the total number of clean tiles in the room.

        returns: an integer
        """
        nct = 0
        for e in self.room :
            nct += self.room[e]
        return nct

    def getPct(self):
        """
        Returns the percentage of clean tiles in the room.

        return: a float between 0 and 1
        """
        return float(self.getNumCleanedTiles())/self.getNumTiles()


    def getRandomPosition(self):
        """
        Return a random position inside the room.

        returns: a Position object.
        """
        return Position(self.width*random.random(),self.height*random.random())

    def isPositionInRoom(self, pos):
        """
        Return True if pos is inside the room.

        pos: a Position object.
        returns: True if pos is in the room, False otherwise.
        """
        return int(math.floor(pos.getX())) >= 0 and int(math.floor(pos.getX())) < self.width\
               and int(math.floor(pos.getY())) >= 0 and int(math.floor(pos.getY())) < self.height


class Robot(object):
    """
    Represents a robot cleaning a particular room.

    At all times the robot has a particular position and direction in the room.
    The robot also has a fixed speed.

    Subclasses of Robot should provide movement strategies by implementing
    updatePositionAndClean(), which simulates a single time-step.
    """
    def __init__(self, room, speed):
        """
        Initializes a Robot with the given speed in the specified room. The
        robot initially has a random direction and a random position in the
        room. The robot cleans the tile it is on.

        room:  a RectangularRoom object.
        speed: a float (speed > 0)
        """
        self.room = room; self.speed = speed
        self.position = self.room.getRandomPosition()
        self.room.cleanTileAtPosition(self.position)
        self.direction = random.randrange(360)

    def getRobotPosition(self):
        """
        Return the position of the robot.

        returns: a Position object giving the robot's position.
        """
        return self.position

    def getRobotDirection(self):
        """
        Return the direction of the robot.

        returns: an integer d giving the direction of the robot as an angle in
        degrees, 0 <= d < 360.
        """
        return self.direction

    def setRobotPosition(self, position):
        """
        Set the position of the robot to POSITION.

        position: a Position object.
        """
        self.position = position

    def setRobotDirection(self, direction):
        """
        Set the direction of the robot to DIRECTION.

        direction: integer representing an angle in degrees
        """
        self.direction = direction

    def updatePositionAndClean(self):
        """
        Simulate the raise passage of a single time-step.

        Move the robot to a new position and mark the tile it is on as having
        been cleaned.
        """
        raise NotImplementedError # don't change this!


## === Problem 2
class StandardRobot(Robot):
    """
    A StandardRobot is a Robot with the standard movement strategy.

    At each time-step, a StandardRobot attempts to move in its current
    direction; when it would hit a wall, it *instead* chooses a new direction
    randomly.
    """
    time_unit = 0

    def updatePositionAndClean(self):
        """
        Simulate the raise passage of a single time-step.

        Move the robot to a new position and mark the tile it is on as having
        been cleaned.
        """
        self.setRobotPosition(self.position.getNewPosition(self.direction,self.speed))
        while self.room.isPositionInRoom(self.getRobotPosition()) == False:
            direction = random.randrange(360)
            self.setRobotPosition(self.position.getNewPosition(self.direction,self.speed))


        self.room.cleanTileAtPosition(self.position)

1 个答案:

答案 0 :(得分:0)

    At each time-step, a StandardRobot attempts to move in its current
    direction; when it would hit a wall, it *instead* chooses a new direction
    randomly.

由于这个机器人随机选择一个新的方向,所以需要一段不确定的时间来继续前进,更不用说到达房间里的所有瓷砖了。

,这并不奇怪
  

有时会出现以下错误

Your program timed out.