Skip to content

Instantly share code, notes, and snippets.

@pheuter
Created October 8, 2010 22:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pheuter/617676 to your computer and use it in GitHub Desktop.
Save pheuter/617676 to your computer and use it in GitHub Desktop.
str = "FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"
longest = ""
0.upto(str.length) do |len|
0.upto(len) do |sublen|
tmp = str[sublen..len]
longest = tmp if tmp.length > longest.length and tmp == tmp.reverse
end
end
puts longest
@Omnipresent
Copy link

good approach but you should start with 0.upto(str.length-1) or else your code will not work on a easiest of strings like "huh"

@pheuter
Copy link
Author

pheuter commented Oct 13, 2010

right ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment