Created
December 23, 2010 11:23
-
-
Save otobrglez/752847 to your computer and use it in GitHub Desktop.
Economics challenge for (RPCFN: Economics 101 (#13)) - Oto Brglez
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'nokogiri' | |
class World | |
attr_reader :continents | |
def initialize(world_xml="cia-1996.xml") | |
@continents = [] | |
if File.exists?(world_xml) | |
@noko = Nokogiri::XML(File.open world_xml ) | |
@continents = @noko.xpath("/cia/continent").map do |n| | |
c = Continent.new :id => n['id'], :name => n['name'] | |
@noko.xpath("/cia/country[@continent='#{c.to_s}']").each do |country| | |
c.countries.push Country.new :name => country['name'], :population => country['population'], :inflation => country['inflation'] | |
end | |
c.countries.sort!{ |a,b| a.name.downcase <=> b.name.downcase } | |
c | |
end | |
@continents.sort!{ |a,b| a.name.downcase <=> b.name.downcase } | |
end | |
end | |
def country_with_biggest_population | |
continents.collect{ |c| c.countries }.flatten.max{ |a,b| a.population.to_i <=> b.population.to_i } | |
end | |
def countries_with_highest_inflation_rates | |
continents.collect{ |c| c.countries }.flatten.sort { |a,b| b.inflation.to_f <=> a.inflation.to_f }.slice(0,5) | |
end | |
end | |
class Continent | |
attr_reader :countries, :id, :name | |
def initialize(params=nil) | |
@countries = [] | |
@id = params[:id] if params[:id] | |
@name = params[:name] if params[:name] | |
end | |
def to_s | |
"#{@name}" | |
end | |
end | |
class Country | |
attr_reader :name, :population, :inflation | |
def initialize(params=nil) | |
@name = params[:name] if params[:name] | |
@population = params[:population].to_i if params[:population] | |
@inflation = params[:inflation].to_f if params[:inflation] | |
end | |
def to_s | |
"#{@name}" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -w | |
require 'test/unit' | |
require './economics' | |
class EconomicsTest < Test::Unit::TestCase | |
def setup | |
@world = World.new | |
end | |
def test_world | |
assert_equal @world.continents.count.to_s, "6" | |
end | |
def test_number_of_countries_of_first_continent | |
assert_not_equal @world.continents.first.countries.count, "0" | |
end | |
def test_continents_order | |
assert_equal @world.continents.first.name.downcase , "africa" | |
end | |
def test_is_china_the_biggest | |
assert_equal @world.country_with_biggest_population.name , "China" | |
end | |
def test_countries_with_highest_inflation_rates | |
assert_equal %w(Belarus Turkey Azerbaijan Malawi Yemen), @world.countries_with_highest_inflation_rates.map(&:name) | |
end | |
def test_task_one | |
puts " | |
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?" | |
puts " | |
Answer: People in china : #{ @world.country_with_biggest_population.population }" | |
end | |
def test_task_two | |
puts " | |
2) What are the five countries with the highest inflation rates, and what were those rates in 1996?" | |
puts " | |
Answer: Five countries with the highest inflation rates are:" | |
@world.countries_with_highest_inflation_rates.each do |country| | |
puts "\t#{country} - #{country.inflation}" | |
end | |
end | |
def test_task_three | |
puts" | |
3) What are the six continents in the file and which countries belong to which continent? | |
Can you also produce them in alphabetical order?" | |
@world.continents.each do |continent| | |
puts "\n\t #{continent}: \n" + continent.countries.map(&:name).join(", ") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment