secret
anonymous / cs.rb
Created

  • Download Gist
cs.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
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]}"}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.