A programming puzzle

One of the things I do outside of work is participate in a baseball simulation league. Part of the pre-season activities for the league is conducting a draft of all of the available players to fill out the rosters for the teams in the league. I’m building a web tool (using PHP and MySQL) to help me keep track of the drafted players (it’s something that helps me keep up with my programming skills and stay up with a few technologies that we offer to folks in the College of Engineering).

Our draft works like most baseball drafts, by reversing the order in each successive rounds. e.g. if our league had three teams:

RoundTeamPosition in RoundOverall Draft Position



1Red Sox33

2Red Sox14





3Red Sox39

(we don’t allow for trading draft picks)

One of the programming tasks I had to figure out tonight was:

given an overall draft position, figure out the:

While our league has a fixed number of teams (20) — I wanted a generic function that returned this information for me, whether the league had 20 teams or only 3 teams.

So I offer that challenge to you — write a function or set of functions that given a draft position, a number of teams in the league, and the first-round draft order for those teams, returns the round of the given draft position, and the team that should be drafting at that time. The function(s) should allow for any arbitrary number of teams in the league.

Here’s an example PHP array for a 20 team league, with team names and their draft position (intentionally alphabetized by team name, feel free to manipulate the array however you want to make it easier to work with — the built-in PHP array functions make this easy ):

$teamArray = array(‘Atlanta Braves’ => 18, ‘Baltimore Orioles’ => 6, ‘Boston Red Sox’ => 16, ‘Chicago Cubs’ => 1, ‘Chicago White Sox’ => 9, ‘Cincinatti Reds’ => 20, ‘Cleveland Indians’ => 10, ‘Detroit Tigers’ => 12, ‘Houston Astros’ => 14, ‘Kansas City Royals’ => 2, ‘Los Angeles Dodgers’ => 13, ‘New York Mets’ => 15, ‘New York Yankees’ => 19, ‘Oakland Athletics’ => 5, ‘Philadelphia Phillies’ => 3, ‘Pittsburgh Pirates’ => 11, ‘Saint Louis Cardinals’ => 8, ‘San Francisco Giants’ => 4, ‘Seattle Mariners’ => 17, ‘Texas Rangers’ => 7 );

(this challenge is initially meant for our Engineering PHP Developer’s group and my reference implementation is done in PHP, but feel free to use any language you like, and convert the array into the language you are using)

For those non-programmers that might be interested in the math of this ( and as a hint to the programmers) — — what algebraic expression(s) could you write to figure this out?

I’ll post what I came up with in the next few days.

([update]: I will temporarily screen comments that have solutions in them — just to give folks taking a look at the page some opportunity to work through the problem themselves without an answer right in front of them 😉 )