Skip to content

Instantly share code, notes, and snippets.

/cs.rb Secret

Created August 31, 2010 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/de48dca1ae4162165047 to your computer and use it in GitHub Desktop.
Save anonymous/de48dca1ae4162165047 to your computer and use it in GitHub Desktop.
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