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.

Prizes

  • 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

RPCFN

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):

Note:

  • 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:

Donations

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 !

Acknowledgements

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.

Questions?

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

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.

Update

  • 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 :)

Reply

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.

Reply

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?

Reply

Dmytrii Nagirniak August 31, 2010 at 7:42 pm

Solution (#1):

Dmytrii Nagirniak, Australia
https://gist.github.com/a8c86e12d7bf7afff3a7
Code works with 1.9.2-p0:

Reply

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

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

Reply

Kirill Shchepelin September 1, 2010 at 1:33 am

Solution (#2):

Kirill Shchepelin, Russia
https://gist.github.com/467038c4cef2aed2af2f

Reply

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

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

Reply

Lukasz Hanuszczak September 1, 2010 at 1:53 am

Solution (#3):

Lukasz Hanuszczak, Poland
https://gist.github.com/c657c53bf9d018e4ae42
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.

Reply

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”.

Reply

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

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

Reply

Rick DeNatale September 1, 2010 at 2:16 am

Solution (#4):

Rick DeNatale
https://gist.github.com/8d98b92dd97975110ffa
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
rick.denatale@gmail.com

Reply

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

Short code using blocks with no tests.

Reply

EzGraphs September 1, 2010 at 4:58 am

Solution (#5):

EzGraphs
I am a full time programmer – including ruby.
https://gist.github.com/de48dca1ae4162165047
Participating for fun (helpful for learning the best way to do things) – please award to another.

Reply

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

Most concise code using arrays, no tests.

Reply

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.

Reply

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.

Reply

Paul McKibbin September 1, 2010 at 7:17 am

Solution (#6):

Paul McKibbin, U.K.
Just for fun
https://gist.github.com/7a7a651fb565632d6639
Only tested with 1.8.7. Should also work under 1.9.

Reply

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

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

Reply

Bruce September 1, 2010 at 12:46 pm

Folks,
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)
end

def test_parsing
rc = RC.new
rc.parse_file

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)
end
end

Reply

David Lake September 1, 2010 at 3:26 pm

Solution (#7):

David Lake, England
git@gist.github.com:3e91ebee7f87e16f6649.git
Code works with Ruby 1.9.2, tested on Windows

Reply

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

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

Reply

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

Solution (#8):

Julio C. Villasante, Cuba
http://gist.github.com/58145a95a016bf9d089b
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.

Reply

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

Prints formated report to screen with answer and rspec.

Reply

zenchild September 1, 2010 at 11:37 pm

Solution (#9):

Dan Wanek, USA
git://gist.github.com/561079.git
Works with Both 1.8 and 1.9

Reply

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

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

Reply

Matthew Dahl September 2, 2010 at 6:05 am

Solution (#10):

Matthew Dahl, USA
https://gist.github.com/580f37612e43abf2e9a5

Reply

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

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

Reply

CapnCurry September 7, 2010 at 3:46 am

Solution (#11):

Mark Tabler, USA
https://gist.github.com/fbba1fb62751f3c6dec0
Works with 1.9, should work also with 1.8

Reply

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

Several classes and method, no tests.

Reply

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

Solution (#12):

Kalle Lindström, Sweden
Code Tested with 1.9.2
https://gist.github.com/3828453eec3c56d8fb82

Reply

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

Short suitable class with methods and tests.

Reply

Chris Jones September 8, 2010 at 3:55 am

Solution (#13):

Chris Jones, USA
git://gist.github.com/569252.git
Only tested with 1.8, did not use tests in comment section.

Reply

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

Nice class with methods, no tests.

Reply

Tamas Szabo September 8, 2010 at 2:10 pm

Solution (#14):

Tamas Szabo, Australia
https://gist.github.com/4fdac5b4219864e4c401
Code works ONLY with Ruby 1.9

Reply

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

Classes and tests and display methods.

Reply

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

Solution (#15):

Adrian Castillo, Mexico
https://gist.github.com/31f78b38c9f2659ba2f9
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.

Reply

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

Nice simple solution with methods and tests.

Reply

Himansu Desai September 18, 2010 at 4:58 am

Solution (#17):

Himansu Desai, USA
git@gist.github.com:39b3c9b272f2061e79e2.git
Works with: ruby 1.8.7 and 1.9.2

Reply

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

Overly complex solution with no tests.

Reply

Samnang Chhun September 18, 2010 at 10:07 am

Solution (#18):

Samnang Chhun, Cambodia
git@gist.github.com:dbe5caadffb1342f8752.git
Code works with Ruby: 1.8, 1.9
samnang.chhun@gmail.com

Reply

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

Classes and direct methods and rspec tests.

Reply

Predrag Bradaric September 19, 2010 at 10:20 pm

Solution (#19):

Predrag Bradaric, Serbia
https://gist.github.com/e43b1a085c304f385719
Code works with Ruby 1.8 and 1.9.

Reply

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

Simple classes with tests nicely done.

Reply

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

Solution (#16):

Norman Eugene White, USA
git@gist.github.com:95d14570f369a9a27122.git
I only tried my code with Ruby 1.9

Reply

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

Used regex to parse, no tests.

Reply

netmask September 23, 2010 at 6:00 am

Solution (#20):

Jonathan, Mexico
https://gist.github.com/77a97e466129bf49aac7

Reply

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

Uses hashes and arrays with no tests.

Reply

Christopher Fortenberry September 25, 2010 at 3:19 am

Solution (#21):

Christopher Fortenberry, USA
https://gist.github.com/ff93fd9654720b484973
Code tested with Ruby 1.9.2

Reply

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

Using hashes and arrays with no tests.

Reply

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

Solution (#22):

Brad O’Connor, Australia
https://gist.github.com/2ec9890bb6710c0fe610
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.
Thankyou.

Reply

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

Script with loops and puts, no tests.

Reply

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
eregontp@gmail.com
http://gist.github.com/601607
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.

Reply

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.

https://gist.github.com/752847

Reply

Leave a Comment

{ 22 trackbacks }

Previous post:

Next post: