Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created September 12, 2011 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/1212567 to your computer and use it in GitHub Desktop.
Save havenwood/1212567 to your computer and use it in GitHub Desktop.
Find Palindromes
def split_up string
@array = string.downcase.split ''
end
def find_palindromes
@palindromes = []
1.upto @array.size do |spot|
look = 1
while @array[spot - look] == @array[spot + look]
@palindromes << @array[spot - look, look * 2 + 1].join
look += 1
end
end
@palindromes = @palindromes.uniq
end
def find_longest_palindrome
@palindromes.max_by &:size
end
def sort_palindromes
@palindromes.sort_by &:size
end
split_up 'FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth'
find_palindromes
find_longest_palindrome
sort_palindromes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment