secret
Created

  • Download Gist
fortenberry_christopher.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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
require "rexml/document"
file = File.new("cia-1996.xml")
doc = REXML::Document.new file
 
# puts all of the xml information about the countries into an array of hashes
# This hash is used in all of the exercises
countries = []
doc.elements.each("cia/country") { |country| countries << country.attributes }
 
 
# sorts all the countries by population and lists the most populous country and its population
countries.sort_by! { |c| c["population"].to_i }
most_populous_country = countries[-1]
puts "The most populous country on the Earth is #{most_populous_country["name"]} with a population of #{most_populous_country["population"]} as of 1996."
 
 
# sorts all the countries by inflation rate and lists the five highest countries with their rates
# work on formatting for final output
inflation_array = countries.sort_by { |c| c["inflation"].to_f }.reverse
countries.sort_by! { |c| c["inflation"].to_f }
inflation_output = <<EOF
=============================================
Countries with Highest Inflation Rates (1996)
=============================================
EOF
 
(1..5).each do |i|
inflation_output << "#{i}. #{countries[-i]["name"]} #{'%.1f' % countries[-i]["inflation"]}\n"
end
puts inflation_output
 
 
# sorts all the countries by continent and then alphabetizes countries by continent
continents = []
doc.elements.each("cia/continent") { |continent| continents << continent.attributes }
continents.sort_by! { |continent| continent["name"] }
 
alpha_by_continent_output = ''
continents.each do |continent|
alpha_by_continent_output << "\n" << continent["name"] << "\n=================\n"
countries_in_continent = countries.find_all { |country| country["continent"] == continent["name"] }.sort_by { |country| country["name"] }
countries_in_continent.each { |country| alpha_by_continent_output << country["name"] << "\n" }
end
puts alpha_by_continent_output

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.