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.
#Ruby
return_to_origin?("GRGRGRG")
=> true
- 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
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
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")