World Cup group stage: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(8 intermediate revisions by 5 users not shown)
Line 1:
{{task}}
 
It's World Cup season (or at least it was when this page was created)!
It's World Cup season (or at least it was when this page was created)! The World Cup is an international football/soccer tournament that happens every 4 years. Countries put their international teams together in the years between tournaments and qualify for the tournament based on their performance in other international games. Once a team has qualified they are put into a group with 3 other teams. For the first part of the World Cup tournament the teams play in "group stage" games where each of the four teams in a group [[wp:Round-robin tournament|plays all three other teams once]]. The results of these games determine which teams will move on to the "knockout stage" which is a standard single-elimination tournament. The two teams from each group with the most standings points move on to the knockout stage. Each game can result in a win for one team and a loss for the other team or it can result in a draw/tie for each team. A win is worth three points in the standings. A draw/tie is worth one point. A loss is not worth any points.
 
The World Cup is an international football/soccer tournament that happens every 4 years.   Countries put their international teams together in the years between tournaments and qualify for the tournament based on their performance in other international games.   Once a team has qualified they are put into a group with 3 other teams.
Generate all possible outcome combinations for the six group stage games. With three possible outcomes for each game there should be 3<sup>6</sup> = 729 of them. Calculate the standings points for each team with each combination of outcomes. Show a histogram (graphical, ASCII art, or straight counts--whichever is easiest/most fun) of the standings points for all four teams over all possible outcomes.
 
For the first part of the World Cup tournament the teams play in "group stage" games where each of the four teams in a group [[wp:Round-robin tournament|plays all three other teams once]]. &nbsp; The results of these games determine which teams will move on to the "knockout stage" which is a standard single-elimination tournament. &nbsp; The two teams from each group with the most standings points move on to the knockout stage.
Don't worry about tiebreakers as they can get complicated. We are basically looking to answer the question "if a team gets x standings points, where can they expect to end up in the group standings?".
 
Each game can result in a win for one team and a loss for the other team or it can result in a draw/tie for each team.
<small>''Hint: there should be no possible way to end up in second place with less than two points as well as no way to end up in first with less than three. Oddly enough, there is no way to get 8 points at all.''</small>
:::* &nbsp; A win is worth three points.
:::* &nbsp; A draw/tie is worth one point.
:::* &nbsp; A loss is worth zero points.
 
 
;Task:
:* &nbsp; Generate all possible outcome combinations for the six group stage games. &nbsp; With three possible outcomes for each game there should be 3<sup>6</sup> = 729 of them.
:* &nbsp; Calculate the standings points for each team with each combination of outcomes.
:* &nbsp; Show a histogram (graphical, &nbsp; ASCII art, or straight counts--whichever is easiest/most fun) of the standings points for all four teams over all possible outcomes.
 
 
Don't worry about tiebreakers as they can get complicated. &nbsp; We are basically looking to answer the question "if a team gets x standings points, where can they expect to end up in the group standings?".
 
<small>''Hint: there should be no possible way to end up in second place with less than two points as well as no way to end up in first with less than three. &nbsp; Oddly enough, there is no way to get 8 points at all.''</small>
<br><br>
 
=={{header|11l}}==
{{trans|Go}}
 
<syntaxhighlight lang="11l">V games = [‘12’, ‘13’, ‘14’, ‘23’, ‘24’, ‘34’]
V results = ‘000000’
 
F numberToBase(=n, b)
I n == 0
R ‘0’
V digits = ‘’
L n != 0
digits ‘’= String(Int(n % b))
n I/= b
R reversed(digits)
 
F nextResult()
I :results == ‘222222’
R 0B
V res = Int(:results, radix' 3) + 1
:results = numberToBase(res, 3).zfill(6)
R 1B
 
V points = [[0] * 10] * 4
L
V records = [0] * 4
L(i) 0 .< games.len
S results[i]
‘2’
records[games[i][0].code - ‘1’.code] += 3
‘1’
records[games[i][0].code - ‘1’.code]++
records[games[i][1].code - ‘1’.code]++
‘0’
records[games[i][1].code - ‘1’.code] += 3
 
records.sort()
L(i) 4
points[i][records[i]]++
 
I !nextResult()
L.break
 
print(|‘POINTS 0 1 2 3 4 5 6 7 8 9
-------------------------------------------------------------’)
V places = [‘1st’, ‘2nd’, ‘3rd’, ‘4th’]
L(i) 4
print(places[i], end' ‘ place ’)
L(j) 10
print(‘#<5’.format(points[3 - i][j]), end' ‘’)
print()</syntaxhighlight>
 
{{out}}
<pre>
POINTS 0 1 2 3 4 5 6 7 8 9
-------------------------------------------------------------
1st place 0 0 0 1 14 148 152 306 0 108
2nd place 0 0 4 33 338 172 164 18 0 0
3rd place 0 18 136 273 290 4 8 0 0 0
4th place 108 306 184 125 6 0 0 0 0 0
</pre>
 
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Line 107 ⟶ 183:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>POINTS 0 1 2 3 4 5 6 7 8 9
Line 118 ⟶ 194:
=={{header|C++}}==
{{trans|Go}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <array>
#include <iomanip>
Line 207 ⟶ 283:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>POINTS 0 1 2 3 4 5 6 7 8 9
Line 221 ⟶ 297:
<!-- By Martin Freedman, 17/01/2018 -->
Unlike the Python solution, this does not use a library for combinations and cartesian products but provides 4 1-liner Linq methods.
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 302 ⟶ 378:
}
}
</syntaxhighlight>
</lang>
Produces:
<pre>
Line 315 ⟶ 391:
=={{header|Common Lisp}}==
{{works with|SBCL|1.4.9}}
<langsyntaxhighlight lang="lisp">(defun histo ()
(let ((scoring (vector 0 1 3))
(histo (list (vector 0 0 0 0 0 0 0 0 0 0) (vector 0 0 0 0 0 0 0 0 0 0)
Line 347 ⟶ 423:
(dotimes (i (length el))
(format t "~3D " (aref el i)))
(format t "~%"))</langsyntaxhighlight>
{{out}}
<pre>
Line 359 ⟶ 435:
This imports the module of the third D solution of the Combinations Task.
{{trans|Python}}
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.range, std.array, std.algorithm, combinations3;
 
Line 378 ⟶ 454:
}
writefln("%(%s\n%)", histo[].retro);
}</langsyntaxhighlight>
{{out}}
<pre>[0, 0, 0, 1, 14, 148, 152, 306, 0, 108]
Line 386 ⟶ 462:
 
This alternative version is not fully idiomatic D, it shows what to currently do to tag the main function of the precedent version as @nogc.
<langsyntaxhighlight lang="d">import core.stdc.stdio, std.range, std.array, std.algorithm, combinations3;
 
immutable uint[2][6] combs = 4u.iota.array.combinations(2).array;
Line 415 ⟶ 491:
printf("\n");
}
}</langsyntaxhighlight>
{{out}}
<pre>0 0 0 1 14 148 152 306 0 108
Line 424 ⟶ 500:
=={{header|Elena}}==
{{trans|Java}}
ELENA 56.0x :
<lang elenasyntaxhighlight>import system'routines;
import extensions;
Line 445 ⟶ 521:
}
closurefunction()
{
var points := IntMatrix.allocate(4, 10);
Line 454 ⟶ 530:
var records := new int[]{0,0,0,0};
for(int i := 0,; i < 6,; i += 1)
{
var r := results[i];
Line 468 ⟶ 544:
records := records.ascendant();
for(int i := 0,; i <= 3,; i += 1)
{
points[i][records[i]] := points[i][records[i]] + 1
Line 480 ⟶ 556:
});
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 491 ⟶ 567:
=={{header|Elixir}}==
{{trans|Erlang}}
<langsyntaxhighlight lang="elixir">defmodule World_Cup do
def group_stage do
results = [[3,0],[1,1],[0,3]]
Line 522 ⟶ 598:
:io.format(format, Enum.to_list(0..9))
IO.puts String.duplicate(" ---", 10)
Enum.each(World_Cup.group_stage, fn x -> :io.format(format, x) end)</langsyntaxhighlight>
 
{{out}}
Line 537 ⟶ 613:
This solution take advantage of the expressiveness power of the list comprehensions expressions. Function ''combos'' is copied from [http://panduwana.wordpress.com/2010/04/21/combination-in-erlang/ panduwana blog].
 
<langsyntaxhighlight lang="erlang">
-module(world_cup).
 
Line 571 ⟶ 647:
combinations([H|T],List2) ->
[ [{H,Item} | Comb] || Item <- List2, Comb <- combinations(T,List2)].
</syntaxhighlight>
</lang>
 
Output:
Line 583 ⟶ 659:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 636 ⟶ 712:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 652 ⟶ 728:
There might be a more elegant way of expressing this.
 
<langsyntaxhighlight Jlang="j">require'stats'
outcome=: 3 0,1 1,:0 3
pairs=: (i.4) e."1(2 comb 4)
standings=: +/@:>&>,{<"1<"1((i.4) e."1 pairs)#inv"1/ outcome</langsyntaxhighlight>
 
Here, standings represents all the possible outcomes:
<langsyntaxhighlight Jlang="j"> $standings
729 4</langsyntaxhighlight>
 
Of course, not all of them are distinct:
<langsyntaxhighlight Jlang="j"> $~.standings
556 4</langsyntaxhighlight>
 
With only 556 distinct outcomes, there must be some repeats. Looking at this more closely (gathering the outcomes in identical groups, counting how many members are in each group, and then considering the unique list of group sizes):
<langsyntaxhighlight Jlang="j"> ~.#/.~standings
1 2 3 4 6</langsyntaxhighlight>
 
Some standings can be attained two different ways, some three different ways, some four different ways, and some six different ways. Let's look at the one with six different possibilities:
 
<langsyntaxhighlight Jlang="j"> (I.6=#/.~standings){{./.~standings
4 4 4 4</langsyntaxhighlight>
 
That's where every team gets 4 standing.
Line 678 ⟶ 754:
How about outcomes which can be achieved four different ways?
 
<langsyntaxhighlight Jlang="j"> (I.4=#/.~standings){{./.~standings
6 6 3 3
6 3 3 6
Line 684 ⟶ 760:
3 6 6 3
3 6 3 6
3 3 6 6</langsyntaxhighlight>
 
Ok, that's simple. So how about a histogram. Actually, it's not clear what a histogram should represent. There are four different teams, and possible standings range from 0 through 9, so we could show the outcomes for each team:
 
<langsyntaxhighlight Jlang="j"> +/standings =/ i.10
27 81 81 108 162 81 81 81 0 27
27 81 81 108 162 81 81 81 0 27
27 81 81 108 162 81 81 81 0 27
27 81 81 108 162 81 81 81 0 27</langsyntaxhighlight>
 
Each team has the same possible outcomes. So instead, let's order the results so that instead of seeing each team as a distinct entity we are seeing the first place, second place, third place and fourth place team (and where there's a tie, such as 4 4 4 4, we just arbitrarily say that one of the tied teams gets in each of the tied places...):
 
<langsyntaxhighlight Jlang="j"> +/(\:"1~ standings)=/"1 i.10
0 0 0 1 14 148 152 306 0 108
0 0 4 33 338 172 164 18 0 0
0 18 136 273 290 4 8 0 0 0
108 306 184 125 6 0 0 0 0 0</langsyntaxhighlight>
 
Here, the first row represents whichever team came in first in each outcome, the second row represents the second place team and so on.
Line 709 ⟶ 785:
{{works with|Java|1.5+}}
This example codes results as a 6-digit number in base 3. Each digit is a game. A 2 is a win for the team on the left, a 1 is a draw, and a 0 is a loss for the team on the left.
<langsyntaxhighlight lang="java5">import java.util.Arrays;
public class GroupStage{
Line 749 ⟶ 825:
System.out.println("Fourth place: " + Arrays.toString(points[0]));
}
}</langsyntaxhighlight>
{{out}}
<pre>First place: [0, 0, 0, 1, 14, 148, 152, 306, 0, 108]
Line 758 ⟶ 834:
=={{header|Julia}}==
{{trans|Java}}
<langsyntaxhighlight lang="julia">function worldcupstages()
games = ["12", "13", "14", "23", "24", "34"]
results = "000000"
Line 798 ⟶ 874:
 
worldcupstages()
</langsyntaxhighlight>{{out}}
<pre>
First place: [0, 0, 0, 1, 14, 148, 152, 306, 0, 108]
Line 808 ⟶ 884:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
val games = arrayOf("12", "13", "14", "23", "24", "34")
Line 843 ⟶ 919:
println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 857 ⟶ 933:
=={{header|Lua}}==
{{trans|C++}}
<langsyntaxhighlight lang="lua">function array1D(a, d)
local m = {}
for i=1,a do
Line 943 ⟶ 1,019:
end
print()
end</langsyntaxhighlight>
{{out}}
<pre>POINTS 0 1 2 3 4 5 6 7 8 9
Line 955 ⟶ 1,031:
{{trans|Python}}
{{libheader|itertools}}
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, strutils
import itertools
 
Line 971 ⟶ 1,047:
 
for x in reversed(histo):
echo x.mapIt(($it).align(3)).join(" ")</langsyntaxhighlight>
 
{{out}}
Line 981 ⟶ 1,057:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use Math::Cartesian::Product;
 
@scoring = (0, 1, 3);
Line 1,001 ⟶ 1,077:
 
$fmt = ('%3d ') x 10 . "\n";
printf $fmt, @$_ for reverse @histo;</langsyntaxhighlight>
{{out}}
<pre> 0 0 0 1 14 148 152 306 0 108
Line 1,013 ⟶ 1,089:
In this case I used both, and modified both to fit their particular tasks (games and results).<br>
Some credit is due to the Kotlin entry for inspiring some of the innermost code.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function game_combinations(sequence res, integer pool, needed, sequence chosen={})
<span style="color: #008080;">function</span> <span style="color: #000000;">game_combinations</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">pool</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">needed</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">chosen</span><span style="color: #0000FF;">={})</span>
if needed=0 then
<span style="color: #008080;">if</span> <span style="color: #000000;">needed</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
res = append(res,chosen) -- collect the full sets
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">chosen</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- collect the full sets</span>
else
<span style="color: #008080;">else</span>
for i=iff(length(chosen)=0?1:chosen[$]+1) to pool do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">chosen</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">?</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #000000;">chosen</span><span style="color: #0000FF;">[$]+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">pool</span> <span style="color: #008080;">do</span>
res = game_combinations(res,pool,needed-1,append(chosen,i))
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">game_combinations</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pool</span><span style="color: #0000FF;">,</span><span style="color: #000000;">needed</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">chosen</span><span style="color: #0000FF;">),</span><span style="color: #000000;">i</span><span style="color: #0000FF;">))</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return res
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
constant games = game_combinations({},4,2) -- ie {{1,2},{1,3},{1,4},{2,3},{2,4},{3,4}}
<span style="color: #008080;">constant</span> <span style="color: #000000;">games</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">game_combinations</span><span style="color: #0000FF;">({},</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- ie {{1,2},{1,3},{1,4},{2,3},{2,4},{3,4}}</span>
 
constant scores = {{3,0},{1,1},{0,3}} -- ie win/draw/lose
<span style="color: #008080;">constant</span> <span style="color: #000000;">scores</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}}</span> <span style="color: #000080;font-style:italic;">-- ie win/draw/lose</span>
 
sequence points = repeat(repeat(0,10),4) -- 1st..4th place, 0..9 points
<span style="color: #004080;">sequence</span> <span style="color: #000000;">points</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">),</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- 1st..4th place, 0..9 points</span>
 
procedure result_combinations(integer pool, needed, sequence chosen={})
<span style="color: #008080;">procedure</span> <span style="color: #000000;">result_combinations</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">pool</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">needed</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">chosen</span><span style="color: #0000FF;">={})</span>
if needed=0 then
<span style="color: #008080;">if</span> <span style="color: #000000;">needed</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
-- (here, chosen is {1,1,1,1,1,1}..{3,3,3,3,3,3}, 729 in all)
<span style="color: #000080;font-style:italic;">-- (here, chosen is {1,1,1,1,1,1}..{3,3,3,3,3,3}, 729 in all)</span>
sequence results = repeat(0,4)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
for i=1 to length(chosen) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">chosen</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
integer {team1,team2} = games[i],
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">team1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">team2</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">games</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
{points1,points2} = scores[chosen[i]]
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">points1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">points2</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">scores</span><span style="color: #0000FF;">[</span><span style="color: #000000;">chosen</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]]</span>
results[team1] += points1
<span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">team1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">points1</span>
results[team2] += points2
<span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">team2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">points2</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
results = sort(results)
<span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">)</span>
for i=1 to 4 do points[i][results[i]+1] += 1 end for
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">4</span> <span style="color: #008080;">do</span> <span style="color: #000000;">points</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
else
<span style="color: #008080;">else</span>
for i=1 to pool do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">pool</span> <span style="color: #008080;">do</span>
result_combinations(pool,needed-1,append(chosen,i))
<span style="color: #000000;">result_combinations</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pool</span><span style="color: #0000FF;">,</span><span style="color: #000000;">needed</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">chosen</span><span style="color: #0000FF;">),</span><span style="color: #000000;">i</span><span style="color: #0000FF;">))</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
 
-- accumulate the results of all possible outcomes (1..3) of 6 games:
<span style="color: #000080;font-style:italic;">-- accumulate the results of all possible outcomes (1..3) of 6 games:</span>
result_combinations(3,6) -- (the result ends up in points)
<span style="color: #000000;">result_combinations</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (the result ends up in points)
--result_combinations(length(scores),length(games)) -- (equivalent)
--result_combinations(length(scores),length(games)) -- (equivalent)</span>
 
constant fmt = join(repeat("%5d",10))&"\n",
<span style="color: #008080;">constant</span> <span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%5d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">))&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">,</span>
cardinals = {"st","nd","rd","th"}
<span style="color: #000000;">cardinals</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"st"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"nd"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"rd"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"th"</span><span style="color: #0000FF;">}</span>
printf(1," points "&fmt&repeat('-',69)&"\n",tagset(9,0))
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" points "</span><span style="color: #0000FF;">&</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'-'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">69</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">))</span>
for i=1 to 4 do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">4</span> <span style="color: #008080;">do</span>
printf(1,"%d%s place "&fmt,{i,cardinals[i]}&points[5-i])
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d%s place "</span><span style="color: #0000FF;">&</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cardinals</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]}&</span><span style="color: #000000;">points</span><span style="color: #0000FF;">[</span><span style="color: #000000;">5</span><span style="color: #0000FF;">-</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
end for</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,070 ⟶ 1,148:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from itertools import product, combinations, izip
 
scoring = [0, 1, 3]
Line 1,085 ⟶ 1,163:
 
for x in reversed(histo):
print x</langsyntaxhighlight>
{{out}}
<pre>[0, 0, 0, 1, 14, 148, 152, 306, 0, 108]
Line 1,093 ⟶ 1,171:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
;; Tim Brown 2014-09-15
(define (sort-standing stndg#)
Line 1,152 ⟶ 1,230:
4 "Sack the team!"))
 
(show-histogram histogram captions)</langsyntaxhighlight>
 
{{out}}
Line 1,165 ⟶ 1,243:
{{Works with|rakudo|2016.12}}
{{trans|Python}}
<syntaxhighlight lang="raku" perl6line>constant scoring = 0, 1, 3;
my @histo = [0 xx 10] xx 4;
 
Line 1,181 ⟶ 1,259:
}
 
say .fmt('%3d',' ') for @histo.reverse;</langsyntaxhighlight>
{{out}}
<pre> 0 0 0 1 14 148 152 306 0 108
Line 1,191 ⟶ 1,269:
===version 1, static game sets===
{{trans|Java}}
<langsyntaxhighlight lang="rexx">/* REXX -------------------------------------------------------------------*/
results = '000000' /*start with left teams all losing */
games = '12 13 14 23 24 34'
Line 1,263 ⟶ 1,341:
End
End
Return</langsyntaxhighlight>
{{out}}
<pre>[0, 0, 0, 1, 14, 148, 152, 306, 0, 108]
Line 1,293 ⟶ 1,371:
:::* &nbsp; used lowercase spellings of REXX keywords for easier reading.
:::* &nbsp; removed some deadcode &nbsp; (code not needed &nbsp; or &nbsp; code not used).
:::* &nbsp; elided unnecessary/superfluous &nbsp; '''do''' &nbsp; groups and/or loops.
:::* &nbsp; aligned the numbers in the output &nbsp; (used a consisted width/length).
:::* &nbsp; added whitespace within assignments and other REXX clauses.
Line 1,310 ⟶ 1,388:
:::* &nbsp; used an idiomatic method to show the place winners.
:::* &nbsp; added the capability to simulate a &nbsp; ''Cricket World Cup''.
<langsyntaxhighlight lang="rexx">/*REXX pgm calculates world cup standings based on the number of games won by the teams.*/
parse arg teams win . /*obtain optional argument from the CL.*/
if teams=='' | teams=="," then teams= 4 /*Not specified? Then use the default.*/
Line 1,364 ⟶ 1,442:
end /*j*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
th: arg th; return (th/1) || word('th st nd rd', 1 +(th//10) *(th//100%10\==1)*(th//10<4))</langsyntaxhighlight>
Programming note: &nbsp; additional code was add to support a nice-looking grid for displaying the output.
 
Line 1,425 ⟶ 1,503:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">teams = [:a, :b, :c, :d]
matches = teams.combination(2).to_a
outcomes = [:win, :draw, :loss]
Line 1,451 ⟶ 1,529:
puts fmt % [" ", *0..9]
puts fmt % ["-", *["---"]*10]
places_histogram.each.with_index(1) {|hist,place| puts fmt % [place, *hist]}</langsyntaxhighlight>
{{out}}
<pre> : 0 1 2 3 4 5 6 7 8 9
Line 1,461 ⟶ 1,539:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object GroupStage extends App { //team left digit vs team right digit
val games = Array("12", "13", "14", "23", "24", "34")
val points = Array.ofDim[Int](4, 10) //playing 3 games, points range from 0 to 9
Line 1,499 ⟶ 1,577:
println("Fourth place: " + points(0).mkString("[",", ","]"))
 
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{trans|Java}}
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
proc groupStage {} {
foreach n {0 1 2 3} {
Line 1,532 ⟶ 1,610:
foreach nth {First Second Third Fourth} nums [groupStage] {
puts "$nth place:\t[join [lmap n $nums {format %3s $n}] {, }]"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,543 ⟶ 1,621:
=={{header|Visual Basic .NET}}==
{{trans|C++}}
<langsyntaxhighlight lang="vbnet">Imports System.Text
 
Module Module1
Line 1,619 ⟶ 1,697:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>POINTS 0 1 2 3 4 5 6 7 8 9
Line 1,632 ⟶ 1,710:
{{libheader|Wren-fmt}}
{{libheader|Wren-sort}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Conv, Fmt
import "./sort" for Sort
 
var games = ["12", "13", "14", "23", "24", "34"]
Line 1,671 ⟶ 1,749:
points[3-i].each { |p| Fmt.write("$5d", p) }
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 1,685 ⟶ 1,763:
=={{header|Yabasic}}==
{{trans|Java}}
<langsyntaxhighlight Yabasiclang="yabasic">data "12", "13", "14", "23", "24", "34"
 
dim game$(6)
Line 1,797 ⟶ 1,875:
next
print
next</langsyntaxhighlight>
 
=={{header|zkl}}==
{{trans|D}}{{trans|Python}}
<langsyntaxhighlight lang="zkl">combos :=Utils.Helpers.pickNFrom(2,T(0,1,2,3)); // ( (0,1),(0,2) ... )
scoring:=T(0,1,3);
histo :=(0).pump(4,List().write,(0).pump(10,List().write,0).copy); //[4][10] of zeros
Line 1,814 ⟶ 1,892:
foreach h,v in (histo.zip(s.sort())){ h[v]+=1; }
}
foreach h in (histo.reverse()){ println(h.apply("%3d ".fmt).concat()) }</langsyntaxhighlight>
{{out}}
<pre>
9,482

edits