William James wrote: > 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 Simpler: num_teams = ARGV.shift.to_i n = 1 ; begin n *= 2 end until n >= num_teams teams = (1..num_teams).to_a + ["bye"] * (n - num_teams) while (n = teams.size) > 2 do teams = teams[0, n/2].zip( teams[n/2 .. -1].reverse ) end p teams.flatten.partition{n=!n}.reverse.transpose "The most valuable of all talents is that of never using two words when one will do. " -Thomas Jefferson "Programmers are always surrounded by complexity; we cannot avoid it.... If our basic tool, the language in which we design and code our programs, is also complicated, the language itself becomes part of the problem rather than part of its solution. " -C. A. R. Hoare (1980 Turing Award Lecture) "To attain knowledge, add things every day. To attain wisdom, remove things every day." -Lao-tse "Perfection is attained, not when there is nothing left to add, but when there is nothing left to take away. " -Antoine de Saint-Exupery