fork download
  1. INPUT = 'FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth'
  2. chars = INPUT.split('')
  3. max = 0
  4. offset = 0
  5. to_push = []
  6.  
  7. chars.each_with_index do |c, n|
  8. next if n == 0 or n == chars.length
  9. count = 0
  10. i = 0
  11. loop do
  12. if chars[n + i] == chars[n - i]
  13. count += 1
  14. i += 1
  15. else
  16. break
  17. end
  18. end
  19. if count > max
  20. max = count
  21. offset = n
  22. end
  23. # p "#{c}: #{count} (offset: #{n})"
  24. end
  25.  
  26. max.times do |i|
  27. to_push << chars[offset + i]
  28. end
  29.  
  30. to_push.reverse!
  31. # Slightly incorrect output if double center chars, but love it, it works.
  32. # Lunch break is now over and I'll find a fix to that bug later.
  33. to_push = to_push + to_push[0..(to_push.length / 2)].reverse
  34. p to_push.join
Success #stdin #stdout 0.02s 7436KB
stdin
Standard input is empty
stdout
"ranynar"