secret
Last active

  • Download Gist
programming_challenge.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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
=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
programming_challenge.txt
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
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.