-
-
Save anonymous/de48dca1ae4162165047 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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