Skip to content

Instantly share code, notes, and snippets.

@holladayian
Last active December 10, 2020 17:22
Show Gist options
  • Save holladayian/0365bb1f85ff35f7274b247946399404 to your computer and use it in GitHub Desktop.
Save holladayian/0365bb1f85ff35f7274b247946399404 to your computer and use it in GitHub Desktop.

Problem

Robot

You are working with a computer simulation of a mobile robot. The robot moves on an plane, and its movements are described by a command string consisting of one or more of the following letters:

  • G instructs the robot to move forward one step
  • L instructs the robot to turn left
  • R instructs the robot to turn right

The robot CANNOT go backwards - poor robot. After running all of the movement commands, you want to know if the robot returns to its original starting location.

For instance, the command GRGRGRG would make the robot return to its original starting location.

Instructions

  1. Copy this markdown and paste in your own, privte gist
  2. In your private gist, fill out the questions below
  3. Submit by the due time as instructed in Zoom

Do not publish your code on a public repl.it or repo or other public means.

With only the information provided, what data is safe to extrapolate, based on the information given?

I would assume the robot can turn in a circle with out traveling any distance. For instance, R, R would spin the robot around, and thus, R, R, G would be a way for the robot to spin and travel "backwards" from it's initial orientation, like a roomba or a tank.

What are the first steps you would take to solve this problem?

Well, we know this bot is on a plane, and therefore can measure it's distance traveled on the x/y axes. We can also find the direction it has traveled based on how many L's or R's were input.

What type of data structure/algorithm is this?

  • Searching of Data
  • Sorting of Data
  • Pattern Recognition Because this problem deals with a pattern input of G, L, R I would think it's safe to assume it would utilize a pattern to solve.
  • Build/Navigate a Grid Because this problem already states the robot will travel on a plane, I would assume it would be safe to use a grid for this project.
  • Math
  • Language API knowledge
  • Optimization

How would you organize your data? Why?

I would assume an object would be the easiest for me to organize this problem. I don't know if front end is a familiar with different types of data structures as backend. If I was more versed, I might used a graph or binary tree.

Mock out some of the data flow for this project using mid-level design.

robot = { north: 0, east: 0, south: 0, west: 0, orientation: 'north' }

function inputG() { robot[robot.orientation] += 1 }

function inputR() { var directions = ['north', 'east', 'south', 'west']; if (robot.orientation === 'west') { robot.orientation === 'north' } else { robot.orientation === directions[directions.indexOf(robot.orientation) += 1] } }

function inputL() { var directions = ['north', 'east', 'south', 'west']; if (robot.orientation === 'north') { robot.orientation === 'west' } else { robot.orientation === directions[directions.indexOf(robot.orientation) -= 1] } }

function checkLocation() { if (robot.north -= robot.south === 0 && robot.west -= robot.east === 0) { return 'home' } else { return 'lost' } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment