Create a gist now

Instantly share code, notes, and snippets.

anonymous /cs.rb Secret
Created Aug 31, 2010

require 'rubygems'
require 'hpricot'
doc= Hpricot(open('cia-1996.xml'))
# What is the population of the country with the most people?
r={}
(doc/"/cia/country").each{|n|r[n[:name]]=n[:population].to_i}
r.to_a.sort_by{|a,b|b}.reverse.first
# What are the five countries with the highest inflation rates,
# and what were those rates in 1996?
r={}
(doc/"/cia/country").each{|n|r[n[:name]]=n[:inflation].to_i}
puts r.to_a.sort_by{|a,b|b}.reverse[0..4]
# What are the six continents in the file and which
# countries belong to which continent?
# Can you also produce them in alphabetical order?
r=[]
(doc/"/cia/country").each{|n|r<<[n[:name],n[:continent]]}
r = r.sort_by{|a,b|b + a}
r.each{|e|puts "#{e[1]} #{e[0]}"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment