Skip to content

Instantly share code, notes, and snippets.

@eqdw
Created October 9, 2010 19:14
Show Gist options
  • Save eqdw/618500 to your computer and use it in GitHub Desktop.
Save eqdw/618500 to your computer and use it in GitHub Desktop.
@max_size = 0
def is_palindrome(str)
# puts " checking string #{str}"
rtn = true
i = 0
j = str.size-1
while(rtn && i<str.length && j >= 0)
puts " i,j: #{i},#{j}"
puts " chars: #{str[i,1]},#{str[j,1]}"
if str[i] != str[j]
rtn = false
end
i+=1
j-=1
end
rtn
end
@block = "fourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"
puts "length of string: #{@block.size}"
@answer = ""
0.upto(@block.length-1) do |i|
puts "i=#{i}"
(i+1).upto(@block.length-1) do |j|
puts "#{i},#{j}"
if is_palindrome(@block[i..j]) && @block[i..j].size > @max_size
@max_size = @block[i..j].size
@answer = @block[i..j]
puts "answer updated: #{@answer} new length is #{@max_size}"
end
end
end
puts "Answer: #{@answer}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment