# Re: Tournament Matchups (#105)

**URL:** <https://www.ruby-forum.com/t/re-tournament-matchups-105/78943>\
**Category:** Ruby\
**Created:** [December 10, 2006, 10:10pm UTC](https://www.ruby-forum.com/t/re-tournament-matchups-105/78943 "2006-12-10T22:10:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [December 10, 2006, 10:10pm UTC](https://www.ruby-forum.com/t/re-tournament-matchups-105/78943/1 "2006-12-10T22:10:02Z")

</div>

Following (and attached) is the copy of my code. The main logic of it  
is very simple:

1. Create “bye” teams until there is a power of two
2. From an array sorted by “goodness” of a team, make matches by taking  
a team from the two ends.

A nice and short version which does the above is below (and attached as  
tournielean.rb). I actually wrote this version after writing the other  
version because most of the entries to Ruby Q. seem to be short.  
./tournielean n where n is the number of teams to run it.  
#! /usr/bin/ruby

class Numeric  
# Is the given number a power of self?  
# 16.isPowerOf(2) == true  
# 100.isPowerOf(2) == false  
def isPowerOf(other)  
i = 0  
while (other \*\* i \<= self)  
return true if other \*\* i == self  
i += 1  
end  
false  
end  
end

class Tournament  
# Recieves a list of team names in order of ranking with the best  
first.  
def initialize(teams)  
@eligible = teams  
@round = 0  
end

```
def createNextRound
	out = "Round #{@round += 1}: "
	inCurrentRound = []

	until (@eligible.length.isPowerOf(2))
		out += "#{@eligible.first} vs. bye, "
		inCurrentRound.push @eligible.shift
	end

	until (@eligible.empty?)
		winner = @eligible.shift
		loser = @eligible.pop

		out += "#{winner} vs. #{loser}, "

		inCurrentRound.push(winner)
	end

	@eligible = inCurrentRound
	out[0, out.length - 2] + "."
end

def createAllRounds
	out = ""
	until (@eligible.length == 1)
		out << createNextRound << "\n"
	end
	out# + "Winner: #{@eligible[0]}"
end

```

end

t = Tournament.new((1…(ARGV[0].to\_i)).to\_a)  
puts t.createAllRounds
