Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 35 lines (32 sloc) 0.868 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#!/usr/bin/ruby -w
require 'rubygems'

def is_palindrome?(val)
  len = val.size
  center = len / 2
  center_index = val.size.odd? ? center : center-1
  val[0..center_index] == val[center..(val.size-1)].reverse
end

def find_longest_palindrome_a(input)
  len = input.size
  len.downto(1) { |sublen|
    for index in 0..(len-sublen)
      s = input[index..(index+sublen)]
      return s if is_palindrome?(s)
    end
  }
  '(NO PALINDROME FOUND)'
end

if ARGV.size != 1
  puts "Usage: find_longest_palindrome_assume_long (filename)"
else
  file = File.open("#{ARGV[0]}", "rb")
  contents = file.read
  puts "Processing file..."
  t1 = Time.now
  puts "LONGEST PALINDROME: #{find_longest_palindrome_a(contents)}"
  t2 = Time.now
  total = t2 - t1
  rate = contents.size / total
  puts "Time to process: #{total} sec"
  puts "Processing rate: #{rate} characters / sec"
end
Something went wrong with that request. Please try again.