RPCFN: Ruby**Fun 4

Ruby Programming Challenge For Newbies

RPCFN: Ruby**Fun (#4)

By Michael Kohl

About Michael Kohl

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.

Sponsors

Chargify

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.

Prizes

  • 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

RPCFN

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

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

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:

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

Winners

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

Previous Challenge

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

Update

  • 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.
comments powered by Disqus