tinydancer9454
tinydancer9454

Reputation: 401

python matching up items in a list to create new lists

I'm trying to write a program that creates a round robin tournament schedule (every player plays every other player) for a certain number of players.
I was wondering if there was a way to do this using just lists. I'm trying not to use itertools.combinations()

Can anyone think of a way to do this if I have a program that allows you to input the number of players and generates a list of those players? Example: 4 players generates the list [1,2,3,4]

I need the program to print:

Round 1: 1 vs. 4 2 vs. 3

Round 2: 1 vs. 3 4 vs. 2

Round 3: 1 vs. 2 3 vs. 4

Upvotes: 1

Views: 98

Answers (1)

shx2
shx2

Reputation: 64318

Here's a simple implementation of the first algorithm described here.

N = 6
a = range(N)
for i in xrange(N-1):
    print zip(a[:N/2], reversed(a[N/2:]))
    a = a[:1] + a[-1:] + a[1:-1]

EDIT: actually, as pointed out, it even works for odd Ns.

(ORIG: This only works if N is even. The link explains how to extend it to support odd Ns.)

Upvotes: 2

Related Questions