Created
January 9, 2012 08:42
-
-
Save andreapavoni/1581987 to your computer and use it in GitHub Desktop.
therubygame palindrome solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_longest_palindrome(string) | |
(string.size/2).downto(1) do |n| | |
if m = string.match(Regexp.new("#{'(.)' * (n+1)}?\\#{n.downto(1).to_a.join('\\')}")) | |
return m | |
end | |
end | |
end |
Argh! thank you for the explanation. For ruby 1.9.x looks like it's a sort of bug (even if I know it's almost weird to use very long regex). I know it's not as fast as other ways, but it was fun to test alternatives ;)
cheers!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow, I didn't realise Ruby supported backrefs > 9. Anyway, it looks like your code might be failing on longer strings. I was able to get it to fail with runtime errors for two different reasons depending on the version of Ruby used:
Code to exercise the failure:
Ruby 1.9.{2,3}
Ruby 1.8.7