RosettaCodeData/Task/Semordnilap/Ruby/semordnilap-1.rb

9 lines
322 B
Ruby
Raw Permalink Normal View History

2015-02-20 00:35:01 -05:00
dict = File.readlines("unixdict.txt").collect(&:strip)
2013-04-10 23:57:08 -07:00
i = 0
2015-02-20 00:35:01 -05:00
res = dict.collect(&:reverse).sort.select do |z|
i += 1 while z > dict[i] and i < dict.length-1
z == dict[i] and z < z.reverse
end
2013-04-10 23:57:08 -07:00
puts "There are #{res.length} semordnilaps, of which the following are 5:"
2015-02-20 00:35:01 -05:00
res.take(5).each {|z| puts "#{z} #{z.reverse}"}