pastebin.ch - Python

Subject
Python
Author
Lolcontest
Description
Find the winner of a contest without contesting!
Posted on
Mon, 25 Jan 2010 23:05:29 +0100
Content
class Player(object):
    def __init__(self, name):
        self.name = name
 
    def __str__(self):
        return self.name
 
class Game(object):
    def __init__(self, a, b):
        self.a = a
        self.b = b
 
    def __str__(self):
        return "(winner of %s vs. %s)" % (self.a, self.b)
 
players = [Player(x) for x in 'abcdefghi']
this_round_players = players[:] # players to go forward into the next round
rounds = []
while len(this_round_players) > 1:
    games = [] # list of games in this round
    while len(this_round_players) > 1:
        a, b = this_round_players[0:2] # match the first two players
        del this_round_players[0:2] # remove them from the list
        games.append(Game(a, b)) # create new Game object, and add it to the games list
    rounds.append(games)
    if len(this_round_players) > 0:
        odd_one_out = this_round_players[0]
    else:
        odd_one_out = None
    # The winners of these games play in the next round
    this_round_players = games[:]
    if odd_one_out:
        this_round_players.append(odd_one_out)
# We have a winner!
print "And the winner is..."
print this_round_players[0]
 
Don't email to hereI'm now supporting the experiment spamschlucker.org too :)