Feb09

Problem 11

comments

The basic strategy is to only check to the east, south, southeast and southwest. By checking those we are implicitly checking north, west, northwest and northeast…..

Continue reading »

Feb09

Problem 10

comments

ruby1.9

This problem uses a lot of the helper functions we have defined previously. The major changes are:

  • We define a “primes” method that returns an array of all primes than that self…..
Continue reading »

Oct07

Problem 9

comments

Not much interesting on this problem. Just brute force trial of numbers 1..999. We do trim the ranges a bit to make it run fast but that is about it.

Oct07

Problem 8

comments

data product string

This one really involves almost more string manipulation than number crunching. Notice how nicely we can get a substring using the brackets. Also notice how chars gives us each character in our substring as something we can enumerate. This means a simple Enumerable helper method to calculate the product (like previous problems did with sum) and the problem become almost a one liner…..

Continue reading »

Oct07

Problem 7

comments

prime

Most of the helper methods are just borrowed from Problem 3. What I like about this problem is that even though often blocks in Ruby provide a unique way of solving a problem you can also use more traditional control structures to express an equally elegant solution…..

Continue reading »

Oct07

Problem 6

comments

sum

This is another one where after defining a few support methods (sum from a previous problems) the actual calculation almost reads exactly as the problem describes.

Oct07

Problem 5

comments

This problem really doesn’t introduce anything previous problems have shown. But I do love the way I can use a quick block in an if statement combined with all?. Makes something that is a big loop in most languages almost feel like a keyword.

Sep22

Problem 4

comments

palindromic reverse range

This solution tries to be elegant as possible but I am not in love with two aspects:

  • The actual block that calculates the solution seems unnecessarily complex. Is there a way we can abstract some of the logic into generic methods like we did for Problem 3?….

Continue reading »

Sep21

Problem 3

comments

prime

What I like about this solution is that it consists of just adding a few extensions to the Numeric class plus a simple line that almost repeats the problem given. Ruby does actually include a prime generator through the Prime object in the mathn library. But it seemed to perform worse that even my ultra-naive solution to generating the prime numbers…..

Continue reading »

Sep21

Alias include? with match operator

comments

commentary

Often I need to test some variable against 2 or 3 values. The standard way to do it in most languages is:

This can get ugly if you start using more than 2 conditions (even if it is still a small number such as 3 or 4). Also sometimes your variable is not a variable but a chain of method calls that you don’t want to evaluate every time. For example:….

Continue reading »
Archive