Round-robin tournament schedule: Difference between revisions

Implemented 2 Python solutions to Round-Robin Tournament Schedule task
imported>Thebeez
(Implemented 2 Python solutions to Round-Robin Tournament Schedule task)
Line 1,122:
 
This seems to be related to the OEIS sequence [https://oeis.org/A036981 "A036981: (2n+1) X (2n+1) symmetric matrices each of whose rows is a permutation of 1..(2n+1)"]. The next term (for N=10) would be 444733651353600 which takes too long to check.
=={{header|Python}}==
 
==Original method by R. Schurig==
<syntaxhighlight lang="python">
# round_robin.py by Xing216
from copy import deepcopy
def shift_up(myList):
myLen = len(myList)
return [myList[i % myLen] for i in range(1, 1 + myLen)]
def scheduler(competitors):
"""Uses the original method by Richard Schurig"""
if competitors % 2 == 1:
n = competitors + 1
horizontal_rows = n - 1
else:
n = competitors
horizontal_rows = n
vertical_rows = n // 2
table = [[[] for _ in range(vertical_rows)] for _ in range(horizontal_rows)]
competitor = 1
for i, row in enumerate(table):
for col in table[i]:
if competitor == competitors:
col.append(competitor)
competitor = 1
else:
col.append(competitor)
competitor += 1
table2 = deepcopy(table)
table2 = shift_up(table2)
for row in table2: row.reverse()
for i, row in enumerate(table):
for j, col in enumerate(table[i]):
col.append(table2[i][j][0])
return table
def print_table(table):
for i, round in enumerate(table):
print(f"Round {(i + 1):2}", end=": ")
for match in round:
print(f"{match[0]:2}-{match[1]:<2}", end=" ")
print()
print_table(scheduler(12))
</syntaxhighlight>
{{out}}
<pre style="height:10em">
Round 1: 1-12 2-11 3-10 4-9 5-8 6-7
Round 2: 7-6 8-5 9-4 10-3 11-2 12-1
Round 3: 1-12 2-11 3-10 4-9 5-8 6-7
Round 4: 7-6 8-5 9-4 10-3 11-2 12-1
Round 5: 1-12 2-11 3-10 4-9 5-8 6-7
Round 6: 7-6 8-5 9-4 10-3 11-2 12-1
Round 7: 1-12 2-11 3-10 4-9 5-8 6-7
Round 8: 7-6 8-5 9-4 10-3 11-2 12-1
Round 9: 1-12 2-11 3-10 4-9 5-8 6-7
Round 10: 7-6 8-5 9-4 10-3 11-2 12-1
Round 11: 1-12 2-11 3-10 4-9 5-8 6-7
Round 12: 7-6 8-5 9-4 10-3 11-2 12-1
</pre>
==Berger Tables==
<syntaxhighlight lang="python">
# berger_table.py by Xing216
def scheduler(competitors):
if competitors & 1:
competitors += 1
last = competitors
half = competitors // 2
rounds = last - 1
tables = [list() for i in range(rounds)]
for i in range(1, last):
row = i - 1
tables[row] = [list() * half]
tables[row][0] = [0, 0]
if i & 1:
tables[row][0][1] = last
opponent = (i + 1) // 2
tables[row][0][0] = opponent
else:
tables[row][0][0] = last
opponent = half + i // 2
tables[row][0][1] = opponent
for _ in range(1, half):
next_opponent = opponent + 1 if opponent < last - 1 else 1
tables[row].append([next_opponent, 0])
opponent = next_opponent
last_guest = 1
for i in reversed(range(1, last)):
row = i - 1
for j in reversed(range(0, half)):
opponent = last_guest
if j > 0:
tables[row][j][1] = opponent
last_guest = opponent + 1 if opponent < last - 1 else 1
return tables
def print_table(table):
for i, round in enumerate(table):
print(f"Round {(i + 1):2}", end=": ")
for match in round:
print(f"{match[0]:2}-{match[1]:<2}", end=" ")
print()
print_table(scheduler(12))
</syntaxhighlight>
{{out}}
<pre style="height:10em">
Round 1: 1-12 2-11 3-10 4-9 5-8 6-7
Round 2: 12-7 8-6 9-5 10-4 11-3 1-2
Round 3: 2-12 3-1 4-11 5-10 6-9 7-8
Round 4: 12-8 9-7 10-6 11-5 1-4 2-3
Round 5: 3-12 4-2 5-1 6-11 7-10 8-9
Round 6: 12-9 10-8 11-7 1-6 2-5 3-4
Round 7: 4-12 5-3 6-2 7-1 8-11 9-10
Round 8: 12-10 11-9 1-8 2-7 3-6 4-5
Round 9: 5-12 6-4 7-3 8-2 9-1 10-11
Round 10: 12-11 1-10 2-9 3-8 4-7 5-6
Round 11: 6-12 7-5 8-4 9-3 10-2 11-1
</pre>
=={{header|Quackery}}==
 
34

edits