Skip to content

Instantly share code, notes, and snippets.

@chrisjones
Created September 7, 2010 22:19
Show Gist options
  • Save chrisjones/569252 to your computer and use it in GitHub Desktop.
Save chrisjones/569252 to your computer and use it in GitHub Desktop.
1. The country with the highest population (1,210,004,956 people) is China.
2. Top 5 countries sorted by inflation rate:
Belarus - 244%
Turkey - 94%
Azerbaijan - 85%
Malawi - 83.3%
Yemen - 71.3%
3a. 6 continents found in factbook:
Africa
Asia
Australia/Oceania
Europe
North America
South America
3b. 260 countries found in factbook listed alphabetically by country:
EUROPE(49): Albania, Andorra, Austria, Belarus, Belgium, Bosnia and Herzegovina, Bulgaria, Croatia, Czech Republic, Denmark, Estonia, Faroe Islands, Finland, France, Germany, Gibraltar, Greece, Guernsey, Holy See, Hungary, Iceland, Ireland, Italy, Jan Mayen, Jersey, Latvia, Liechtenstein, Lithuania, Luxembourg, Macedonia, Malta, Man, Moldova, Monaco, Netherlands, Norway, Poland, Portugal, Romania, San Marino, Serbia and Montenegro, Slovakia, Slovenia, Spain, Svalbard, Sweden, Switzerland, Ukraine, United Kingdom
AFRICA(65): Algeria, Angola, Bassas da India, Benin, Botswana, Bouvet Island, Burkina Faso, Burundi, Cameroon, Cape Verde, Central African Republic, Chad, Comoros, Congo, Cote dIvoire, Djibouti, Egypt, Equatorial Guinea, Eritrea, Ethiopia, Europa Island, French Southern and Antarctic Lands, Gabon, Gambia, Ghana, Glorioso Islands, Guinea, Guinea-Bissau, Heard Island and McDonald Islands, Juan de Nova Island, Kenya, Lesotho, Liberia, Libya, Madagascar, Malawi, Mali, Mauritania, Mauritius, Mayotte, Morocco, Mozambique, Namibia, Niger, Nigeria, Reunion, Rwanda, Saint Helena, Sao Tome and Principe, Senegal, Seychelles, Sierra Leone, Somalia, South Africa, Sudan, Swaziland, Tanzania, Togo, Tromelin Island, Tunisia, Uganda, Western Sahara, Zaire, Zambia, Zimbabwe
SOUTH AMERICA(15): Argentina, Bolivia, Brazil, Chile, Colombia, Ecuador, Falkland Islands, French Guiana, Guyana, Paraguay, Peru, South Georgia and the South Sandwich Islands, Suriname, Uruguay, Venezuela
AUSTRALIA/OCEANIA(33): American Samoa, Australia, Baker Island, Cook Islands, Coral Sea Islands, Fiji, French Polynesia, Guam, Howland Island, Jarvis Island, Johnston Atoll, Kingman Reef, Kiribati, Marshall Islands, Micronesia, Midway Islands, Nauru, New Caledonia, New Zealand, Niue, Norfolk Island, Northern Mariana Islands, Palau, Palmyra Atoll, Pitcairn Islands, Solomon Islands, Tokelau, Tonga, Tuvalu, Vanuatu, Wake Island, Wallis and Futuna, Western Samoa
NORTH AMERICA(39): Anguilla, Antigua and Barbuda, Aruba, Bahamas, Barbados, Belize, Bermuda, British Virgin Islands, Canada, Cayman Islands, Clipperton Island, Costa Rica, Cuba, Dominica, Dominican Republic, El Salvador, Greenland, Grenada, Guadeloupe, Guatemala, Haiti, Honduras, Jamaica, Martinique, Mexico, Montserrat, Navassa Island, Netherlands Antilles, Nicaragua, Panama, Puerto Rico, Saint Kitts and Nevis, Saint Lucia, Saint Pierre and Miquelon, Saint Vincent and the Grenadines, Trinidad and Tobago, Turks and Caicos Islands, United States, Virgin Islands
ASIA(59): Afghanistan, Armenia, Ashmore and Cartier Islands, Azerbaijan, Bahrain, Bangladesh, Bhutan, British Indian Ocean Territory, Brunei, Burma, Cambodia, China, Christmas Island, Cocos Islands, Cyprus, Gaza Strip, Georgia, Hong Kong, India, Indonesia, Iran, Iraq, Israel, Japan, Jordan, Kazakstan, Kuwait, Kyrgyzstan, Laos, Lebanon, Macau, Malaysia, Maldives, Mongolia, Nepal, North Korea, Oman, Pakistan, Papua New Guinea, Paracel Islands, Philippines, Qatar, Russia, Saudi Arabia, Singapore, South Korea, Spratly Islands, Sri Lanka, Syria, Taiwan, Tajikistan, Thailand, Turkey, Turkmenistan, United Arab Emirates, Uzbekistan, Vietnam, West Bank, Yemen
class Factbook
require "rexml/document"
def initialize(book)
file = File.new(book)
doc = REXML::Document.new file
@world = {}
@continents = doc.root.elements.to_a("//continent").sort! { |a,b| a.attributes["name"] <=> b.attributes["name"] }
@countries = doc.root.elements.to_a("//country")
end
def find_country_with_highest_population
country_name, population = "", 0
@countries.each do |country|
if country.attributes["population"].to_i > population
country_name, population = country.attributes["name"], country.attributes["population"].to_i
end
end
puts "1. The country with the highest population (#{number_with_delimiter(population)} people) is #{country_name}."
end
def find_top_5_countries_by_inflation_rate
@countries.sort! { |a,b| b.attributes["inflation"].to_f <=> a.attributes["inflation"].to_f }
puts "\n2. Top 5 countries sorted by inflation rate:"
@countries[0..4].each do |country|
puts " #{country.attributes["name"]} - #{country.attributes["inflation"]}%"
end
end
def list_continents
puts "\n3a. #{@continents.count.to_s} continents found in factbook:"
@continents.each { |continent| puts " #{continent.attributes["name"]}" }
end
def list_countries_by_continent
order_countries
puts "\n3b. #{@countries.count.to_s} countries found in factbook listed alphabetically by country:\n\n"
@world.each_pair { |continent, countries| very_pretty_print(continent,countries) }
end
private
def number_with_delimiter(number, delimiter=",")
number.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
end
def order_countries
@continents.each do |continent|
temp_continent = []
@countries.each do |country|
if country.attributes["continent"] == continent.attributes["name"]
temp_continent << country.attributes["name"]
end
end
@world[continent.attributes["name"]] = temp_continent.sort! { |a,b| a <=> b }
end
end
def very_pretty_print(continent, countries)
print "#{continent.upcase}(#{countries.count.to_s}):"
last_country = countries.pop
countries.collect! { |c| " #{c}," }
puts "#{countries.to_s} #{last_country}"
end
end
factbook = Factbook.new("cia-1996.xml")
factbook.find_country_with_highest_population
factbook.find_top_5_countries_by_inflation_rate
factbook.list_continents
factbook.list_countries_by_continent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment