Problem 462719 · easy · Level 04 Non-Linear Data Structures

A Fleet of Floor Robots

py-classes · state and behaviour · simulation

A warehouse runs a fleet of small floor robots on a square grid. Each robot has a name, a position (x, y), a heading ("N", "E", "S" or "W") and a battery. Moving one cell uses one unit of battery; turning is free. North increases y, east increases x.

Write a class Robot:

  • Robot(name, x=0, y=0, heading="N", battery=10). The starting battery is also the robot's capacity.
  • turn(side) turns a quarter to the left ("L") or right ("R").
  • forward(steps) moves up to steps cells straight ahead and returns how many cells it really moved: a robot stops when its battery is empty.
  • recharge() fills the battery back to capacity and returns how many units were added.
  • position() returns (x, y, heading).
  • The attribute odometer is the total number of cells the robot has moved.

The tests control the fleet with the helper drive(Robot, fleet, orders): fleet is a list of (name, x, y, heading, battery) tuples, and each order is a string "<name> <command>", where the command is L, R, F<steps> (for example F3), C (recharge), ? (position) or odo (odometer). It returns one result per order (None for turns).

Examples

Input:  drive(Robot, [("ada", 0, 0, "N", 5), ("bo", 2, 2, "W", 10)],
              ["ada F3", "ada R", "ada F4", "bo F1", "ada ?", "bo ?", "ada C", "ada odo"])
Output: [3, None, 2, 1, (2, 3, 'E'), (1, 2, 'W'), 5, 5]
Explanation: ada has 5 units: it moves 3 north, turns east and only 2 more cells are possible.

Constraints

  • Up to 50 robots and 5000 orders; 0 <= steps <= 100.
  • Robots do not block each other, and the grid has no edges.

Goals

  • Model many independent machines as objects of one class
  • Keep position, heading and battery consistent inside the methods
  • Return what really happened when a request can only partly be met
Starting Python…