RPCFN: Economics 101 (#13)

by Satish Talim on August 31, 2010

Ruby Programming Challenge For Newbies

RPCFN: Economics 101 (#13)

By Dr. Bruce Scharlau

About Dr. Bruce Scharlau

Dr. Bruce Scharlau In Dr. Bruce’s own words: “I’ve been using and teaching Ruby since trying out the cookbook example in the summer of 2006. As soon as I saw how much easier it all was with Ruby and Rails, I was hooked. I now try to do as much with Ruby as I can with my teaching and own work. It’s a joy to code with Ruby compared to using other languages, which don’t seem as intuitive by comparison. When I’m not busy working, then I try to spend time with the family, or get out sailing.”

Dr. Bruce has this to say about the challenge:

The challenge is useful for newbies as a way to extend their skills in a useful manner. They will learn how they solved the problem, and also gain from seeing how others solved the problem too. We all start from different places when we solve problems, so the ‘obvious’ solution to you, might not occur to someone else who has a different experience of Ruby. This is why it’s good to share examples and code together when possible too.


  • The participant with the best Ruby solution (if there is a tie between answers, then the one who posted first will be the winner) will be awarded any one of PeepCode’s Ruby on Rails screencasts.
  • From the remaining working Ruby solutions, three participants would be selected randomly and each one would be awarded any one of Pragmatic’s The Ruby Object Model and Metaprogramming screencasts.

The four persons who win, can’t win again in the next immediate challenge but can still participate.

The Ruby Challenge


The Challenge

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.

You can use any XML library. I used REXML as it’s already there if you have Ruby installed; so don’t need to worry about any gem installs. You may also want to look at how REXML uses XPath.

Submit your solution of your code, which includes a test file that answers the three questions.

How to Enter the Challenge

Read the Challenge Rules. By participating in this challenge, you agree to be bound by these Challenge Rules. It’s free and registration is optional. You can enter the challenge just by posting the following as a comment to this blog post:

  1. Your name:
  2. Country of Residence:
  3. GIST URL of your Solution (i.e. Ruby code) with explanation and / or test cases:
  4. Code works with Ruby 1.8 / 1.9 / Both:
  5. Email address (will not be published):
  6. Brief description of what you do (will not be published):


  • As soon as we receive your GIST URL, we will fork your submission. This means that your solution is frozen and accepted. Please be sure that is the solution you want, as it is now recorded in time and is the version that will be evaluated.
  • All solutions posted would be hidden to allow participants to come up with their own solutions.
  • You should post your entries before midnight of 27th Sept. 2010 (Indian Standard Time). No new solutions will be accepted from 28th Sept. onwards.
  • On 28th Sept. 2010 all the solutions will be thrown open for everyone to see and comment upon.
  • The winning entries will be announced on this blog before 30th Sept. 2010. The winners will be sent their prizes by email.

More details on the RPCFN?

Please refer to the RPCFN FAQ for answers to the following questions:


RPCFN is entirely financed by RubyLearning and sometimes sponsors, so if you enjoy solving Ruby problems and would like to give something back by helping with the running costs then any donations are gratefully received.

Click here to lend your support to: Support RubyLearning With Some Love and make a donation at www.pledgie.com !


Special thanks to:

  • Dr. Bruce Scharlau.
  • GitHub, for giving us access to a private repository on GitHub to store all the submitted solutions.
  • The RubyLearning team.


Contact Satish Talim at satish [dot] talim [at] gmail.com OR if you have any doubts / questions about the challenge (the current problem statement), please post them as comments to this post and the author will reply asap.

The Participants

There are two categories of participants. Some are vying for the prizes and some are participating for the fun of it.

In the competition

  1. Dmytrii Nagirniak, Australia
  2. Kirill Shchepelin, Russia
  3. Lukasz Hanuszczak, Poland
  4. Rick DeNatale, USA
  5. David Lake, England
  6. Julio C. Villasante, Cuba
  7. Dan Wanek, USA
  8. Matthew Dahl, USA
  9. Mark Tabler, USA
  10. Kalle Lindström, Sweden
  11. Chris Jones, USA
  12. Tamas Szabo, Australia
  13. Adrian Castillo, Mexico
  14. Norman E. White, USA
  15. Himansu Desai, USA
  16. Samnang Chhun, Cambodia
  17. Predrag Bradaric, Serbia
  18. Jonathan, Mexico
  19. Christopher Fortenberry, USA
  20. Brad O’Connor, Australia

Just for Fun

  1. Casimir Saternos, USA
  2. Paul McKibbin, U.K.

The Winners


Congratulations to the winners of this Ruby Challenge. They are:

Previous Challenge

RPCFN: Cycle Tracks (#12) by David Griffiths.

Note: All the previous challenges, sponsors and winners can be seen on the Ruby Programming Challenge for Newbies page.


  • This challenge is now closed.
  • The (#14) challenge by Joseph Wilk, U.K. is scheduled for Oct. 2010.

Technorati Tags: , , , , ,

Posted by Satish Talim

{ 53 comments… read them below or add one }

Dmytrii Nagirniak August 31, 2010 at 5:01 pm

For the lazzy bustards like me it would be nice to have the answers for our tests :)

I don’t want to go through the 6k lines file manually and running the solution against it would not be a real TDD :)


Julio C. Villasante August 31, 2010 at 11:31 pm

That’s true, I’ve already solved (pretty roughly actually) the challenge at work (in lunch time of course) but I’m waiting to get home to put some tests together cause i feel it will take me some time. So, having a test case already prepared would be nice.


Dmytrii Nagirniak August 31, 2010 at 5:10 pm

Also how the question “which countries belong to which continent?” should be answered in the test?
Should we write a test(s) to match all of the countries to appropriate continent? Or one or two countries from the continent is enough?


Dmytrii Nagirniak August 31, 2010 at 7:42 pm

Solution (#1):

Dmytrii Nagirniak, Australia
Code works with 1.9.2-p0:


Scharlau, Dr Bruce A. September 28, 2010 at 6:49 am

Uses ‘world’ so could compare different models, and tests.


Kirill Shchepelin September 1, 2010 at 1:33 am

Solution (#2):

Kirill Shchepelin, Russia


Scharlau, Dr Bruce A. September 28, 2010 at 6:52 am

Short direct code, no classes, no tests, only puts.


Lukasz Hanuszczak September 1, 2010 at 1:53 am

Solution (#3):

Lukasz Hanuszczak, Poland
I discovered Ruby this holidays and REXML in this entry so I’m sure that it could be achived in thousands more optimized and shorter ways.


Lukasz Hanuszczak September 1, 2010 at 1:58 am

As I see I can’t use polish diacritic letters. So my first name would be “Lukasz”.


Scharlau, Dr Bruce A. September 28, 2010 at 6:53 am

Short direct code, no classes, no tests and only puts.


Rick DeNatale September 1, 2010 at 2:16 am

Solution (#4):

Rick DeNatale
This works on Ruby 1.8.6, 1.8.7 and 1.9.2. It includes a conditional definition of two enumerable methods missing from 1.8.6


Scharlau, Dr Bruce A. September 28, 2010 at 6:54 am

Short code using blocks with no tests.


EzGraphs September 1, 2010 at 4:58 am

Solution (#5):

I am a full time programmer – including ruby.
Participating for fun (helpful for learning the best way to do things) – please award to another.


Scharlau, Dr Bruce A. September 28, 2010 at 6:54 am

Most concise code using arrays, no tests.


Paul McKibbin September 1, 2010 at 6:54 am

I like this one. Real world, no answers given so you have to work out yourself if it is working or not. Lots of data to play with. I wanted to play around with Nokogiri’s XML library in any case, and this gives me a challenge to use it with. Thanks.


Bruce September 1, 2010 at 12:50 pm

Glad you like the ‘real world’ feel. I always like to make exercises as real as appropriate so you know what you’re up against, instead of always working on trivial examples.


Paul McKibbin September 1, 2010 at 7:17 am

Solution (#6):

Paul McKibbin, U.K.
Just for fun
Only tested with 1.8.7. Should also work under 1.9.


Scharlau, Dr Bruce A. September 28, 2010 at 6:55 am

Does more than is asked for in klunky manner, no tests.


Bruce September 1, 2010 at 12:46 pm

The test case is rather straightforward. I assumed that you’d do what I did initially. Run some puts statements to see what’s in there and then start writing some tests to check that you got what you wanted.

rc_parse is the file I used for parsing by the way.
However, here’s the test case such as it is:

#test parse
require 'rc_parse'
require 'test/unit'

class TestRCParse < Test::Unit::TestCase

def test_country
c = Country.new("Scotland", 5555555, 3.3)
assert_equal("Scotland", c.name)

def test_parsing
rc = RC.new

assert_equal("China has the largest population at 1210004956", rc.rc_population)
inflation_list = rc.sort_inflation
inflation_list.each {|c |
# puts "#{c.name} has inflation of #{c.inflation}"
assert_equal("Belarus", rc.sort_inflation[0].name)
continent = rc.list_continent("Europe")
assert_equal("Austria", continent[2])
assert_equal(260, rc.rc_total_countries)
assert_equal(6, rc.rc_total_continents)


David Lake September 1, 2010 at 3:26 pm

Solution (#7):

David Lake, England
Code works with Ruby 1.9.2, tested on Windows


Scharlau, Dr Bruce A. September 28, 2010 at 6:55 am

Does more than is asked for in klunky manner and no tests.


Julio C. Villasante September 1, 2010 at 6:38 pm

Solution (#8):

Julio C. Villasante, Cuba
Tested in 1.9 only
Simple solution that uses Nokogiri with a little report to visualize the asked questions. Also some tests (RSpec) are included. This was a fun one, very real world challenge. Thanks Bruce.


Scharlau, Dr Bruce A. September 28, 2010 at 6:55 am

Prints formated report to screen with answer and rspec.


zenchild September 1, 2010 at 11:37 pm

Solution (#9):

Dan Wanek, USA
Works with Both 1.8 and 1.9


Scharlau, Dr Bruce A. September 28, 2010 at 6:56 am

Neatly done with good use of ruby qualities and rspec tests.


Matthew Dahl September 2, 2010 at 6:05 am

Solution (#10):

Matthew Dahl, USA


Scharlau, Dr Bruce A. September 28, 2010 at 6:57 am

Short direct code, no classes, no tests at all and only puts.


CapnCurry September 7, 2010 at 3:46 am

Solution (#11):

Mark Tabler, USA
Works with 1.9, should work also with 1.8


Scharlau, Dr Bruce A. September 28, 2010 at 6:57 am

Several classes and method, no tests.


Kalle Lindström September 7, 2010 at 1:21 pm

Solution (#12):

Kalle Lindström, Sweden
Code Tested with 1.9.2


Scharlau, Dr Bruce A. September 28, 2010 at 6:57 am

Short suitable class with methods and tests.


Chris Jones September 8, 2010 at 3:55 am

Solution (#13):

Chris Jones, USA
Only tested with 1.8, did not use tests in comment section.


Scharlau, Dr Bruce A. September 28, 2010 at 6:58 am

Nice class with methods, no tests.


Tamas Szabo September 8, 2010 at 2:10 pm

Solution (#14):

Tamas Szabo, Australia
Code works ONLY with Ruby 1.9


Scharlau, Dr Bruce A. September 28, 2010 at 6:58 am

Classes and tests and display methods.


adrian.castillo September 16, 2010 at 2:34 am

Solution (#15):

Adrian Castillo, Mexico
Code works with 1.8 (not tested in 1.9 yet)
I created a class to do the stuff on the initialize and i did it all on the same iteration to don’t duplicate code lines.


Scharlau, Dr Bruce A. September 28, 2010 at 6:58 am

Nice simple solution with methods and tests.


Himansu Desai September 18, 2010 at 4:58 am

Solution (#17):

Himansu Desai, USA
Works with: ruby 1.8.7 and 1.9.2


Scharlau, Dr Bruce A. September 28, 2010 at 3:32 pm

Overly complex solution with no tests.


Samnang Chhun September 18, 2010 at 10:07 am

Solution (#18):

Samnang Chhun, Cambodia
Code works with Ruby: 1.8, 1.9


Scharlau, Dr Bruce A. September 28, 2010 at 3:33 pm

Classes and direct methods and rspec tests.


Predrag Bradaric September 19, 2010 at 10:20 pm

Solution (#19):

Predrag Bradaric, Serbia
Code works with Ruby 1.8 and 1.9.


Scharlau, Dr Bruce A. September 28, 2010 at 3:37 pm

Simple classes with tests nicely done.


Norman Eugene White September 20, 2010 at 6:45 pm

Solution (#16):

Norman Eugene White, USA
I only tried my code with Ruby 1.9


Scharlau, Dr Bruce A. September 28, 2010 at 7:00 am

Used regex to parse, no tests.


netmask September 23, 2010 at 6:00 am

Solution (#20):

Jonathan, Mexico


Scharlau, Dr Bruce A. September 28, 2010 at 3:33 pm

Uses hashes and arrays with no tests.


Christopher Fortenberry September 25, 2010 at 3:19 am

Solution (#21):

Christopher Fortenberry, USA
Code tested with Ruby 1.9.2


Scharlau, Dr Bruce A. September 28, 2010 at 3:34 pm

Using hashes and arrays with no tests.


Brad O'Connor September 26, 2010 at 7:18 pm

Solution (#22):

Brad O’Connor, Australia
Code works with Ruby 1.9 (probably 1.8 as well)
I wasn’t sure how to go about doing this in a way that could be tested (writing tests is not my strong point – good thing I don’t do this for a living). I used nokogiri as my XML library as I have used it before.
The file cia-1996.xml must be placed in the same directory as my file for the program to work.


Scharlau, Dr Bruce A. September 28, 2010 at 7:00 am

Script with loops and puts, no tests.


Benoit Daloze September 29, 2010 at 12:55 am

This is a (very late) participation, but I’d like to participate ‘Just for fun’.
Benoit Daloze, Belgium
Ruby 1.9
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))

Sorry for the delay.


Oto Brglez December 23, 2010 at 4:59 pm

Just for the fun of it… This is my “for fun” submission. Oto Brglez, Slovenia.
I was using Nokogiri and Ruby 1.9.2-head.



Leave a Comment

{ 22 trackbacks }

Previous post:

Next post: