Round-robin tournament schedule: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 12: Line 12:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f ROUND-ROBIN_TOURNAMENT_SCHEDULE.AWK
# syntax: GAWK -f ROUND-ROBIN_TOURNAMENT_SCHEDULE.AWK
BEGIN {
BEGIN {
Line 51: Line 51:
}
}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 114: Line 114:
</pre>
</pre>
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>function nob( n as uinteger, i as uinteger, bye as boolean ) as string
<syntaxhighlight lang="freebasic">function nob( n as uinteger, i as uinteger, bye as boolean ) as string
'helper function to allow byes to be printed intelligently
'helper function to allow byes to be printed intelligently
dim as string pad
dim as string pad
Line 161: Line 161:
print "Nine teams with byes"
print "Nine teams with byes"
roundrob(9)
roundrob(9)
</syntaxhighlight>
</lang>
{{out}}<pre>
{{out}}<pre>
Twelve teams
Twelve teams
Line 189: Line 189:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Wren}}
{{trans|Wren}}
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 227: Line 227:
fmt.Println("\n\nRound robin for 5 players (0 denotes a bye) :\n")
fmt.Println("\n\nRound robin for 5 players (0 denotes a bye) :\n")
roundRobin(5)
roundRobin(5)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 235: Line 235:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>""" https://rosettacode.org/mw/index.php?title=Round-robin_tournament_schedule """
<syntaxhighlight lang="julia">""" https://rosettacode.org/mw/index.php?title=Round-robin_tournament_schedule """


function schurig(N, verbose = true)
function schurig(N, verbose = true)
Line 266: Line 266:
print("\n\nSchurig table for round robin with 7 players:")
print("\n\nSchurig table for round robin with 7 players:")
schurig(7)
schurig(7)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Schurig table for round robin with 12 players:
Schurig table for round robin with 12 players:
Line 293: Line 293:
=={{header|Perl}}==
=={{header|Perl}}==
===Even===
===Even===
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Round-robin_tournament_schedule
use strict; # https://rosettacode.org/wiki/Round-robin_tournament_schedule
Line 304: Line 304:
@teams[0,$n-1,1..$n-2] = @teams;
@teams[0,$n-1,1..$n-2] = @teams;
printf 'Round %2d:' . '%4d vs %2d'x($n/2) . "\n", $_, @teams[ map { $_, $n-1-$_} 0..($n/2)-1 ];
printf 'Round %2d:' . '%4d vs %2d'x($n/2) . "\n", $_, @teams[ map { $_, $n-1-$_} 0..($n/2)-1 ];
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 320: Line 320:
</pre>
</pre>
===Even and Odd===
===Even and Odd===
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 343: Line 343:
say join '', round_robin 12;
say join '', round_robin 12;
say '';
say '';
say join '', map { s/0 vs /Bye: /r } round_robin 7;</lang>
say join '', map { s/0 vs /Bye: /r } round_robin 7;</syntaxhighlight>
{{out}}
{{out}}
<pre>Round 1: 1 vs 12 2 vs 11 3 vs 10 4 vs 9 5 vs 8 6 vs 7
<pre>Round 1: 1 vs 12 2 vs 11 3 vs 10 4 vs 9 5 vs 8 6 vs 7
Line 367: Line 367:
=={{header|Phix}}==
=={{header|Phix}}==
Based on the circle with rotor diagrams on the wikipedia page, and implements home/away.
Based on the circle with rotor diagrams on the wikipedia page, and implements home/away.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 426: Line 426:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 449: Line 449:
While I "optimised away" the need for a physical rotate, obviously not because I was concerned with performance but more in the hope of creating shorter and more elegant code, in the end it made little difference.
While I "optimised away" the need for a physical rotate, obviously not because I was concerned with performance but more in the hope of creating shorter and more elegant code, in the end it made little difference.
Should it be more to your taste, you can remove "l" and replace the inner loop above with:
Should it be more to your taste, you can remove "l" and replace the inner loop above with:
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">circle</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">circle</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">rownd</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (since "round" is a bultin)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">rownd</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (since "round" is a bultin)</span>
Line 465: Line 465:
<span style="color: #000000;">circle</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">circle</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]&</span><span style="color: #000000;">circle</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #000080;font-style:italic;">-- (physically rotate it)</span>
<span style="color: #000000;">circle</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">circle</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]&</span><span style="color: #000000;">circle</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #000080;font-style:italic;">-- (physically rotate it)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|Picat}}==
=={{header|Picat}}==
===Constraint modelling===
===Constraint modelling===
<lang Picat>import sat.
<syntaxhighlight lang="picat">import sat.


main =>
main =>
Line 530: Line 530:
nl
nl
end,
end,
nl.</lang>
nl.</syntaxhighlight>


{{out}}
{{out}}
Line 555: Line 555:
* 7 vs 12 must be played in the last round
* 7 vs 12 must be played in the last round


<lang Picat>main =>
<syntaxhighlight lang="picat">main =>
nolog,
nolog,
N = 12,
N = 12,
Line 563: Line 563:
[7,12,N-1]],
[7,12,N-1]],
tournament_cp(N, NumRounds,NumPairs,Extras,X,Bye),
tournament_cp(N, NumRounds,NumPairs,Extras,X,Bye),
print_tournament(X,NumRounds,NumPairs,Bye).</lang>
print_tournament(X,NumRounds,NumPairs,Bye).</syntaxhighlight>


{{out}}
{{out}}
Line 585: Line 585:
Here the cp solver is used since it's faster than the sat solver for generating all solutions.
Here the cp solver is used since it's faster than the sat solver for generating all solutions.


<lang Picat>import cp.
<syntaxhighlight lang="picat">import cp.


main =>
main =>
Line 591: Line 591:
Count = count_all(tournament_cp(N, _NumRounds,_NumPairs,_Extras,_X,_Bye)),
Count = count_all(tournament_cp(N, _NumRounds,_NumPairs,_Extras,_X,_Bye)),
println(N=Count)
println(N=Count)
end.</lang>
end.</syntaxhighlight>


{{out}}
{{out}}
Line 603: Line 603:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>my @players = (1,0)[$_%2] .. $_ given 12;
<syntaxhighlight lang="raku" line>my @players = (1,0)[$_%2] .. $_ given 12;
my $half = +@players div 2;
my $half = +@players div 2;
my $round = 0;
my $round = 0;
Line 611: Line 611:
@players[1..*].=rotate(-1);
@players[1..*].=rotate(-1);
last if [<] @players;
last if [<] @players;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Round 1: ( 1 vs 12) ( 2 vs 11) ( 3 vs 10) ( 4 vs 9 ) ( 5 vs 8 ) ( 6 vs 7 )
<pre>Round 1: ( 1 vs 12) ( 2 vs 11) ( 3 vs 10) ( 4 vs 9 ) ( 5 vs 8 ) ( 6 vs 7 )
Line 626: Line 626:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>def round_robin( n )
<syntaxhighlight lang="ruby">def round_robin( n )
rotating_players = (2..n).map(&:to_s) #player 1 to be added later
rotating_players = (2..n).map(&:to_s) #player 1 to be added later
rotating_players << "bye" if n.odd?
rotating_players << "bye" if n.odd?
Line 642: Line 642:
puts
puts
end
end
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>Round 1
<pre>Round 1
Line 691: Line 691:
=={{header|Wren}}==
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "./fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "./fmt" for Fmt


var rotate = Fn.new { |lst|
var rotate = Fn.new { |lst|
Line 717: Line 717:
roundRobin.call(12)
roundRobin.call(12)
System.print("\n\nRound robin for 5 players (0 denotes a bye) :\n")
System.print("\n\nRound robin for 5 players (0 denotes a bye) :\n")
roundRobin.call(5)</lang>
roundRobin.call(5)</syntaxhighlight>


{{out}}
{{out}}
Line 746: Line 746:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>def N = 12; \number of players (must be even)
<syntaxhighlight lang="xpl0">def N = 12; \number of players (must be even)
int I, Player(N+1), Round, Temp;
int I, Player(N+1), Round, Temp;
[for I:= 1 to N do Player(I):= I;
[for I:= 1 to N do Player(I):= I;
Line 762: Line 762:
Player(2):= Temp;
Player(2):= Temp;
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}