RPCFN: Average Arrival Time For A Flight (#2)

by Satish Talim on October 8, 2009

Ruby Programming Challenge For Newbies

RPCFN: Average Arrival Time For A Flight (#2)

By Chris Strom

Thank you for the very encouraging response to the first-everRuby Programming Challenge For Newbies (RPCFN)“. The second Ruby challenge is from Chris Strom.

About Chris Strom

Chris StromChris Strom (twitter / blog) in his day job, is the Director of Software Engineering for mdlogix, a small company in Baltimore, Maryland. They develop software that manages clinical research trials and associated data. They primarily code with Ruby on Rails. His background is in web development, mostly in Perl until ~2005 when he made the switch to Ruby.

Chris has this to say about the challenge:

RPCFN is a good idea as reading books and documentation can only take you so far when learning a new language. To really learn, you need to use the language. RPCFN provides a fabulous forum for using Ruby in the form of regular, engaging (but not arcanely difficult) challenges. Better yet, it provides feedback on how to use Ruby well, as each fortnight the best solution to a challenge is chosen. RPCFN is a wonderful introduction to the Ruby language and to the Ruby community. Welcome newbies!


Railsware for premium-quality web applications

This fortnights programming challenge is sponsored by Railsware. Railsware is glad to support the Ruby Programming Challenge and help the Ruby community grow and get stronger.

Railsware is a product development company specializing in Ruby on Rails and UI design creating premium-quality web applications. The company works with startups and established businesses looking to build ecommerce, social networking, specialized business applications and many other products.


  • 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 PeepCode’s Ruby on Rails screencasts.
  • The other prize, selected randomly amongst the remaining working Ruby solutions, will be awarded any one of BDDCasts’ screencasts.

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

The Ruby Challenge


You owe a big favor and have agreed to pick up a friend at the airport every Friday night. The airline on which your friend flies is cheap, but terrible with reporting delays and departure/arrival times. You soon realize that the 10pm flight is never on time and is usually late by more than an hour. If the plane has arrived at 11:15pm, 12:03am, 11:30pm, 11:23pm and 11:48pm, what is the average arrival time?

Does the solution still work if your friend changes to a flight arriving 6 hours later? What about 12 hours later?

Program Output

The output should look something like this when run from the console:

>> average_time_of_day(["11:51pm", "11:56pm", "12:01am", "12:06am", "12:11am"])
=> "12:01am"

>> average_time_of_day(["6:41am", "6:51am", "7:01am"])
=> "6:51am"


  • Your digital ways will not help you, time of day is cyclical.
  • You may need to use the Math and Time classes.

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. Email address (will not be published):
  3. Brief description of what you do (will not be published):
  4. Country of Residence:
  5. GIST URL of your Solution (i.e. Ruby code) with explanation and / or test cases:
  6. Code works with Ruby 1.8 / 1.9 / Both:


  • 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 18th Oct. 2009 (Indian Standard Time). No new solutions will be accepted from 19th to 22nd Oct. 2009.
  • On Monday, 19th Oct. 2009 all the solutions will be thrown open for everyone to see and comment upon.
  • The winning entries will be announced on this blog on 22nd Oct. 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.talim@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. Othmane Benkirane, Morocco – declared winner
  2. Tisho Georgiev, Bulgaria
  3. Pete Campbell, USA
  4. Jonathan Julian, USA
  5. Antonio, Canada
  6. Robison WR Santos, Brazil
  7. Ricardo Duarte, Brazil
  8. Paul Barry, USA
  9. Haris Amin, USA
  10. Charles Feduke, USA – declared winner
  11. Oliver, UK
  12. Bryan Liles, USA
  13. Gunther Diemant, Germany
  14. Valério Farias, Brazil
  15. Vikas Maskeri, India
  16. Jiren Patel, India
  17. Stefan, Germany
  18. Ahmed Al Hafoudh, Slovakia
  19. Tom Voltz, USA
  20. David Jenkins, USA
  21. Michael Lang, USA
  22. Thiago Fernandes Massa, Brazil
  23. Tim Rand, USA
  24. Milan Dobrota, Serbia
  25. Mike Hodgson, Canada
  26. Brad O’Connor, Australia
  27. Giordano Scalzo, Italy
  28. Rainer Thiel, New Zealand
  29. Todd Huss, USA
  30. Pankaj Sisodiya, India
  31. Loïc Paillotin, USA
  32. Chuck Ha, USA
  33. Josh Baxley, USA
  34. Javier Blanco Gutiérrez, Spain
  35. Sogo Ohta, Japan
  36. Daniel Wanek, USA
  37. Himansu Desai, USA
  38. John McDonald, USA
  39. Ben Miller, UK
  40. Sriram Varahan, India
  41. Conner Peirce, USA
  42. Ben Marini, USA

Just for Fun

  1. Michael Kohl, Austria
  2. Peter Cooper, UK

The Winners


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

Previous Challenge

RPCFN: Shift Subtitle (#1) by Fabio Akita.

Next Challenge

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


  • This Challenge is now closed. Chris Strom has a working solution to this problem. This is not a “perfect” or the sole “correct” solution, but just one way of doing it.
  • Chris Strom has written a blog post that talks about the most common “issues” faced by Ruby beginners.
  • The (#3) challenge by Gautam Rege, India is scheduled for 1st Nov. 2009.
  • The (#4) challenge by Michael Kohl, Austria is scheduled for 1st Dec. 2009.
  • The (#5) challenge by Peter Cooper, UK is scheduled for 1st Jan. 2010.

Technorati Tags: , , , , ,

Posted by Satish Talim

{ 171 comments… read them below or add one }

conner October 19, 2009 at 12:10 am

Solution (#43):

Conner Peirce


Jeff Savin October 20, 2009 at 10:18 am

Nice job which passed all my tests with flying colors. Compact code, easy to read, one of my favorites.


Chris Strom October 23, 2009 at 5:57 am

Ooh, so close…

average_time_of_day(["11:51pm", "11:56pm", "12:01am", "12:06am", "12:11am"])
=> “2:25pm”

This is because the parsed date is only being compared to the previous date for shifting. In this case “12:01am” gets shifted because it is so far away from “11:56pm”, but “12:06am” does not get shifted because it is right next to “12:01am”.

Tests in a test harness can really help in cases like this. Make a tweak to work in a new scenario and you can get immediate feedback on whether or not a previously passing test case broke.

The only ruby code suggestion I would make is that accumulators such as sum are more properly done in an inject (or reduce) than they are in a for block.

Good overall approach and well commented.


Ben Marini October 19, 2009 at 6:08 am

Solution (#44):

Code works with Ruby 1.8


Jeff Savin October 20, 2009 at 10:18 am

Small and compact code which was well documented. Passed all tests except:

a = ["11:58pm", "12:00pm"] #=> “5:59am” where the expected output was “5:59pm”


Chris Strom October 23, 2009 at 5:57 am

Given the constraint of ordered times, it works well. Would have been even better if it didn’t force the caller to determine how to order the times :)

Well commented, compact code with test cases even!

The only ruby quibble that I would have is using the same name (times) for the accumulator inside the inject as was assigned to the result of the inject block. They should be different to avoid confusion.


Oliver October 19, 2009 at 1:58 pm

I think you should perhaps go through the comments and remove the email addresses that you said would not be published?


Satish Talim October 19, 2009 at 2:27 pm

Thanks for pointing that out. The comments have now been edited.


Tom October 21, 2009 at 8:51 pm

I already know that I am late for submitting my solution for the challenge but here is my code anyway : https://gist.github.com/cf6b3e6330e6da8c102f

The code is very simple. If the time is “am”, just create a time for tomorrow (now+1), all “pm” dates are converted as date=today. All tests are working right. I don’t know if another timezone will work.

Btw, I am Tom, from France. I am learning Ruby (just read the first 300 pages of the pickaxe 3rd edition).



Praful Kelkar October 24, 2009 at 9:28 am

The amount of time spent by Chris Strom, evaluating every submitted solution is amazing. His comments provide helpful hints for Ruby beginners like me. Thanks Chris.


Leave a Comment

{ 23 trackbacks }

Previous post:

Next post: