Building things that matter
blake-connally-373084.jpg

Code Challenges

CodeWars: Reverse or Rotate (Ruby)

This one was easy once I read the directions correctly. 

Basically create function that takes two arguments (string and size) and then chunk the string into arrays the length of the size given. Then look at each array and based on a math mathematical formula (is sum of the cubes of its digits is divisible by 2), either rotate or reverse the array and join it all back and return that string as the value. 

Again, working the Single Responsibility angle here I added three additional functions to handle the chunking of the string, the math on each chunk and then checking the result of the math and performing the rotate or reverse action. 

One of the challenges I had here was I read the directions wrong the first time, so I was not picking up on the need to break the initial string into multiple (occasionally more than 2) arrays.  I was expecting to only be dealing with two arrays, so my first attempts failed badly.  

I ended up going to the white board to sort it out visually and that was when I could see what I had done wrong and came up with my solution in about 20 mins after. 

IMG_0340.JPG

I may look at refactoring this to use a hash instead of the array, but the each_slice handled splitting the given string so easily and then I was able to iterate over each of the array's easily after that.  Plus, including my white board time, I had spent more time on this than I had wanted today and well, other things call.