Find the Longest Palindrome in a String

I recently discovered programmingpraxis.com, and one of the more recent posts dealt with one of the greplin programming challenges. I figured since people were posting their code there, then it wouldn’t be too bad for me to post mine here!

Find the Longest Palindrome in a string:

I doubt this is an optimal solution, but I like how it works:

text = "FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"

def find_longest_palindrome s1, size
	longest = ""

	s1.size.times do |start|
		break if start + size > s1.size
		s2 = s1[start, size].reverse
		if s1.include? s2
			return s2
		end
	end

	find_longest_palindrome s1, size - 1
end

puts find_longest_palindrome(text, text.length)

I found the site via this post, 10 puzzle websites to sharpen your programming skills. Now that the wedding’s over (pics soon!) I can’t wait to get back to a regular extracurricular programming schedule!