25 XP

44. Where does the robot end up?

Beginner simulation strings

A robot starts at the point (0, 0) on a grid and follows a string of moves, one letter per step:

  • U moves it up: y goes up by 1;
  • D moves it down: y goes down by 1;
  • R moves it right: x goes up by 1;
  • L moves it left: x goes down by 1.

Write a function final_position(moves) that takes the string of moves and returns where the robot ends up, as a tuple (x, y). With no moves it stays at (0, 0).

The tests show a tuple in square brackets, [x, y], as JSON writes it, and a list [x, y] counts as a right answer too.

Example 1

Inputmoves = "UURRD"
Output[2, 1]
ExplanationUp 2, right 2, down 1.

Example 2

Inputmoves = "LDLD"
Output[-2, -2]
ExplanationLeft and down go below 0.

Example 3

Inputmoves = ""
Output[0, 0]
ExplanationNo moves: still at the start.

Constraints

  • moves has 0 to 100 characters, each U, D, L or R.

Submit also runs 7 more tests you can't see.

Practises Tuples (lesson 10).

Code
def final_position(moves):
    """Find where the robot stops.

    moves: str, made of U, D, L and R; may be empty
    Returns: tuple of two ints, (x, y), starting from
        (0, 0)
    """
    # Write your code here
    pass