diff --git a/README.md b/README.md new file mode 100644 index 0000000..9b46a51 --- /dev/null +++ b/README.md @@ -0,0 +1,73 @@ +# Toy Robot Simulator + +## Problem Statement + +Initial problem description taken from https://github.com/xandeep/ToyRobot. + +Create a library that can read in commands of the following form: + +``` +PLACE X,Y,DIRECTION +MOVE +LEFT +RIGHT +REPORT +``` + +*The library allows for a simulation of a toy robot moving on a 6 x 6 square tabletop.* + +1. There are no obstructions on the table surface. +2. The robot is free to roam around the surface of the table, but must be prevented from falling to destruction. Any movement that would result in this must be prevented, however further valid movement commands must still be allowed. +3. PLACE will put the toy robot on the table in position X,Y and facing NORTH, SOUTH, EAST or WEST. +4. (0,0) can be considered as the SOUTH WEST corner and (5,5) as the NORTH EAST corner. +5. The first valid command to the robot is a PLACE command. After that, any sequence of commands may be issued, in any order, including another PLACE command. The library should discard all commands in the sequence until a valid PLACE command has been executed. +6. The PLACE command should be discarded if it places the robot outside of the table surface. +7. Once the robot is on the table, subsequent PLACE commands could leave out the direction and only provide the coordinates. When this happens, the robot moves to the new coordinates without changing the direction. +8. MOVE will move the toy robot one unit forward in the direction it is currently facing. +9. LEFT and RIGHT will rotate the robot 90 degrees in the specified direction without changing the position of the robot. +10. REPORT will announce the X,Y and orientation of the robot. +11. A robot that is not on the table can choose to ignore the MOVE, LEFT, RIGHT and REPORT commands. +12. The library should discard all invalid commands and parameters. + +### Example Input and Output: + +``` +a) +PLACE 0,0,NORTH +MOVE +REPORT +Output: 0,1,NORTH +``` + +``` +b) +PLACE 0,0,NORTH +LEFT +REPORT +Output: 0,0,WEST +``` + +``` +c) +PLACE 1,2,EAST +MOVE +MOVE +LEFT +MOVE +REPORT +Output: 3,3,NORTH +``` + +``` +d) +PLACE 1,2,EAST +MOVE +LEFT +MOVE +PLACE 3,1 +MOVE +REPORT +Output: 3,2,NORTH +``` + + diff --git a/example_a.txt b/example_a.txt new file mode 100644 index 0000000..99caa04 --- /dev/null +++ b/example_a.txt @@ -0,0 +1,3 @@ +PLACE 0,0,NORTH +MOVE +REPORT \ No newline at end of file diff --git a/example_b.txt b/example_b.txt new file mode 100644 index 0000000..7b879ce --- /dev/null +++ b/example_b.txt @@ -0,0 +1,3 @@ +PLACE 0,0,NORTH +LEFT +REPORT \ No newline at end of file diff --git a/example_c.txt b/example_c.txt new file mode 100644 index 0000000..4552d71 --- /dev/null +++ b/example_c.txt @@ -0,0 +1,6 @@ +PLACE 1,2,EAST +MOVE +MOVE +LEFT +MOVE +REPORT \ No newline at end of file diff --git a/example_d.txt b/example_d.txt new file mode 100644 index 0000000..43ea899 --- /dev/null +++ b/example_d.txt @@ -0,0 +1,7 @@ +PLACE 1,2,EAST +MOVE +LEFT +MOVE +PLACE 3,1 +MOVE +REPORT \ No newline at end of file diff --git a/toyrobot/robot.py b/toyrobot/robot.py index 90fa5f1..a768da8 100644 --- a/toyrobot/robot.py +++ b/toyrobot/robot.py @@ -24,6 +24,14 @@ class Robot: "WEST": 3, } + VALID_COMMANDS = [ + "PLACE", + "MOVE", + "LEFT", + "RIGHT", + "REPORT" + ] + # Private internals # Key corresponds to numerical direction defined in Robot.directions # Value is an offset vector in with x and y keys (positive x is NORTH, @@ -144,13 +152,14 @@ class Robot: new_position_x = self._position_x + vector["x"] new_position_y = self._position_y + vector["y"] - if not Robot.valid_position(new_position_x, new_position_y): + if not self.valid_position(new_position_x, new_position_y): return self._position_x = new_position_x self._position_y = new_position_y - def place(self, position_x: int, position_y: int, direction_name: str): + def place(self, position_x: int, position_y: int, + direction_name: str = None): """ Places the Robot instance at the specified coordinates with the specified direction. @@ -164,16 +173,28 @@ class Robot: position_x (int): Horizontal coordinate for placement. position_y (int): Vertical coordinate for placement. direction_name (str): Direction of placement; must be one of the - string keys in `Robot.DIRECTIONS`. + string keys in `Robot.DIRECTIONS`. Must be specified on first + placement (or this call will fail silently), but is optional + thereafter. """ - direction_name = direction_name.upper() - if direction_name not in Robot.DIRECTIONS.keys() or \ - not self.valid_position(position_x, position_y): + + # Must be careful not to make any state changes until all inputs + # have been validated + if not self.valid_position(position_x, position_y): return + if direction_name is not None: + direction_name = direction_name.upper() + if direction_name not in Robot.DIRECTIONS.keys(): + return + elif not self.is_initialized(): + # Direction MUST be specified on first placement + return + + if direction_name is not None: + self._direction = Robot.DIRECTIONS[direction_name] self._position_x = position_x self._position_y = position_y - self._direction = Robot.DIRECTIONS[direction_name] def get_position(self) -> (int, int): """ @@ -184,6 +205,7 @@ class Robot: order (position_x, position_y), or (None, None) if this instance has not yet been initialized. """ + return ( (self._position_x, self._position_y) if self.is_initialized() @@ -200,6 +222,7 @@ class Robot: keys of `Robot.DIRECTION`. Will return None if this instance has not yet been initialized. """ + return ( {v: k for k, v in Robot.DIRECTIONS.items()}[self._direction] if self.is_initialized() @@ -214,8 +237,10 @@ class Robot: Does nothing if this instance has not yet been correctly placed (initialized). """ + if not self.is_initialized(): return + self._direction = (self._direction - 1) % 4 def rotate_right(self): @@ -226,10 +251,68 @@ class Robot: Does nothing if this instance has not yet been correctly placed (initialized). """ + if self.is_initialized(): return self._direction = (self._direction + 1) % 4 + def interpret_command(self, command: str): + """ + Interprets a given string command and applies the appropriate + transformation to this Robot instance. Fails silently if the command + is unrecognized or invalid. + + Refer to the full problem description for a list and examples of valid + commands. + + Args: + command (str): The command to be interpreted. + """ + + command = command.upper() + command_tokens = [x.strip() for x in command.split(' ') if len(x) > 0] + + if len(command_tokens) == 0 or not command_tokens[0] in \ + Robot.VALID_COMMANDS: + return + + match command_tokens[0]: + case "PLACE": + try: + # Must have parameters + if len(command_tokens) < 2: + return + parameter_tokens = [x.strip() for x in \ + command_tokens[1].split(',')] + + # Must have at least X, Y + if len(parameter_tokens) < 2: + return + + # Throws ValueError if invalid input + place_x = int(parameter_tokens[0]) + place_y = int(parameter_tokens[1]) + + # Direction parameter is optional on second and subsequent + # placements. The place() method accounts for an absent + # direction on first call and fails silently. + if len(parameter_tokens) > 2: + place_direction = parameter_tokens[2] + self.place(place_x, place_y, place_direction) + else: + self.place(place_x, place_y) + except ValueError as ve: + # Unable to convert x or y token to int + return + case "MOVE": + self.move() + case "LEFT": + self.rotate_left() + case "RIGHT": + self.rotate_right() + case "REPORT": + print("Output: {}".format(str(self))) + def __str__(self) -> str: """ Returns a string representation of the instance including position and @@ -238,9 +321,11 @@ class Robot: Returns: str: This instance's string representation. """ + if not self.is_initialized(): return "Uninitialized" - return "X: {}, Y: {}, direction: {}".format( + + return "{},{},{}".format( self._position_x, self._position_y, {v: k for k, v in Robot.DIRECTIONS.items()}[self._direction], diff --git a/trexamples.py b/trexamples.py new file mode 100644 index 0000000..c60ec0e --- /dev/null +++ b/trexamples.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +import toyrobot + +def feed_file(filename: str, robot: toyrobot.Robot): + with open(filename) as f: + for line in f: + line = line.strip() + print(line) + robot.interpret_command(line) + +def main(): + print('a)') + feed_file('example_a.txt', toyrobot.Robot(6, 6)) + print() + + print('b)') + feed_file('example_b.txt', toyrobot.Robot(6, 6)) + print() + + print('c)') + feed_file('example_c.txt', toyrobot.Robot(6, 6)) + print() + + print('d)') + feed_file('example_d.txt', toyrobot.Robot(6, 6)) + print() + +if __name__ == "__main__": + main() diff --git a/trmain.py b/trmain.py deleted file mode 100644 index 2369451..0000000 --- a/trmain.py +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env python3 - -# import sys -# import argparse - -import toyrobot - - -def main(): - current_bot = toyrobot.Robot(10, 10) - print(current_bot) - current_bot.place(6, 6, "NORTH") - print(current_bot) - current_bot.place(2, 2, "NORTH") - print(current_bot) - current_bot.place(6, 6, "NORTH") - print(current_bot) - print("Not yet implemented") - - -if __name__ == "__main__": - main()