MIT 6.0002 Introduction to Computational Thinking and Data Science, Fall 2016 · Problem set 3, Problem 1: Implementing the RectangularRoom and Robot classes — RectangularRoom · Prof. Eric Grimson, Prof. John Guttag and Dr. Ana Bell · CC BY-NC-SA 4.0 · MIT publishes no solution for this set; the worked solution is ours
Complete the abstract class RectangularRoom, which represents the space to be cleaned and keeps track of which tiles have been cleaned. You write __init__, clean_tile_at_position, is_tile_cleaned, get_num_cleaned_tiles, is_position_in_room and get_dirt_amount; the class Position is given.
This problem set simulates Roomba-like robots cleaning the floor of a room. A room is a rectangle divided into square tiles. At the start of the simulation, each tile is covered in some amount of dirt, which is the same across tiles.
(0, 0), (0, 1), …, (0, h-1), (1, 0), (1, 1), …, (w-1, h-1). Tiles can never have a negative amount of dirt.dirt_amount.Position represents a location in x- and y-coordinates, floats satisfying 0 ≤ x < w and 0 ≤ y < h, measured from the lower left corner of the room, (0.0, 0.0). Note that room tiles are ordered pairs of integers but a Position is specified as floats — be careful converting between the two.An abstract class will never be instantiated in the simulation; it is a template for its subclasses (EmptyRoom and FurnishedRoom, the next challenges). The methods whose comment says "do not change" — get_num_tiles, is_position_valid and get_random_position — stay unimplemented here and must keep raising NotImplementedError. Store the room's dimensions as self.width and self.height: the given FurnishedRoom code reads them.
room = RectangularRoom(3, 4, 2) print(room.get_dirt_amount(1, 2)) # 2 room.clean_tile_at_position(Position(1.5, 2.5), 1) print(room.get_dirt_amount(1, 2)) # 1 print(room.is_tile_cleaned(1, 2)) # False room.clean_tile_at_position(Position(1.9, 2.1), 5) print(room.get_dirt_amount(1, 2)) # 0 print(room.get_num_cleaned_tiles()) # 1 print(room.is_position_in_room(Position(3.0, 1.0))) # False
Adapted for the browser: MIT's tester cleans each tile at a random point inside it; the tests here use fixed points inside each tile, and the room's dimensions are named width and height because the given furniture code relies on them.