RPCFN: Ruby**Fun (#4)

by Satish Talim on November 26, 2009

Ruby Programming Challenge For Newbies

RPCFN: Ruby**Fun (#4)

By Michael Kohl

About Michael Kohl

Michael KohlMichael Kohl (Twitter / blog) in his day job, works as an IT systems engineer in Vienna, Austria. He fell in love with Ruby in 2003 or so, maintained various Ruby-related packages for Gentoo Linux from 2004-2006 and started being an assistant teacher for RubyLearning.org in early 2009. Besides all things Ruby his interests include mathematics, literature, travelling, foreign languages, (functional) programming languages (e.g. Clojure, Haskell), chess and so much more that he really wishes he wouldn’t need to sleep.

Michael has this to say about the challenge:

The best way to learn programming is to write code! Ruby is fun because it’s easy to achieve results and I really believe that the RPCFN shows new Rubyists how much they can accomplish with relatively little Ruby. Thinking about a problem and then being able to compare your own solution to dozens of others is a lot of fun and a great opportunity for learning, so make sure to take part in these challenges! The reason that I picked this particular challenge is that it’s easy to solve, but requires a bit of thinking to get a somewhat attractive solution.



This monthly programming challenge is sponsored by Chargify and O’Reilly Media.

Chargify simplifies recurring billing for Web 2.0 and SaaS companies. Build innovative web applications without worrying about how to bill your customers. Whether you’re a start up or an established business billing thousands of customers a month, Chargify works for you.

Access customer insight, revenue, signups, and cancellation trends right from your real-time dashboard, helping you focus on what’s important – your company’s growth. Get started for FREE to Chargify your business today.

O'Reilly Media

O’Reilly Media spreads the knowledge of innovators through its books, online services, magazine, and conferences. Since 1978, O’Reilly has been a chronicler and catalyst of leading-edge development, homing in on the technology trends that really matter and spurring their adoption by amplifying “faint signals” from the alpha geeks who are creating the future. An active participant in the technology community, the company has a long history of advocacy, meme-making, and evangelism.


  • The person 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 O’Reilly Media’s Ebook bundle.
  • The person with the second 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.
  • The other two prizes, selected randomly amongst the remaining working Ruby solutions, would be any one of:

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

The Ruby Challenge


You just started working for CoolNewCompany which is developing mathematics related software. Since you are new to the team, your boss gives you an easy task to test your abilities. Write a class that pretty-prints polynomials, following some simple rules:

  • if a coefficient is 1, it doesn’t get printed
  • if a coefficient is negative, you have to display something like “- 2x^3″, not “+ -2x^3″
  • if a coefficient is 0, nothing gets added to the output
  • for x^1 the ^1 part gets omitted
  • x^0 == 1, so we don’t need to display it

Here’s a couple of usage examples:

puts Polynomial.new([-3,-4,1,0,6]) # => -3x^4-4x^3+x^2+6
puts Polynomial.new([1,0,2]) # => x^2+2

Don’t concern yourself too much with error handling, but if somebody tries to create a polynomial with less than 2 elements, your program has to raise an ArgumentError with the message “Need at least 2 coefficients.”

Please check the provided unit tests for more examples and make sure to use them for verifying your solution!

Requirements: This has to be a pure Ruby script, using only the Ruby Standard Libraries (meaning, no external Gems). You do not need to build a gem for this. Pure Ruby code is all that is needed.

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 20th Dec. 2009 (Indian Standard Time). No new solutions will be accepted from 21st Dec. onwards.
  • On 21st Dec. 2009 all the solutions will be thrown open for everyone to see and comment upon.
  • The winning entries will be announced on this blog before end of Dec. 2009. 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:


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. Gimi Liang, China – declared winner (randomly selected)
  2. William Yanez, Venezuela
  3. Christiaan Van den Poel, Belgium
  4. Tom Stuart, U.K.
  5. José Sazo, Chile
  6. James Daniels, USA
  7. Pedro Diogo, Portugal
  8. Felipe Elias Philipp, Brazil
  9. Fabio Kreusch, Brazil
  10. Milan Dobrota, Serbia
  11. Jefferson Mariano de Souza, Brazil
  12. Aldric Giacomoni, USA
  13. Michael Lang, USA
  14. Rohit Arondekar, India
  15. Bill Sullivan, USA
  16. Jorge Dias, Spain
  17. Alexander Klink, Germany
  18. Chris Jones, USA
  19. Aurélien Bottazzini, France
  20. Ali Al-Sahaf, Saudi Arabia – declared winner (randomly selected)
  21. John McDonald, USA
  22. Aleksey Gureiev, Ukraine – declared winner (best solution)
  23. Fred Fordham, Australia
  24. Tony Chen, USA
  25. Rohit Sasikumar, India
  26. Paul Harrington, USA
  27. Aashish Kiran Chittimilla, India
  28. Benoit Daloze, Belgium
  29. Steve Wilhelm, USA
  30. Marc Minneman, USA
  31. Othmane Benkirane, Morocco
  32. Oleksandr Manzyuk, Ukraine
  33. Pankaj Sisodiya, India
  34. Oliver, UK
  35. Sérgio Silva, Portugal
  36. Isley Aardvark, USA
  37. Rémy Coutable, France
  38. Brad O’Connor, Australia
  39. Suraj Dhakankar, India
  40. Sunny Dackie, India
  41. Philippe Antras, France
  42. Amr Tamimi, Palestine
  43. Sriram Varahan, India – declared winner (second best solution)

Just for Fun

  1. James Daniels, USA
  2. Phil, Germany

The Winners


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

Previous Challenge

RPCFN: Short Circuit (#3) by Gautam Rege.


  • This challenge is now closed. Michael Kohl has a working solution to this problem. This is not a “perfect” or the sole “correct” solution, but just one way of doing it.
  • The (#5) challenge by Peter Cooper, UK is scheduled for 1st Jan. 2010.
  • The (#6) challenge by John Trupiano, USA is scheduled for 1st Feb. 2010.

Technorati Tags: , , , , ,

Posted by Satish Talim

{ 155 comments… read them below or add one }

Amr December 19, 2009 at 7:39 am

Is it possible to print the initialize method??

puts Polynomial.new([-3,-4,1,0,6]) # => prints #


Amr December 19, 2009 at 7:55 am

a method is missed!
def to_s


Michael Kohl December 19, 2009 at 3:27 pm

Sorry, I don’t understand what you are asking here, can you maybe rephrase your question?


Amr December 19, 2009 at 8:02 am

Solution (#44):

Amr Tamimi, Palestine
Original: https://gist.github.com/f81ab319b15e5355b712
Forked: https://gist.github.com/fa90d1cee66e05d6d261
Works with both 1.8/1.9


Jeff Savin December 22, 2009 at 6:50 am

Nice clean code from Amr. Very readable (although some comments are always nice). Solution passed all but:

Polynomial.new([0, 0, 0, 0, 100]) => +100 (instead of 100)

Overall, I liked.


Sriram Varahan December 19, 2009 at 5:26 pm

Solution (#45):

Sriram Varahan, India
Code tested with: Ruby 1.8
Original: https://gist.github.com/24e6da4f93c6a7cd5ebc
Forked: https://gist.github.com/d5f3615f95aae4e2845b


Jeff Savin December 22, 2009 at 6:51 am

Sriram, I loved your introduction explaining your thought process as well as the nice comments in your code. Your solution passed each of my tests without a hitch, and as one of the more concise code footprints, was easy to follow. Another one of my favorites, good job.


Sunny Dackie December 21, 2009 at 8:24 pm

This entry is not for the competition. I am just confused with the actual motive behind this competition.

Sunny Dackie, India.

I am not sure what this competition is looking for. Is it the prettiness of the program or the compactness of the program.
Take for example the solution which I had provided earlier Solution#42
If it is compared it with the following http://gist.github.com/260985
Then you would notice that the algorithm behind both the files are same however the level of readabilty is compromised with the degree of compactness.
In such scenario which one is the ideal solution to the problem according to you ?

Code works with Ruby 1.8 / 1.9 / Both:


Jeff Savin December 22, 2009 at 7:53 am

Hi Sunny. You ask a good question. I can’t speak for any of the other judges, only what I am looking for. In my mind, neither prettiness nor compactness supersedes working flawlessly and readability. I completely agree with you that your first entry is much more readable than the second which you just posted, although a bit longer.

So, if the code works perfectly, I then look for readability. In many cases, a smaller program does make for ease of understanding, but as you point out, this isn’t always the case. I would say your first entry was just about right in the length of code and readability. I liked it. Unfortunately, it failed the utmost priority by not passing all my test cases.

Thanks for your submission and hope to see you in future competitions.


Michael Kohl December 22, 2009 at 4:13 am

Thanks everyone, RPCFN#4 has come to an end and I have now picked a winner, who Satish will announce in due time.

Here are a couple of closing thoughts about this challenge:

The good
I was really happy to see so many submissions, I hope this means I managed to strike the difficult balance between being interesting yet not overwhelming for newbies and not too boring for somewhat more advanced Rubyists.

The bad
Quite a lot of otherwise really nice solutions messed up small details and therefore didn’t have a chance of winning. How easy that was to do I just realized today, because after adding some more unit tests my own example solution failed one of them (I updated the Gist).

The ugly
I’m sorry for the initial confusion about the unit tests and also for not having as much time for this challenge as I hoped I would.

Anyway, I hope you all enjoyed this challenge, I’m definitely looking forward to the next one! :-)


Leave a Comment

{ 24 trackbacks }

Previous post:

Next post: