Adding Up Hours or The Road Less Traveled

Two roads diverged in a wood, and I—
I took the one less traveled by,
And that has made all the difference.

Robert Frost

I usually work around 40 hours a week, receiving my paychecks bi-monthly. Every two weeks I have to add up my hours and invoice them. I’d like to do this task as quickly as I can with the least amount of errors possible.

There are a couple of ways that I could go about solving this problem.
The first road that one may take might be to use the trusted calculator.
Another path one may choose would be to use google.com or Mac OS X spotlight.
Even more, one may choose to write a small script to add the hours.
Which way yields the fastest result? Which way yields the least amount of errors?

Let’s Have Some Fun!
Here are some made up hours:

8.13, 7.85, 8.00, 7.90, 8.30, 8.0, 7.5, 8.4, 8.0, 7.65

If I were the man who took the old fashion calculator route, it would take me about 35 seconds to add up these numbers. Usually if I use a calculator I like to check my answer once to make sure that it yields the same result twice. So, we could say that it would take a good 60 seconds.

So, 60 seconds isn’t bad. Hell, that’s only 1 minute of the day gone… not too much to sweat.

Let’s see how long it takes for us to type our numbers in the Google search.
I pasted my list of numbers into Google and inserted the plus symbol in between each number.
EX.)

8.13 + 7.85 + 8.00 + 7.90 + 8.30 + 8.0 + 7.50 + 8.4 + 8.0 + 7.65

That only took 23 seconds! That’s a nice increase from 60. You see, I trust that Google won’t make errors and I know that the risk of manually entered errors is much less than a manual calculator (especially since I copy and paste the numbers.)

For good measure let’s see how long Mac OS X spotlight will take. My guess is that it will be around 20-25 seconds. On second thought, let’s not use spotlight. It doesn’t seem that spotlight enjoys our method of copy and paste and insert plus symbols, so we’ll wave it goodbye!

Now, let’s try to write a few lines of ruby code to calculate our hours.

# We need to define an array of hours
hours = %w(8.13 7.85 8.00 7.90 8.30 8.0 7.50 8.4 8.0 7.65)

# Whoops! The elements in the hours array are saved as strings!
# They really should be floats... Let's fix that!
hours.map!{ |hour| hour.to_f }

# Now, we need to add all the hours together using the inject method
total_hours = hours.inject() {|result, element| result + element}

I timed this path at a blazing 5 SECONDS!
Sure, it took me a little longer to write a script, but I have it forever now.

I might not have proven much in this post, but it is important to try new things.
So, either dust off your old calculator, head on over to Google, or type up a little Ruby script. There’s lots of ways to solve the problem, but which way solves the problem the fastest with the least amount of errors? Discuss it in the comments.

Post a Comment

*Required
*Required (Never published)