Skip to content

@CapnCurry /marktabler.rb secret
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#RPCFN #13 challenge - Economics 101
#Written by Mark Tabler
require 'nokogiri'
class Country
attr_accessor :name, :population, :inflation, :continent
def initialize (name, continent, population = nil, inflation = nil)
@name = name
@continent = continent
@population = population
@inflation = inflation
end
end
class Worldbook
attr_accessor :countries, :xml_tree
def initialize(doc_name)
begin
@countries = []
@xml_tree = Nokogiri::XML(File.read(doc_name)).xpath("/cia/country")
read_worldbook
rescue IOError => e
abort "Unable to read file #{doc_name}, aborting."
end
end
def read_worldbook
@xml_tree.each do |country|
name = country.attributes['name'].value
population = country.attributes['population'].value rescue nil
inflation = country.attributes['inflation'].value rescue nil
continent = country.attributes['continent'].value rescue nil
add_country(name, continent, population, inflation)
end
end
def add_country (name, continent, population, inflation)
@countries << Country.new(name.to_s, continent.to_s, population.to_i, inflation.to_i)
end
def high_population
@countries.sort_by!{ |country| country.population }.reverse!
@countries.first
end
def high_inflation
@countries.sort_by!{ |country| country.inflation }.reverse!
@countries.values_at(0..4)
end
def continent_list
@countries.sort_by!{ |country| country.name }
continent_list = {}
@countries.each do |country|
(continent_list[country.continent] ||= []) << country.name
end
continent_list
end
end
def add_commas(n)
n.to_s.reverse.scan(/\d{1,3}/).join(',').reverse
end
wb_1996 = Worldbook.new("cia-1996.xml")
puts "\nHighest Population was held by #{wb_1996.high_population.name}"
puts "Its population was #{add_commas(wb_1996.high_population.population)}"
puts "\nThe five nations with the highest inflation index in 1996 were: "
wb_1996.high_inflation.each do |country|
puts "#{country.name} with #{country.inflation} percent inflation"
end
puts "\nHere is a list of each continent in the 1996 World Factbook, \n"
puts "coupled with a list of each country on that continent.\n\n"
wb_1996.continent_list.each do |continent, countries|
puts "#{continent}:"
puts countries.join(", ")
end
__END__
Highest Population was held by China
Its population was 1,210,004,956
The five nations with the highest inflation index in 1996 were:
Belarus with 244 percent inflation
Turkey with 94 percent inflation
Azerbaijan with 85 percent inflation
Malawi with 83 percent inflation
Yemen with 71 percent inflation
Here is a list of each continent in the 1996 World Factbook,
coupled with a list of each country on that continent.
Asia:
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
Europe:
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:
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
Australia/Oceania:
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:
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
South America:
Argentina, Bolivia, Brazil, Chile, Colombia, Ecuador, Falkland Islands, French Guiana, Guyana, Paraguay, Peru, South Georgia and the South Sandwich Islands, Suriname, Uruguay, Venezuela
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.