Skip to content

Instantly share code, notes, and snippets.

@jhennerich
Created July 27, 2022 20:55
Show Gist options
  • Select an option

  • Save jhennerich/9cf30823aa8479a287e041001fa690a6 to your computer and use it in GitHub Desktop.

Select an option

Save jhennerich/9cf30823aa8479a287e041001fa690a6 to your computer and use it in GitHub Desktop.

Instructions

In this challenge, you are working with a computer simulation of a mobile robot. The robot moves on a 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.

Example

#Ruby
    return_to_origin?("GRGRGRG")
    => true

Question: any user input from keyboard/CLI?

Brainstorming

  • string of char passed into the method
  • parse the passed in param,
  • methods for each command, G, L, R
  • keep track of the position in arrray

Psudo-code

create an position arrary to start, using number of commands passed in

A B 2 . . 1 . . 0 . .

robot start at 0 position 1st comand will be G,L,R. check for even number of G

Try within given time

def return_to_origin?(commands)
  # look for even number of  G
  if commands.count("G").odd?
     return false
  elsif commands.count("G") == 0
     return true
   elsif commands.split("G").count("R").even? || commands.split("G").count("L").even?
     return false
  end
end

#wip = return_to_origin?("GRGRGRG")
wip = return_to_origin?("GRGLGRG")
#wip = return_to_origin?("RGRGRG")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment