secret
Created

  • Download Gist
rpcfn13.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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
require "rexml/document.rb"
include REXML
XMLFILE = "cia-1996.xml"
 
class CIAFile1996
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Parse xml file and create instance variable @world (REXML::Element),
# which we will use throughout our class methods.
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def initialize
xmlfile = File.new(XMLFILE)
doc = Document.new xmlfile
@world = doc.root
end
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# This method searches country with the highest population and returns
# result in the form of an array with 2 objects (first object is String
# object representing name of the country, while second object is Integer
# (actually Bignum...) object representing population number for that country).
#
# Example:
# max_population = ["China", 1210004956]
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def highestPopulationCountry
max_population = ["country name", 0]
@world.elements.each { |country|
if country.attributes["population"].to_i > max_population[1].to_i
max_population[0] = country.attributes["name"]
max_population[1] = country.attributes["population"].to_i
end
}
return max_population
end
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# This method first creates Hash collection containing countries (keys) and
# their inflation rates (values). It then sorts Hash by element values, and
# then creates Array object with [country_name, inflation_value] element pairs.
#
# Example:
# a[0] = ["Belarus",244.0]
# a[1] = ["Turkey",94.0]
# ...
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def topHighestInflationRates(nr)
temp_arr = Hash.new
@world.elements.each { |country|
if country.attributes["inflation"]
temp_arr[country.attributes["name"]] = country.attributes["inflation"].to_f
end
}
temp_arr = temp_arr.sort{|a,b| b[1]<=>a[1]}
a = Array.new
counter = 0
temp_arr.each { |country, value|
(counter+=1)>=(nr+1) ? break : a.push([country, value])
}
return a
end
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# This method creates Hash collection with key values representing continets
# names. Values of Hash elements are arrays with String objects representing
# country name. Arrays are sorted alphabetically.
#
# Example:
# a["Africa"] = ["Algeria", "Angola", "Bassas da India" ... etc]
# a["Europe"] = ["Albania", "Andorra", "Austria" ... etc]
# ...
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def listCountriesByContinent
a = Hash.new
@world.elements.each{ |country|
if country.attributes["continent"]
if a[country.attributes["continent"]].class == Array
a[country.attributes["continent"]].push(country.attributes["name"])
else
a[country.attributes["continent"]] = Array.new
a[country.attributes["continent"]].push(country.attributes["name"])
end
end
}
a.each { |continent, countries|
countries.sort!
}
return a
end
 
end
test_rpcfn13.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
require "rpcfn13.rb"
require "test/unit.rb"
 
class REXMLParseTest < Test::Unit::TestCase
@@xmlFile = CIAFile1996.new
def test1
puts "Testing highestPopulationCountry method..."
a = @@xmlFile.highestPopulationCountry
assert_equal "China", a[0]
assert_equal 1210004956, a[1]
end
def test2
puts "Testing topHighestInflationRates method..."
a = @@xmlFile.topHighestInflationRates(5)
assert_equal "Belarus", a[0][0]
assert_equal 244.0, a[0][1]
assert_equal "Turkey", a[1][0]
assert_equal 94.0, a[1][1]
assert_equal "Azerbaijan", a[2][0]
assert_equal 85.0, a[2][1]
assert_equal "Malawi", a[3][0]
assert_equal 83.3, a[3][1]
assert_equal "Yemen", a[4][0]
assert_equal 71.3, a[4][1]
end
 
def test3
puts "Testing listCountriesByContinent method..."
a = @@xmlFile.listCountriesByContinent
assert a["Africa"].include?("Morocco")
assert a["Europe"].include?("Serbia and Montenegro")
assert a["South America"].include?("Chile")
assert a["Australia/Oceania"].include?("Niue")
assert a["North America"].include?("Clipperton Island")
assert a["Asia"].include?("Russia")
assert_equal "Algeria", a["Africa"][0]
assert_equal "Albania", a["Europe"][0]
assert_equal "Argentina", a["South America"][0]
assert_equal "American Samoa", a["Australia/Oceania"][0]
assert_equal "Anguilla", a["North America"][0]
assert_equal "Afghanistan", a["Asia"][0]
assert_equal 260, a["Africa"].length + a["Europe"].length + a["South America"].length + a["Australia/Oceania"].length + a["North America"].length + a["Asia"].length
assert_equal 6, a.keys.length
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.