Followup: A Programming Puzzle

I was actually pleasantly surprised by the followups that I received to my “Programming Puzzle” query the other day.

It’s a simple problem, but it does take a little bit of thinking.

My original answer that I did was:

Basically, reverse the index into one’s array by determining which round you were in, handling the edge cases of a “0” remainder from using the MODulus operator to be the last team or the first team. Like most of my emails, my answer is a little verbose I think.

Which is why I really liked Charles Brabec’s Perl-based answer. By kicking up the number of teams internal within the logic, you obviate having to test for the edge cases. Nice.

He also did a Postscript answer. I told him he was just showing off :-). But it’s really interesting, and interesting to remember that Postscript is actually a programming language.

Charles also provided a test case with his code, which is important to help verify the results. One of the folks that submitted a PHP example would have benefited by running through their function through a test suite. It looked good until you got around the picks that rolled back around.

Professor Jeff Joines did the following in Matlab:

Getting around the edge case check by not using a modulus to compute the relative position.

Doug Goodall did the following in Python

Doug also calculated the pick position like Jeff did.

One interesting difference between Python and PHP shows up in the round calculation, PHP returns a float value with division and Python does an integer division.

Billy Beaudoin mixed his languages and provided a PHP example that would work only in Perl. It was unique because it raised -1 to the power of the round within an array reference, basically:

The subscripts are off – but it takes advantage of a neat feature in Perl, negative array indexes index back from the end of the array.

Billy also found the gmp_div_qr() function – providing quotient and remainder in one fell swoop.

Troy Hurteau also did php:

one thing Troy did (not shown) was provide lots of error checking for the number of teams against the provided team array, and checking for silly values like 0 teams (which the rest of us really forgot to do).

While I would have loved more responses, having so many different code examples was great.