Skip to content

Instantly share code, notes, and snippets.

@rodacato
Created September 8, 2010 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodacato/31f78b38c9f2659ba2f9 to your computer and use it in GitHub Desktop.
Save rodacato/31f78b38c9f2659ba2f9 to your computer and use it in GitHub Desktop.
=begin
Application for respond the rubylearning challenge.
=end
require 'rexml/document'
class FacebookData
def initialize
file = File.new "cia-1996.xml"
doc = REXML::Document.new file
@population = {}
@inflation = [["",""]]
@continents = {}
doc.elements.each("*/continent") { |element| @continents.merge!({ element.attributes["name"] => [] }) }
doc.elements.each("*/country") do |element|
@population = { :name => element.attributes["name"], :population => element.attributes["population"]} if element.attributes["population"] && element.attributes["population"].to_i > @population[:population].to_i
@inflation << [ element.attributes["inflation"], element.attributes["name"]] if element.attributes["inflation"] && element.attributes["inflation"].to_f > @inflation.last[1].to_f
@continents[element.attributes["continent"]].push << element.attributes["name"]
end
end
def get_biggest_population
@population
end
def get_continents
@continents
end
def get_continents_sorted
@continents.sort
end
def get_biggest_inflation
@inflation.sort{|x,y| y[0].to_f <=> x[0].to_f }[0..4]
end
end
require 'rubygems'
gem 'test-unit', '>2.1'
require 'test/unit'
class MyTest < Test::Unit::TestCase
DATA = FacebookData.new
test "Testing Population" do
assert_equal DATA.get_biggest_population[:name], 'China'
end
test "Testing inflation" do
assert_equal DATA.get_biggest_inflation.length, 5
end
test "Testing continents " do
continents = ['Africa', 'Asia', 'Australia/Oceania', 'Europe', 'North America', 'South America']
assert_equal DATA.get_continents.length, 6
continents.each{|continent| assert_equal DATA.get_continents.include?(continent), true, "Fail #{continent} continent #{DATA.get_continents}" }
end
end
As a developer it helps to be able to understand a client’s perspective and to build suitable applications to help them in their field. This means knowing a bit about the world. We’ll help this background knowledge by doing looking at some economic data, and also testing our XML parsing skills.
The file cia-1996.xml is the data from the CIA World Factbook of 1996 in XML format. It has details about 260 countries across five continents. Your challenge, should you choose to accept it, is to uncover the following details buried within this file:
1. What is the population of the country with the most people? Yes, we know it’s China, but just how many people lived there in 1996?
2. What are the five countries with the highest inflation rates, and what were those rates in 1996?
3. What are the six continents in the file and which countries belong to which continent? Can you also produce them in alphabetical order?
Once you’ve worked out how to do part (2), then you can do anything with this file; all you need is a bit of time. Knowing how to do (2) you could then do (3) without too much effort.
ANSWERS:
Country with more population: China with 1210004956 persons.
Countries with biggest inflation:
244 - Belarus
94 - Turkey
85 - Azerbaijan
83.3 - Malawi
71.3 - Yemen
Continents with countries:
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
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,North Korea,South Korea,Kuwait,Kyrgyzstan,Laos,Lebanon,Macau,Malaysia,Maldives,Mongolia,Nepal,Oman,Pakistan,Papua New Guinea,Paracel Islands,Philippines,Qatar,Russia,Saudi Arabia,Singapore,Spratly Islands,Sri Lanka,Syria,Taiwan,Tajikistan,Thailand,Turkey,Turkmenistan,United Arab Emirates,Uzbekistan,Vietnam,West Bank,Yemen
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
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
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