This is a follow up to my previous blog post on the Ruby Monk problem. The challenge with which I ended that post was to solve the problem with the constraint of using the
inject method. Here’s a solution that meets that challenge:
1 2 3
Instead of initializing the inject method with an empty string, I used the
map method to apply the
capitalize method on each of the elements in the array. Once I have done that, then using method chaining, I can start combining each element in the array in a cumulative manner with the
Here’s an interesting question: If I had several thousands elements in the array, which solution would be better in terms of performance? It would probably be more efficient to use the hack in the previous post. In this solution, I am iterating through the array twice: once using the map method and a second time using the inject method.
I use Benchmark to give me an idea of what performance looks like between the two solutions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Take a look at these results:
1 2 3
The new solution is 40% faster than the solution in my previous post! This was not what I expected at all! It seems counter-intuitive that iterating through an array twice is faster than iterating through it once.
Yup, I’m guilty of having relied too readily on my assumptions. This will serve as a reminder to keep asking questions that challenge my thinking. What may seem obvious may not be so.