Building things that matter

Code Challenges

CodeWars: Take a 10 Min Walk (Ruby)

Challenge here was to write a function that, given a random string of directions (n, s, e, w) will calculate if the given string would return you to your current location in exactly 10 mins. Each direction given equals 1 city block which takes 1 min to cover. 

The way they describe this one makes it feel harder than it ends up being, but it takes some pre-code reasoning to make sense of. 

Screen Shot 2018-02-25 at 9.56.03 PM.png

First thing to reason about is that if the string provides more than 10 directions (the length of the string is less than or greater than 10), you will it will not meet the requirements set forth above. 

From there, again, focusing on Single Responsibility or "Do One Thing," I created a second method that would do nothing but calculate if the directions given will return me to where I was in exactly 10 mins.  

The challenge here is that you instinctively want to start out thinking about it like a person, because, well you are a person, so you are thinking about executing the directions in the order they were given and that feels complicated to track. 

What you realize after looking at it for a bit is that no matter how many of a single direction are given, the exact same of its opposite direction must be given in order to get you back to where you were. For example, if I'm told to go 5 blocks N, that will take me 5 mins.  To get back to where I am, I will then need to travel back 5 blocks S. If i am given 4 blocks south and 1 block east for the second 5 directions, then I will be 1 block north and east of my original location, thus failing the requirements. 

So simply put, you need to check to see if, the two opposite directions are given an equal number of times in the random string.  if they are, then you will return in exactly 10 mins.  If they are not, you will not and it should flag as a failed combination. 

I also made the decision to put the collection I was evaluating as a single hash, after having explored and considered using multiple arrays and comparing them, I am in a Hash mood lately so I'm using them a lot more lately.