Instantly share code, notes, and snippets.

# eregon/README Created Sep 28, 2010

What would you like to do?
 This is my solution to RPCFN #13: Economics 101 http://rubylearning.com/blog/2010/08/31/rpcfn-economics-101-13 I used 3 ways to solve it: - using Hpricot and Nokogiri - using RegExp - parsing the questions (and even write them in pure Ruby (no String))
 # 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? # Idea: parse these sentences to make the search :p require 'rubygems' require 'hpricot' doc = open('cia-1996.xml') { |f| Hpricot(f) } # Countries without population (25/260) # p countries.select { |country| country['population'].nil? }.map { |country| country['name'] } # 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? # China # 1 210 004 956 countries = doc / :country # or doc.xpath('/cia/country') china = countries.max_by { |country| country[:population].to_i } puts china[:name] puts china[:population].reverse.chars.each_slice(3).map(&:join).join(' ').reverse puts # 2 What are the five countries with the highest inflation rates, and what were those rates in 1996? # {"Belarus"=>244.0, "Turkey"=>94.0, "Azerbaijan"=>85.0, "Malawi"=>83.3, "Yemen"=>71.3} cc = countries.dup p 5.times.inject({}) { |h,_| c = cc.delete cc.max_by { |c| c[:inflation].to_f } h[c[:name]] = c[:inflation].to_f h } p countries.sort_by { |country| - country[:inflation].to_f }[0...5].inject({}) { |h,c| h[c[:name]] = c[:inflation].to_f and h } 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? # ["Europe", "Asia", "North America", "Australia/Oceania", "South America", "Africa"] p continents = (doc / :continent).map { |continent| continent[:name] } puts countries_by_continent = countries.inject(Hash.new { |h,k| h[k] = [] }) { |h,country| h[country[:continent]] << country[:name] h } p Hash[countries_by_continent.sort_by { |continent, countries| countries.sort and continent }]
 # 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? # Idea: parse these sentences to make the search :p require 'nokogiri' doc = Nokogiri open 'cia-1996.xml' # Countries without population (25/260) # p countries.select { |country| country['population'].nil? }.map { |country| country['name'] } # 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? # China # 1 210 004 956 countries = (doc / :country) china = countries.max_by { |country| country[:population].to_i } puts china[:name] puts china[:population].reverse.chars.each_slice(3).map(&:join).join(' ').reverse puts # 2 What are the five countries with the highest inflation rates, and what were those rates in 1996? # {"Belarus"=>244.0, "Turkey"=>94.0, "Azerbaijan"=>85.0, "Malawi"=>83.3, "Yemen"=>71.3} cc = countries.dup p 5.times.with_object({}) { |_,h| c = cc.delete cc.max_by { |c| c[:inflation].to_f } h[c[:name]] = c[:inflation].to_f } p countries.sort_by { |country| - country[:inflation].to_f }[0...5].each_with_object({}) { |c,h| h[c[:name]] = c[:inflation].to_f } #max = Float::INFINITY #countries.inject([]) { |best, country| #} 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? # ["Europe", "Asia", "North America", "Australia/Oceania", "South America", "Africa"] p continents = (doc / :continent).map { |continent| continent[:name] } puts countries_by_continent = countries.each_with_object(Hash.new { |h,k| h[k] = [] }) { |country,h| h[country[:continent]] << country[:name] } #p Hash[countries_by_continent.sort_by { |continent, countries| countries.sort and continent }] puts p Hash[countries.group_by { |country| country[:continent] }.map { |continent,countries| [continent,countries.map { |c| c[:name] }] }.sort_by { |continent, countries| countries.sort and continent }]