Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
require "rexml/document"
file ="cia-1996.xml")
doc = 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)
(1..5).each do |i|
inflation_output << "#{i}. #{countries[-i]["name"]} #{'%.1f' % countries[-i]["inflation"]}\n"
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" }
puts alpha_by_continent_output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment