Ruby Programming Challenge For Newbies
RPCFN: Ruby**Fun (#4)
By Michael Kohl
About Michael Kohl
Michael 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 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:
- Your name:
- Country of Residence:
- GIST URL of your Solution (i.e. Ruby code) with explanation and / or test cases:
- Code works with Ruby 1.8 / 1.9 / Both:
- Email address (will not be published):
- 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:
- What Is The Ruby Programming Challenge For Newbies (RPCFN)?
- How does RPCFN benefit you?
- Challenge Rules
- Best Solution
- Can I Submit A Ruby Programming Challenge Topic?
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.
Special thanks to:
- Michael Kohl.
- Sponsor Chargify.
- Book Sponsor O’Reilly Media.
- The RubyLearning team, namely Jeff Savin (Canada), Michael Kohl (Austria), Peter Crawford (Italy) and Satoshi Asakawa (Japan).
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.
There are two categories of participants. Some are vying for the prizes and some are participating for the fun of it.
In the competition
- Gimi Liang, China – declared winner (randomly selected)
- William Yanez, Venezuela
- Christiaan Van den Poel, Belgium
- Tom Stuart, U.K.
- José Sazo, Chile
- James Daniels, USA
- Pedro Diogo, Portugal
- Felipe Elias Philipp, Brazil
- Fabio Kreusch, Brazil
- Milan Dobrota, Serbia
- Jefferson Mariano de Souza, Brazil
- Aldric Giacomoni, USA
- Michael Lang, USA
- Rohit Arondekar, India
- Bill Sullivan, USA
- Jorge Dias, Spain
- Alexander Klink, Germany
- Chris Jones, USA
- Aurélien Bottazzini, France
- Ali Al-Sahaf, Saudi Arabia – declared winner (randomly selected)
- John McDonald, USA
- Aleksey Gureiev, Ukraine – declared winner (best solution)
- Fred Fordham, Australia
- Tony Chen, USA
- Rohit Sasikumar, India
- Paul Harrington, USA
- Aashish Kiran Chittimilla, India
- Benoit Daloze, Belgium
- Steve Wilhelm, USA
- Marc Minneman, USA
- Othmane Benkirane, Morocco
- Oleksandr Manzyuk, Ukraine
- Pankaj Sisodiya, India
- Oliver, UK
- Sérgio Silva, Portugal
- Isley Aardvark, USA
- Rémy Coutable, France
- Brad O’Connor, Australia
- Suraj Dhakankar, India
- Sunny Dackie, India
- Philippe Antras, France
- Amr Tamimi, Palestine
- Sriram Varahan, India – declared winner (second best solution)
Just for Fun
- James Daniels, USA
- Phil, Germany
Congratulations to the winners of this Ruby Challenge. They are:
- Aleksey Gureiev from Ukraine (his Ruby Challenge solution) – the person with the best Ruby solution. He wins any one of O’Reilly Media’s Ebook bundle.
- Sriram Varahan from India (his Ruby Challenge solution) – the person with the second best Ruby solution. He wins any one of PeepCode’s Ruby on Rails screencasts.
- Gimi Liang from China (his Ruby Challenge solution) – selected randomly amongst the remaining working Ruby solutions. He wins any one of BDDCasts’ screencasts.
- Ali Al-Sahaf from Saudi Arabia (his Ruby Challenge solution) – selected randomly amongst the remaining working Ruby solutions. He wins any one of Pragmatic’s The Ruby Object Model and Metaprogramming screencasts.
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.