require 'rubygems' | |
require 'nokogiri' | |
# Enumerable#max_by and Enumerable#group_by are implemented in Ruby 1.8.7 and Ruby 1.9. I want this to work in | |
# Ruby 1.8.6 also, just for the heck of it | |
unless Enumerable.instance_methods.include?(:max_by) | |
module Enumerable | |
def max_by(&block) | |
map{|elem| [block[elem], elem]}.max.last | |
end | |
def group_by | |
result = Hash.new {|hash,key| hash[key] = [] } | |
each do |elem| | |
result[yield elem] << elem | |
end | |
result | |
end | |
end | |
end | |
File.open(ARGV[0]) do |f| | |
xml_doc = Nokogiri::XML::Document.parse(f) | |
countries = xml_doc.css('country') | |
most_populous = countries.max_by {|node| node['population'].to_i} | |
puts "The most populous country in 1996 was #{most_populous['name']} with a population of #{most_populous['population']}" | |
puts | |
puts "The five countries with the highest inflation rate in 1996 were:" | |
countries.sort_by {|country| -(country['inflation'] || 0).to_f} [0..4].each do |country| | |
puts " #{country['name']} - #{country['inflation']}%" | |
end | |
continent_info = countries.group_by {|country| country['continent']} | |
puts | |
puts "The continents and their countries in 1996 were:" | |
continent_info.keys.sort.each do |continent| | |
continent_info[continent].sort_by {|country| | |
country['name']}.each do |country| | |
puts " #{country['name']}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment