Problem 4

palindromic reverse range

Tue Sep 22 17:34:43 -0700 2009

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?
  • Ruby’s Range object doesn’t support a reverse order. So 10..1 turns out to be equivalent to an empty array. This seems like an ugly wart of Ruby. We try to resolve the problem by sub-classing Range as a new object called ReverseRange which works the opposite direction. I even monkey-patch a method called reverse on the Range object which creates a ReverseRange based on the current range. This way we can still us the literal notation. I think (100..999).reverse is more elegant than ReverseRange.new 999, 100. But all this seems unnecessary. I kind of want to use downto but that doesn’t allow me to collect or find. Any other suggestions?
blog comments powered by Disqus