Ruby Quiz wrote:

> by Demetrius Nunes
>
> In a single-elimination tournament, there is usually a previously established
> ranking for the participating players or teams, such as that the best players or
> teams are matched against the worst ones. This is done this way so there is a
> higher chance for the top players/teams to meet in the final.
>
> For example, in a small 8-player tournament, there would be 3 rounds. This first
> round would be setup like this:
>
> 	Round 1
> 	1 x 8
> 	2 x 7
> 	3 x 6
> 	4 x 5


Inf = 999

def find_best x
  Array( x ).flatten.min
end

class Array
  def partition_teams
    t = sort_by{|x| find_best( x ) }
    [ t[0,t.size/2], t[t.size/2..-1].reverse ]
  end
end

num_teams = ARGV.shift.to_i

n = 1
begin n *= 2 end until n >= num_teams
teams = (1..num_teams).to_a + [Inf] * (n - num_teams)

while teams.size > 2 do
  teams = teams.partition_teams.transpose
end
f = nil
p teams.flatten.partition{f=!f}.transpose