From: Jay Levitt [mailto:jay+news / jay.fm] # On Wed, 26 Sep 2007 14:57:04 -0700, William James wrote: # > ary = DATA.readlines # > ary.map{|s| s[/\d+$/] }.uniq.each{|server| # > db = ary.grep(/ #{server}$/).map{|s| # > s[/\d+/].to_i}.sort # > puts "Server #{server} databases #{db[0]} to #{db[-1]}" # > } # Definitely wins the "shortest" prize! i was impressed by James and Raf's solutions, so i combined the two, together using 1.9's group_by, so C:\ruby1.9\bin>ruby test.rb Server Databases 3 1 to 2, 133 7 8 to 10, 20, 30 to 33 9 5 144 3 to 4 C:\ruby1.9\bin>cat test.rb require 'set' module Enumerable def ranger to_set.divide {|i,j| (i - j).abs == 1}. map{|s| s.to_a.sort}.sort_by{|s| s.first} end end ary = DATA.readlines.map{|s| s.scan(/\d+/).map{|s| s.to_i}} puts "Server\tDatabases" ary.group_by{|a| a.last}.sort.each do |server,pairs| db=pairs.map{|a| a.first}.ranger. map{|a| "#{a.first}#{" to #{a.last}" if a.size>1}"}.join(", ") puts "#{server}\t#{db}" end __END__ database 8 is on server 7 database 10 is on server 7 database 9 is on server 7 database 5 is on server 9 database 1 is on server 3 database 2 is on server 3 database 133 is on server 3 database 4 is on server 144 database 3 is on server 144 database 20 is on server 7 database 30 is on server 7 database 31 is on server 7 database 32 is on server 7 database 33 is on server 7 C:\ruby1.9\bin> kind regards -botp