RPCFN: XML Transformer - 8
7/Apr 2010
Ruby Programming Challenge For Newbies
RPCFN: XML Transformer (#8)
– By Jamie van Dyke
About Jamie van Dyke
Jamie van Dyke has been using Ruby and Rails since the beginning of 2005, has contributed significantly to the Rails documentation and code base, as well as running his own Rails business and being responsible for building Engine Yard’s European support team. Jamie is now the CTO over at Boxedup.
Jamie has this to say about the challenge:
This challenge is ideal for both beginner and advanced users. You can solve it in multiple ways and with differing levels of complexity, each level giving more flexibility. I hope you enjoy trying to solve the challenge and I look forward to seeing the results.
Our Awesome Sponsors
This monthly programming challenge is co-sponsored by Eden Development and Backup My App.
Eden Development is a leading software development firm based in Winchester, UK, specialising in Ruby applications. They craft dependable, flexible and beautifully made software which meets real business needs. They stand for quality, integrity, real craftsmanship and peace of mind for their customers. They also teach basic Ruby to Agile/XP courses: they love learning themselves and are delighted to support the RubyLearning initiative.
Backup My App is an automatic backup service for Ruby on Rails applications. You simply install the plugin to your Rails application and they handle the rest of the process. They store backup history for several weeks and you can restore any of them automatically. Try out their 1 GB plan for free. Backup My App has co-sponsored this challenge and is proud to make this contribution to the Ruby community.
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 and a free 10 GB account for a year from Backup My App.
- 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.
- All the participants in this challenge (except the participant with the best Ruby solution) will get a free 5 GB account for 6 months from Backup My App.
The four persons who win, can’t win again in the next immediate challenge but can still participate.
The Ruby Challenge
The Challenge
Introduction
I love a good challenge and find it helps you discover different aspects of a language, and also different methods to achieve the same goal. Seeing how others completed a quiz also helps you to expand your knowledge and experience which will give you more insight in the future.
The best tests are always ones based on real world examples. I’ve tried to simplify this one so it’s accessible to all that want to take a stab, while at the same time allowing those that one to be more advanced the opportunity to show off a little. You could complete this with a simple solution, or spend more time and give an elegant, more ruby-ish answer. You choose.
A library I recently wrote had to import data on a regular basis. I needed to normalise the data before I could import it because the data was from different XML feeds, but in unknown formats. So, your challenge is to build an XML transformer that can take any (within reason) XML file and change it into an expected XML format.
Specifics
You can download 3 source XML files as examples. You need to work out how to convert each of these files in to the expected output, bearing in mind that these are merely examples and therefore you should make your transformer as flexible as possible to handle other source inputs.
You will need to employ the use of an XML library, personally I use the nokogiri rubygem, but feel free to choose your own. Once you’ve decided on XML parser it’s up to you how you go about solving this quiz. I’ve designed this quiz to give you the freedom to solve this however you see fit, the only stipulation is that you stick to Ruby!
Additional Information
Qs. Do we have to use an XML parser?
Ans. Well, no, but it will probably be easier if you do.
Qs. Is it okay to assume that the source file has only first names and last names? It has no other data, e.g. ages, sexes, … ?
Ans. In this example there are only 2 fields that you need to worry about. Bonus points go to the results that can handle multiple formats with multiple fields. Of course, any file that has 3 fields in the source, should be outputting 3 fields in the result.
Qs. Is it necessary to indent output data (result xml)?
Ans. The results may ignore whitespace outside of elements but not inside.
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):
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 26th Apr. 2010 (Indian Standard Time). No new solutions will be accepted from 27th Apr. onwards.
- On 27th Apr. 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 Apr.
- 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?
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.
Acknowledgements
Special thanks to:
- Jamie van Dyke.
- Sponsors Eden Development and Backup My App.
- GitHub, for giving us access to a private repository on GitHub to store all the submitted solutions.
- The RubyLearning team, namely Satoshi Asakawa (Japan) and Victor Goff III (USA).
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
- Richard Colley, Australia
- Paul Barry, USA – declared winner (best solution)
- John Prince, USA – declared winner (randomly selected)
- Tanzeeb Khalili, Canada – declared winner (randomly selected)
- Rémy Coutable, France
- Adam Lum, USA – declared winner (randomly selected)
- Vijay Thiruvallur, India
- Benoit Daloze, Belgium
Just for Fun
The Winners
Congratulations to the winners of this Ruby Challenge. They are:
- Paul Barry from USA (his Ruby Challenge solution) – the person with the best and most creative Ruby solution. He wins any one of PeepCode’s Ruby on Rails screencasts and a free 10 GB account for a year from Backup My App.
- Adam Lum from USA, John Prince from USA and Tanzeeb Khalili from Canada win any one of Pragmatic’s The Ruby Object Model and Metaprogramming screencasts.
- All the participants in this challenge (except Paul Barry who gets a free 10 GB account for a year) will get a free 5 GB account for 6 months from Backup My App.
Previous Challenge
RPCFN: Broadsides (#7) by James Edward Gray II.
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 (#9) challenge by Avdi Grimm, USA is scheduled for 1st May 2010.