Round-robin tournament schedule: Difference between revisions

imported>Thebeez
 
(8 intermediate revisions by 5 users not shown)
Line 443:
Next
 
For c@ = 1 To a@-1 ' print the pairings according to the scheme
For c@ = 1 To a@-1 ' print the pairings according to the scheme
Print Using "Round __: ";c@;
Line 586 ⟶ 585:
Round 4: ( 1 vs 3 ) ( 5 vs 2 ) ( 6 vs 0 )
Round 5: ( 1 vs 2 ) ( 4 vs 0 ) ( 5 vs 6 )
</pre>
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Collections.Generic;
using System.Linq;
 
public class RoundRobinTournamentSchedule
{
public static void Main(string[] args)
{
Console.WriteLine("Round robin for 12 players:");
RoundRobin(12);
Console.WriteLine("\n");
Console.WriteLine("Round robin for 5 players, 0 denotes a bye:");
RoundRobin(5);
}
 
private static void RoundRobin(int teamCount)
{
if (teamCount < 2)
{
throw new ArgumentException($"Number of teams must be greater than 2: {teamCount}");
}
 
List<int> rotatingList = Enumerable.Range(2, teamCount - 1).ToList();
if (teamCount % 2 == 1)
{
rotatingList.Add(0);
teamCount += 1;
}
 
for (int round = 1; round < teamCount; round++)
{
Console.Write($"Round {round,2}:");
List<int> fixedList = new List<int> { 1 };
fixedList.AddRange(rotatingList);
for (int i = 0; i < teamCount / 2; i++)
{
Console.Write($" ( {fixedList[i],2} vs {fixedList[teamCount - 1 - i],2} )");
}
Console.WriteLine();
Rotate(rotatingList, 1);
}
}
 
private static void Rotate(List<int> list, int rotationCount)
{
int count = list.Count;
if (count == 0) return;
 
rotationCount %= count;
if (rotationCount < 0)
{
rotationCount += count;
}
 
list.Reverse();
list.Reverse(0, rotationCount);
list.Reverse(rotationCount, count - rotationCount);
}
}
</syntaxhighlight>
{{out}}
<pre>
Round robin for 12 players:
Round 1: ( 1 vs 12 ) ( 2 vs 11 ) ( 3 vs 10 ) ( 4 vs 9 ) ( 5 vs 8 ) ( 6 vs 7 )
Round 2: ( 1 vs 11 ) ( 12 vs 10 ) ( 2 vs 9 ) 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re>
 
=={{header|jq}}==
{{Works with|jq}}
 
'''Works with gojq, the Go implementation of jq'''
 
'''Adapted from [[#Wren|Wren]]'''
<syntaxhighlight lang="jq">
def lpad($len): tostring | ($len - length) as $l | (" " * $l) + .;
 
def rotate: .[-1:] + .[:-1];
 
def roundRobin($n):
{$n, lst: [range(2; $n+1)]}
| if $n % 2 == 1
then .lst += [0] # 0 denotes a bye
| .n += 1
end
| foreach range(1; .n) as $r (.;
.emit = "Round \($r|lpad(3)): "
| ([1] + .lst) as $lst2
| reduce range(0; .n/2) as $i (.;
.emit += " (\($lst2[$i]|lpad(2)) vs \($lst2[.n - 1 - $i]|lpad(2)))")
| .lst |= rotate )
| .emit ;
 
"Round robin for 12 players:",
roundRobin(12),
"\n\nRound robin for 5 players (0 denotes a bye):\n",
roundRobin(5)
</syntaxhighlight>
{{output}}
Essentially the same as for [[#Wren|Wren]].
 
=={{header|Julia}}==
Line 961 ⟶ 1,081:
Round 4: ( 1 vs 3 ) ( 4 vs 2 ) ( 5 vs 0 )
Round 5: ( 1 vs 2 ) ( 3 vs 0 ) ( 4 vs 5 )
</pre>
 
=={{header|Pascal}}==
A console program in Free Pascal. Uses the circle method, but not exactly as in Wikipedia: the fixed player is the dummy, if present, otherwise the last; and the rotation is anticlockwise.
<syntaxhighlight lang="pascal">
program RoundRobin;
(*
Rosetta Code: write list of matches in a round robin tournament.
Command line:
RoundRobin number_of_players
*)
{$mode objfpc}{$H+}
 
uses SysUtils;
 
var
nrPlayers, round : integer;
n, m, c, j, k : integer;
a : array of integer;
 
// Write the matches in a round, formatting nicely
procedure WriteRound();
var
t, u : integer;
begin
Write( 'Round', round:3, ': ');
u := 0;
for t := 0 to m - 2 do begin
Write( '(', a[u]:2); inc(u);
Write( ' v', a[u]:3, ') '); inc(u);
end;
Write( '(', a[u]:2); // u = n - 2
if c > 0 then
WriteLn( ' v', c:3, ')')
else
WriteLn( ' bye)');
end;
 
begin
if ParamCount < 1 then begin
WriteLn( 'Number of players is required');
exit;
end;
nrPlayers := SysUtils.StrToIntDef( ParamStr(1), -1);
// if string can't be converted, nrPlayers := -1
if (nrPlayers < 2) then begin
WriteLn( 'Invalid number of players');
exit;
end;
WriteLn( 'Round robin with ', nrPlayers, ' players');
m := (nrPlayers + 1) div 2;
n := 2*m;
if Odd( nrPlayers) then c := 0 // dummy player, opponent gets a bye
else c := n; // genuine player
SetLength( a, n);
k := 0;
for j := 0 to m - 2 do begin
a[k] := m - j; inc(k);
a[k] := m + 1 + j; inc(k);
end;
a[k] := 1;
a[n - 1] := c; // a[n - 1] stays = c throughout
round := 1;
WriteRound();
for round := 2 to n - 1 do begin
for j := 0 to n - 2 do begin // increment all entries except a[n - 1]
inc(a[j]);
if a[j] = n then a[j] := 1; // wrap round if necessary
end;
WriteRound();
end;
end.
</syntaxhighlight>
{{out}}
<pre>
Round robin with 12 players
Round 1: ( 6 v 7) ( 5 v 8) ( 4 v 9) ( 3 v 10) ( 2 v 11) ( 1 v 12)
Round 2: ( 7 v 8) ( 6 v 9) ( 5 v 10) ( 4 v 11) ( 3 v 1) ( 2 v 12)
Round 3: ( 8 v 9) ( 7 v 10) ( 6 v 11) ( 5 v 1) ( 4 v 2) ( 3 v 12)
Round 4: ( 9 v 10) ( 8 v 11) ( 7 v 1) ( 6 v 2) ( 5 v 3) ( 4 v 12)
Round 5: (10 v 11) ( 9 v 1) ( 8 v 2) ( 7 v 3) ( 6 v 4) ( 5 v 12)
Round 6: (11 v 1) (10 v 2) ( 9 v 3) ( 8 v 4) ( 7 v 5) ( 6 v 12)
Round 7: ( 1 v 2) (11 v 3) (10 v 4) ( 9 v 5) ( 8 v 6) ( 7 v 12)
Round 8: ( 2 v 3) ( 1 v 4) (11 v 5) (10 v 6) ( 9 v 7) ( 8 v 12)
Round 9: ( 3 v 4) ( 2 v 5) ( 1 v 6) (11 v 7) (10 v 8) ( 9 v 12)
Round 10: ( 4 v 5) ( 3 v 6) ( 2 v 7) ( 1 v 8) (11 v 9) (10 v 12)
Round 11: ( 5 v 6) ( 4 v 7) ( 3 v 8) ( 2 v 9) ( 1 v 10) (11 v 12)
 
Round robin with 9 players
Round 1: ( 5 v 6) ( 4 v 7) ( 3 v 8) ( 2 v 9) ( 1 bye)
Round 2: ( 6 v 7) ( 5 v 8) ( 4 v 9) ( 3 v 1) ( 2 bye)
Round 3: ( 7 v 8) ( 6 v 9) ( 5 v 1) ( 4 v 2) ( 3 bye)
Round 4: ( 8 v 9) ( 7 v 1) ( 6 v 2) ( 5 v 3) ( 4 bye)
Round 5: ( 9 v 1) ( 8 v 2) ( 7 v 3) ( 6 v 4) ( 5 bye)
Round 6: ( 1 v 2) ( 9 v 3) ( 8 v 4) ( 7 v 5) ( 6 bye)
Round 7: ( 2 v 3) ( 1 v 4) ( 9 v 5) ( 8 v 6) ( 7 bye)
Round 8: ( 3 v 4) ( 2 v 5) ( 1 v 6) ( 9 v 7) ( 8 bye)
Round 9: ( 4 v 5) ( 3 v 6) ( 2 v 7) ( 1 v 8) ( 9 bye)
</pre>
 
Line 1,548 ⟶ 1,766:
1 3 4 5 6 7
2 12 11 10 9 8
</pre>
 
=={{header|Rust}}==
 
{{Trans|Nim}}
 
<syntaxhighlight lang="rust">
fn round_robin(n: usize) {
assert!(n >= 2);
let mut n = n;
let mut list1: Vec<usize> = (2..=n).collect();
if n % 2 == 1 {
list1.push(0); // 0 denotes a "bye".
n += 1;
}
for r in 1..n {
print!("Round {:2}:", r);
let list2 = vec![1].into_iter().chain(list1.iter().cloned()).collect::<Vec<_>>();
for i in 0..(n / 2) {
print!(" ({:>2} vs {:<2})", list2[i], list2[n - i - 1]);
}
println!();
list1.rotate_right(1);
}
}
 
fn main() {
println!("Round robin for 12 players:\n");
round_robin(12);
 
println!("\n\nRound robin for 5 players (0 denotes a bye):\n");
round_robin(5);
}
</syntaxhighlight>
 
{{out}}
<pre>
Round robin for 12 players:
 
Round 1: ( 1 vs 12) ( 2 vs 11) ( 3 vs 10) ( 4 vs 9 ) ( 5 vs 8 ) ( 6 vs 7 )
Round 2: ( 1 vs 11) (12 vs 10) ( 2 vs 9 ) ( 3 vs 8 ) ( 4 vs 7 ) ( 5 vs 6 )
Round 3: ( 1 vs 10) (11 vs 9 ) (12 vs 8 ) ( 2 vs 7 ) ( 3 vs 6 ) ( 4 vs 5 )
Round 4: ( 1 vs 9 ) (10 vs 8 ) (11 vs 7 ) (12 vs 6 ) ( 2 vs 5 ) ( 3 vs 4 )
Round 5: ( 1 vs 8 ) ( 9 vs 7 ) (10 vs 6 ) (11 vs 5 ) (12 vs 4 ) ( 2 vs 3 )
Round 6: ( 1 vs 7 ) ( 8 vs 6 ) ( 9 vs 5 ) (10 vs 4 ) (11 vs 3 ) (12 vs 2 )
Round 7: ( 1 vs 6 ) ( 7 vs 5 ) ( 8 vs 4 ) ( 9 vs 3 ) (10 vs 2 ) (11 vs 12)
Round 8: ( 1 vs 5 ) ( 6 vs 4 ) ( 7 vs 3 ) ( 8 vs 2 ) ( 9 vs 12) (10 vs 11)
Round 9: ( 1 vs 4 ) ( 5 vs 3 ) ( 6 vs 2 ) ( 7 vs 12) ( 8 vs 11) ( 9 vs 10)
Round 10: ( 1 vs 3 ) ( 4 vs 2 ) ( 5 vs 12) ( 6 vs 11) ( 7 vs 10) ( 8 vs 9 )
Round 11: ( 1 vs 2 ) ( 3 vs 12) ( 4 vs 11) ( 5 vs 10) ( 6 vs 9 ) ( 7 vs 8 )
 
 
Round robin for 5 players (0 denotes a bye):
 
Round 1: ( 1 vs 0 ) ( 2 vs 5 ) ( 3 vs 4 )
Round 2: ( 1 vs 5 ) ( 0 vs 4 ) ( 2 vs 3 )
Round 3: ( 1 vs 4 ) ( 5 vs 3 ) ( 0 vs 2 )
Round 4: ( 1 vs 3 ) ( 4 vs 2 ) ( 5 vs 0 )
Round 5: ( 1 vs 2 ) ( 3 vs 0 ) ( 4 vs 5 )
</pre>
 
 
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
object RoundRobinTournamentSchedule extends App {
def roundRobin(teamCount: Int): Unit = {
require(teamCount >= 2, s"Number of teams must be greater than 2: $teamCount")
 
var rotatingList = (2 to teamCount).toList
var adjustedTeamCount = teamCount
 
if (teamCount % 2 == 1) {
rotatingList :+= 0
adjustedTeamCount += 1
}
 
for (round <- 1 until adjustedTeamCount) {
print(f"Round $round%2d:")
val fixedList = 1 :: rotatingList
for (i <- 0 until adjustedTeamCount / 2) {
print(f" ( ${fixedList(i)}%2d vs ${fixedList(adjustedTeamCount - 1 - i)}%2d )")
}
println()
rotatingList = rotatingList.last :: rotatingList.init
}
}
 
println("Round robin for 12 players:")
roundRobin(12)
println()
println("Round robin for 5 players, 0 denotes a bye:")
roundRobin(5)
}
</syntaxhighlight>
{{out}}
<pre>
Round robin for 12 players:
Round 1: ( 1 vs 12 ) ( 2 vs 11 ) ( 3 vs 10 ) ( 4 vs 9 ) ( 5 vs 8 ) ( 6 vs 7 )
Round 2: ( 1 vs 11 ) ( 12 vs 10 ) ( 2 vs 9 ) ( 3 vs 8 ) ( 4 vs 7 ) ( 5 vs 6 )
Round 3: ( 1 vs 10 ) ( 11 vs 9 ) ( 12 vs 8 ) ( 2 vs 7 ) ( 3 vs 6 ) ( 4 vs 5 )
Round 4: ( 1 vs 9 ) ( 10 vs 8 ) ( 11 vs 7 ) ( 12 vs 6 ) ( 2 vs 5 ) ( 3 vs 4 )
Round 5: ( 1 vs 8 ) ( 9 vs 7 ) ( 10 vs 6 ) ( 11 vs 5 ) ( 12 vs 4 ) ( 2 vs 3 )
Round 6: ( 1 vs 7 ) ( 8 vs 6 ) ( 9 vs 5 ) ( 10 vs 4 ) ( 11 vs 3 ) ( 12 vs 2 )
Round 7: ( 1 vs 6 ) ( 7 vs 5 ) ( 8 vs 4 ) ( 9 vs 3 ) ( 10 vs 2 ) ( 11 vs 12 )
Round 8: ( 1 vs 5 ) ( 6 vs 4 ) ( 7 vs 3 ) ( 8 vs 2 ) ( 9 vs 12 ) ( 10 vs 11 )
Round 9: ( 1 vs 4 ) ( 5 vs 3 ) ( 6 vs 2 ) ( 7 vs 12 ) ( 8 vs 11 ) ( 9 vs 10 )
Round 10: ( 1 vs 3 ) ( 4 vs 2 ) ( 5 vs 12 ) ( 6 vs 11 ) ( 7 vs 10 ) ( 8 vs 9 )
Round 11: ( 1 vs 2 ) ( 3 vs 12 ) ( 4 vs 11 ) ( 5 vs 10 ) ( 6 vs 9 ) ( 7 vs 8 )
 
Round robin for 5 players, 0 denotes a bye:
Round 1: ( 1 vs 0 ) ( 2 vs 5 ) ( 3 vs 4 )
Round 2: ( 1 vs 5 ) ( 0 vs 4 ) ( 2 vs 3 )
Round 3: ( 1 vs 4 ) ( 5 vs 3 ) ( 0 vs 2 )
Round 4: ( 1 vs 3 ) ( 4 vs 2 ) ( 5 vs 0 )
Round 5: ( 1 vs 2 ) ( 3 vs 0 ) ( 4 vs 5 )
 
</pre>
 
 
=={{header|Swift}}==
{{trans|Java}}
<syntaxhighlight lang="Swift">
import Foundation
 
func roundRobin(teamCount: Int) {
if teamCount < 2 {
fatalError("Number of teams must be greater than 2: \(teamCount)")
}
 
var rotatingList = Array(2...teamCount)
var effectiveTeamCount = teamCount
 
if teamCount % 2 == 1 {
rotatingList.append(0) // Adding a 'bye' in case of odd number of teams
effectiveTeamCount += 1
}
 
for round in 1..<effectiveTeamCount {
print("Round \(round):", terminator: "")
let fixedList = [1] + rotatingList
for i in 0..<(effectiveTeamCount / 2) {
print(" (\(fixedList[i]) vs \(fixedList[effectiveTeamCount - 1 - i]))", terminator: "")
}
print()
rotatingList.rotate(shift: 1)
}
}
 
extension Array {
mutating func rotate(shift: Int) {
let index = shift >= 0 ?
self.index(self.startIndex, offsetBy: self.count - shift, limitedBy: self.endIndex) :
self.index(self.startIndex, offsetBy: -shift, limitedBy: self.endIndex)
 
guard let validIndex = index else { return }
self = Array(self[validIndex..<self.endIndex] + self[self.startIndex..<validIndex])
}
}
 
// Example usage
print("Round robin for 12 players:")
roundRobin(teamCount: 12)
print("\nRound robin for 5 players, 0 denotes a bye:")
roundRobin(teamCount: 5)
</syntaxhighlight>
{{out}}
<pre>
Round robin for 12 players:
Round 1: (1 vs 12) (2 vs 11) (3 vs 10) (4 vs 9) (5 vs 8) (6 vs 7)
Round 2: (1 vs 11) (12 vs 10) (2 vs 9) (3 vs 8) (4 vs 7) (5 vs 6)
Round 3: (1 vs 10) (11 vs 9) (12 vs 8) (2 vs 7) (3 vs 6) (4 vs 5)
Round 4: (1 vs 9) (10 vs 8) (11 vs 7) (12 vs 6) (2 vs 5) (3 vs 4)
Round 5: (1 vs 8) (9 vs 7) (10 vs 6) (11 vs 5) (12 vs 4) (2 vs 3)
Round 6: (1 vs 7) (8 vs 6) (9 vs 5) (10 vs 4) (11 vs 3) (12 vs 2)
Round 7: (1 vs 6) (7 vs 5) (8 vs 4) (9 vs 3) (10 vs 2) (11 vs 12)
Round 8: (1 vs 5) (6 vs 4) (7 vs 3) (8 vs 2) (9 vs 12) (10 vs 11)
Round 9: (1 vs 4) (5 vs 3) (6 vs 2) (7 vs 12) (8 vs 11) (9 vs 10)
Round 10: (1 vs 3) (4 vs 2) (5 vs 12) (6 vs 11) (7 vs 10) (8 vs 9)
Round 11: (1 vs 2) (3 vs 12) (4 vs 11) (5 vs 10) (6 vs 9) (7 vs 8)
 
Round robin for 5 players, 0 denotes a bye:
Round 1: (1 vs 0) (2 vs 5) (3 vs 4)
Round 2: (1 vs 5) (0 vs 4) (2 vs 3)
Round 3: (1 vs 4) (5 vs 3) (0 vs 2)
Round 4: (1 vs 3) (4 vs 2) (5 vs 0)
Round 5: (1 vs 2) (3 vs 0) (4 vs 5)
 
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var rotate = Fn.new { |lst|
Line 1,578 ⟶ 1,989:
roundRobin.call(12)
System.print("\n\nRound robin for 5 players (0 denotes a bye) :\n")
roundRobin.call(5)</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
2,442

edits