Skip to content

Instantly share code, notes, and snippets.

@jaypeasee
Last active February 11, 2021 19:05
Show Gist options
  • Save jaypeasee/71707906337d5baaa37b004ce8cdf647 to your computer and use it in GitHub Desktop.
Save jaypeasee/71707906337d5baaa37b004ce8cdf647 to your computer and use it in GitHub Desktop.
Robot Tech Challenge

Robot Tech Challenge

JP Carey

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.

What are my assumptions and why?

  • this is meant to be an algorithmic solution. I can make an interactive design to show what the robot does with event listeners and CSS, but it can't be the source of truth.
  • I will need to create a function that takes in the arguments as a collection because I will need to know when to evaluate the Robots plane. If I received one piece of data at a time, I would have to store previous data and constantly run the function.
  • This function will return true or false because we want to know if it returned to its original position.

My high level thoughts about the problem

I will need a function that takes in an array and returns a boolean. For the design itself, I will design a grid using flexbox with rows and columns that represent elements/spaces in an array. I will have event listeners on key up to determin where this robot moves in the array and within the design.

What are some factors to realize

  • Searching of Data: I need to evaluate the parameters before this function can run. Are the pieces of the argument anything other than "G", "L" or "R"? If they arent, then remove them.
  • Sorting of Data: In order to evaluate if we return, true we need to know how many times each of the relevant keys/letters appeared. They need to be even. We can find part of that through sorting the letters to group them together.
  • Pattern Recognition:
  • Build/Navigate a Grid: You can either predefine the length of the array and take in arguments and fill it in starting at index 0. OR you can let the arguments define the array length.
  • Math: In order to get truthies we need to make sure everything is a modulo of 2. There also needs to be 2 more Ls than Rs or 2 more Rs than Ls.
  • Language API knowledge: I will need to make a post and fetch request to an api in order to store information. otherwise I will need to keep a global variable
  • Optimization: come to a solution first and then refactor. Will I really need to use a sort method?

What will the data look like? What data types will it hold? What are the pros and cons?

My data structure will be an array with strings to hold user submissions. The pros are that I can iterate through the array easily and check each string statically or dynamically. The cons are that I cant create a full design without a predetermined length of the array.

My Pseudocode

  • Create a global variable assigned to an empty array

  • Create a function that takes in a string as an argument

    • If the param is G, L or R, push the param into the global variable
    • Iterate through the variable (reduce)
      • Acc should be an empty object.
      • Using bracket notation with the acc on the reduce param, create or add to the properties respective value by 1.
    • If property G is an even number AND property L is exactly 2 more than property R OR property R is exactly two more than property L
      • return true
    • otherwise return false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment