Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 36 lines (32 sloc) 868 Bytes
#!/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.