World Cup group stage: Difference between revisions

m
m (→‎version 2, generated game sets: added capability to simulate Cricket World Cup, added title and indexing to show what is being displayed, boxed and added a grid for the wide displays of points.)
m (→‎{{header|Wren}}: Minor tidy)
 
(39 intermediate revisions by 12 users not shown)
Line 1:
{{task}}
{{task}}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.
 
It's World Cup season (or at least it was when this page was created)!
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.
 
The World Cup is an international football/soccer tournament that happens every 4 years. &nbsp; Countries put their international teams together in the years between tournaments and qualify for the tournament based on their performance in other international games. &nbsp; Once a team has qualified they are put into a group with 3 other teams.
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?".
 
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.
<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>
 
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.
:::* &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++}}
<syntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
// to supply to qsort
int compare(const void *a, const void *b) {
int int_a = *((int *)a);
int int_b = *((int *)b);
return (int_a > int_b) - (int_a < int_b);
}
 
char results[7];
bool next_result() {
char *ptr = results + 5;
int num = 0;
size_t i;
 
// check if the space has been examined
if (strcmp(results, "222222") == 0) {
return false;
}
 
// translate the base 3 string back to a base 10 integer
for (i = 0; results[i] != 0; i++) {
int d = results[i] - '0';
num = 3 * num + d;
}
 
// to the next value to process
num++;
 
// write the base 3 string (fixed width)
while (num > 0) {
int rem = num % 3;
num /= 3;
*ptr-- = rem + '0';
}
// zero fill the remainder
while (ptr > results) {
*ptr-- = '0';
}
 
return true;
}
 
char *games[6] = { "12", "13", "14", "23", "24", "34" };
char *places[4] = { "1st", "2nd", "3rd", "4th" };
int main() {
int points[4][10];
size_t i, j;
 
strcpy(results, "000000");
for (i = 0; i < 4; i++) {
for (j = 0; j < 10; j++) {
points[i][j] = 0;
}
}
 
do {
int records[] = { 0, 0, 0, 0 };
 
for (i = 0; i < 6; i++) {
switch (results[i]) {
case '2':
records[games[i][0] - '1'] += 3;
break;
case '1':
records[games[i][0] - '1']++;
records[games[i][1] - '1']++;
break;
case '0':
records[games[i][1] - '1'] += 3;
break;
default:
break;
}
}
 
qsort(records, 4, sizeof(int), compare);
for (i = 0; i < 4; i++) {
points[i][records[i]]++;
}
} while (next_result());
 
printf("POINTS 0 1 2 3 4 5 6 7 8 9\n");
printf("-----------------------------------------------------------\n");
for (i = 0; i < 4; i++) {
printf("%s place", places[i]);
for (j = 0; j < 10; j++) {
printf("%5d", points[3 - i][j]);
}
printf("\n");
}
 
return 0;
}</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|Go}}
<syntaxhighlight lang="cpp">#include <algorithm>
#include <array>
#include <iomanip>
#include <iostream>
#include <sstream>
 
std::array<std::string, 6> games{ "12", "13", "14", "23", "24", "34" };
std::string results = "000000";
 
int fromBase3(std::string num) {
int out = 0;
for (auto c : num) {
int d = c - '0';
out = 3 * out + d;
}
return out;
}
 
std::string toBase3(int num) {
std::stringstream ss;
 
while (num > 0) {
int rem = num % 3;
num /= 3;
ss << rem;
}
 
auto str = ss.str();
std::reverse(str.begin(), str.end());
return str;
}
 
bool nextResult() {
if (results == "222222") {
return false;
}
 
auto res = fromBase3(results);
 
std::stringstream ss;
ss << std::setw(6) << std::setfill('0') << toBase3(res + 1);
results = ss.str();
 
return true;
}
 
int main() {
std::array<std::array<int, 10>, 4> points;
for (auto &row : points) {
std::fill(row.begin(), row.end(), 0);
}
 
do {
std::array<int, 4> records {0, 0, 0, 0 };
 
for (size_t i = 0; i < games.size(); i++) {
switch (results[i]) {
case '2':
records[games[i][0] - '1'] += 3;
break;
case '1':
records[games[i][0] - '1']++;
records[games[i][1] - '1']++;
break;
case '0':
records[games[i][1] - '1'] += 3;
break;
default:
break;
}
}
 
std::sort(records.begin(), records.end());
for (size_t i = 0; i < records.size(); i++) {
points[i][records[i]]++;
}
} while (nextResult());
 
std::cout << "POINTS 0 1 2 3 4 5 6 7 8 9\n";
std::cout << "-------------------------------------------------------------\n";
std::array<std::string, 4> places{ "1st", "2nd", "3rd", "4th" };
for (size_t i = 0; i < places.size(); i++) {
std::cout << places[i] << " place";
for (size_t j = 0; j < points[i].size(); j++) {
std::cout << std::setw(5) << points[3 - i][j];
}
std::cout << '\n';
}
return 0;
}</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 sharp|C#}}==
Line 12 ⟶ 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 93 ⟶ 378:
}
}
</syntaxhighlight>
</lang>
Produces:
<pre>
Line 106 ⟶ 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 138 ⟶ 423:
(dotimes (i (length el))
(format t "~3D " (aref el i)))
(format t "~%"))</langsyntaxhighlight>
{{out}}
<pre>
Line 150 ⟶ 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 169 ⟶ 454:
}
writefln("%(%s\n%)", histo[].retro);
}</langsyntaxhighlight>
{{out}}
<pre>[0, 0, 0, 1, 14, 148, 152, 306, 0, 108]
Line 177 ⟶ 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 206 ⟶ 491:
printf("\n");
}
}</langsyntaxhighlight>
{{out}}
<pre>0 0 0 1 14 148 152 306 0 108
Line 215 ⟶ 500:
=={{header|Elena}}==
{{trans|Java}}
ELENA 36.2.1x :
<lang elenasyntaxhighlight>import system'routines.;
import extensions.;
 
public singleton program
program =
{
static games := (new string[]{"12", "13", "14", "23", "24", "34").};
static results := "000000".;
nextResult()
[{
ifvar (resultss :=="222222") [ ^ false ].results;
results :=if (results toInt(3=="222222") +{ 1)^ toLiteral(3)false }; padLeft($48, 6).
results := (results.toInt(3) + 1).toString(3).padLeft($48, 6);
^ true
]}
function()
// program entry point
eval{
var points := IntMatrix.allocate(4, 10);
[
var points := IntMatrix new(4, 10).
int counter := 0;
[do
var r := results.{
var records := IntArray new(4). int[]{0,0,0,0};
0for(int i till:= 0; i < 6; do(:i += 1)
[{
(var r := results[i]) =>;
r "2" [ records[games[i][0] toInt - 49] += 3 ];>
"12" { records[games[i][0].toInt() - 49] := records[games[i][0].toInt() - 49] + 3 }
"1" records[games[i][0] toInt - 49] += 1.{
records[games[i][10].toInt() - 49] := records[games[i][0].toInt() - 49] += 1;
records[games[i];[1].toInt() - 49] := records[games[i][1].toInt() - 49] + 1
"0" [ records[games[i][1] toInt - 49] += 3 ].}
"0" { records[games[i][1].toInt() - 49] := records[games[i][1].toInt() - 49] + 3 };
].
};
records := records ascendant.
records := records.ascendant();
0 to:3 do(:i)[ points[i][records[i]] += 1 ].
for(int i := 0; i <= 3; i += 1)
] repeatUntil:$($self nextResult). {
points[i][records[i]] := points[i][records[i]] + 1
}
0 repeatTill:4; zip:("1st", "2nd", "3rd", "4th") forEach(:i:l)
[}
while(program.nextResult());
console printLine(l,": ", points[3 - i])
].
new Range(0, 4).zipForEach(new string[]{"1st", "2nd", "3rd", "4th"}, (i,l)
]
{
}.</lang>
console.printLine(l,": ", points[3 - i].toArray())
});
}
}</syntaxhighlight>
{{out}}
<pre>
Line 276 ⟶ 567:
=={{header|Elixir}}==
{{trans|Erlang}}
<langsyntaxhighlight lang="elixir">defmodule World_Cup do
def group_stage do
results = [[3,0],[1,1],[0,3]]
Line 307 ⟶ 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 322 ⟶ 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 356 ⟶ 647:
combinations([H|T],List2) ->
[ [{H,Item} | Comb] || Item <- List2, Comb <- combinations(T,List2)].
</syntaxhighlight>
</lang>
 
Output:
Line 368 ⟶ 659:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 421 ⟶ 712:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 437 ⟶ 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 463 ⟶ 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 469 ⟶ 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 494 ⟶ 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 534 ⟶ 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 540 ⟶ 831:
Third place: [0, 18, 136, 273, 290, 4, 8, 0, 0, 0]
Fourth place: [108, 306, 184, 125, 6, 0, 0, 0, 0, 0]</pre>
 
=={{header|Julia}}==
{{trans|Java}}
<syntaxhighlight lang="julia">function worldcupstages()
games = ["12", "13", "14", "23", "24", "34"]
results = "000000"
 
function nextresult()
if (results == "222222")
return false
end
results = lpad(string(parse(Int, results, base=3) + 1, base=3), 6, '0')
true
end
 
points = zeros(Int, 4, 10)
while true
records = zeros(Int, 4)
for i in 1:length(games)
if results[i] == '2'
records[games[i][1] - '0'] += 3
elseif results[i] == '1'
records[games[i][1] - '0'] += 1
records[games[i][2] - '0'] += 1
elseif results[i] == '0'
records[games[i][2] - '0'] += 3
end
end
sort!(records)
for i in 1:4
points[i, records[i] + 1] += 1
end
if !nextresult()
break
end
end
 
for (i, place) in enumerate(["First", "Second", "Third", "Fourth"])
println("$place place: $(points[5 - i, :])")
end
end
 
worldcupstages()
</syntaxhighlight>{{out}}
<pre>
First place: [0, 0, 0, 1, 14, 148, 152, 306, 0, 108]
Second place: [0, 0, 4, 33, 338, 172, 164, 18, 0, 0]
Third place: [0, 18, 136, 273, 290, 4, 8, 0, 0, 0]
Fourth place: [108, 306, 184, 125, 6, 0, 0, 0, 0, 0]
</pre>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
val games = arrayOf("12", "13", "14", "23", "24", "34")
Line 578 ⟶ 919:
println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 590 ⟶ 931:
</pre>
 
=={{header|Perl 6Lua}}==
{{trans|C++}}
{{Works with|rakudo|2016.12}}
<syntaxhighlight lang="lua">function array1D(a, d)
local m = {}
for i=1,a do
table.insert(m, d)
end
return m
end
 
function array2D(a, b, d)
local m = {}
for i=1,a do
table.insert(m, array1D(b, d))
end
return m
end
 
function fromBase3(num)
local out = 0
for i=1,#num do
local c = num:sub(i,i)
local d = tonumber(c)
out = 3 * out + d
end
return out
end
 
function toBase3(num)
local ss = ""
while num > 0 do
local rem = num % 3
num = math.floor(num / 3)
ss = ss .. tostring(rem)
end
return string.reverse(ss)
end
 
games = { "12", "13", "14", "23", "24", "34" }
 
results = "000000"
function nextResult()
if results == "222222" then
return false
end
 
local res = fromBase3(results)
results = string.format("%06s", toBase3(res + 1))
 
return true
end
 
points = array2D(4, 10, 0)
 
repeat
local records = array1D(4, 0)
 
for i=1,#games do
if results:sub(i,i) == '2' then
local j = tonumber(games[i]:sub(1,1))
records[j] = records[j] + 3
elseif results:sub(i,i) == '1' then
local j = tonumber(games[i]:sub(1,1))
records[j] = records[j] + 1
j = tonumber(games[i]:sub(2,2))
records[j] = records[j] + 1
elseif results:sub(i,i) == '0' then
local j = tonumber(games[i]:sub(2,2))
records[j] = records[j] + 3
end
end
 
table.sort(records)
for i=1,#records do
points[i][records[i]+1] = points[i][records[i]+1] + 1
end
until not nextResult()
 
print("POINTS 0 1 2 3 4 5 6 7 8 9")
print("-------------------------------------------------------------")
places = { "1st", "2nd", "3rd", "4th" }
for i=1,#places do
io.write(places[i] .. " place")
local row = points[i]
for j=1,#row do
io.write(string.format("%5d", points[5 - i][j]))
end
print()
end</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|Nim}}==
{{trans|Python}}
{{libheader|itertools}}
<lang perl6>constant scoring = 0, 1, 3;
<syntaxhighlight lang="nim">import algorithm, sequtils, strutils
my @histo = [0 xx 10] xx 4;
import itertools
 
const Scoring = [0, 1, 3]
for [X] ^3 xx 6 -> @results {
my @s;
 
var histo: array[4, array[10, int]]
for @results Z (^4).combinations(2) -> ($r, @g) {
@s[@g[0]] += scoring[$r];
@s[@g[1]] += scoring[2 - $r];
}
 
for results in product([0, 1, 2], repeat = 6):
for @histo Z @s.sort -> (@h, $v) {
var s: array[4, int]
++@h[$v];
for (r, g) in zip(results, toSeq(combinations([0, 1, 2, 3], 2))):
s[g[0]] += Scoring[r]
s[g[1]] += Scoring[2 - r]
for i, v in sorted(s):
inc histo[i][v]
 
for x in reversed(histo):
echo x.mapIt(($it).align(3)).join(" ")</syntaxhighlight>
 
{{out}}
<pre> 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</pre>
 
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl">use Math::Cartesian::Product;
 
@scoring = (0, 1, 3);
push @histo, [(0) x 10] for 1..4;
push @aoa, [(0,1,2)] for 1..6;
 
for $results (cartesian {@_} @aoa) {
my @s;
my @g = ([0,1],[0,2],[0,3],[1,2],[1,3],[2,3]);
for (0..$#g) {
$r = $results->[$_];
$s[$g[$_][0]] += $scoring[$r];
$s[$g[$_][1]] += $scoring[2 - $r];
}
 
my @ss = sort @s;
$histo[$_][$ss[$_]]++ for 0..$#s;
}
 
say .$fmt = ('%3d',' ') forx 10 @histo.reverse "\n";</lang>
printf $fmt, @$_ for reverse @histo;</syntaxhighlight>
{{out}}
<pre> 0 0 0 1 14 148 152 306 0 108
Line 615 ⟶ 1,083:
0 18 136 273 290 4 8 0 0 0
108 306 184 125 6 0 0 0 0 0</pre>
 
=={{header|Phix}}==
There is no official combinations() routine in phix, instead the documentation for
permute() shows two examples that you are expected to copy and modify, every time.<br>
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)-->
<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>
<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>
<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>
<span style="color: #008080;">else</span>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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>
<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>
<span style="color: #008080;">else</span>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000080;font-style:italic;">-- accumulate the results of all possible outcomes (1..3) of 6 games:</span>
<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)</span>
<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>
<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>
<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>
<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: #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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</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|Python}}==
<langsyntaxhighlight lang="python">from itertools import product, combinations, izip
 
scoring = [0, 1, 3]
Line 632 ⟶ 1,163:
 
for x in reversed(histo):
print x</langsyntaxhighlight>
{{out}}
<pre>[0, 0, 0, 1, 14, 148, 152, 306, 0, 108]
Line 640 ⟶ 1,171:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
;; Tim Brown 2014-09-15
(define (sort-standing stndg#)
Line 699 ⟶ 1,230:
4 "Sack the team!"))
 
(show-histogram histogram captions)</langsyntaxhighlight>
 
{{out}}
Line 707 ⟶ 1,238:
Sack the manager: 0 18 136 273 290 4 8 0 0 0
Sack the team! 108 306 184 125 6 0 0 0 0 0 </pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2016.12}}
{{trans|Python}}
<syntaxhighlight lang="raku" line>constant scoring = 0, 1, 3;
my @histo = [0 xx 10] xx 4;
 
for [X] ^3 xx 6 -> @results {
my @s;
 
for @results Z (^4).combinations(2) -> ($r, @g) {
@s[@g[0]] += scoring[$r];
@s[@g[1]] += scoring[2 - $r];
}
 
for @histo Z @s.sort -> (@h, $v) {
++@h[$v];
}
}
 
say .fmt('%3d',' ') for @histo.reverse;</syntaxhighlight>
{{out}}
<pre> 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</pre>
 
=={{header|REXX}}==
===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 783 ⟶ 1,341:
End
End
Return</langsyntaxhighlight>
{{out}}
<pre>[0, 0, 0, 1, 14, 148, 152, 306, 0, 108]
Line 798 ⟶ 1,356:
<br>also generates the character string used for
the &nbsp; ''list of games'' &nbsp; to be played &nbsp; (game sets).
 
<!-- ........................................................................................................
This REXX version can also simulate a Cricket World Cup &nbsp; (by specifying &nbsp; '''2''' &nbsp; for the &nbsp; '''win''' &nbsp; variable).
 
Programming notes: these are some of the changes from REXX version 1:
:::* &nbsp; the number of teams that are playing can be specified from the command line.
:::* &nbsp; the number of game sets are automatically generategenerated &nbsp; (not static).
:::* &nbsp; used logical (boolean) values instead of integers when appropriate.
:::* &nbsp; elided the need for the &nbsp; '''select''' &nbsp; structure.
:::* &nbsp; ordered the cases &nbsp; (from '''select''') &nbsp; in numerical order.
:::* &nbsp; invoked the sort with a specific number of items to be sorted &nbsp; (not hardcodedhard-coded).
:::* &nbsp; used exact comparisons &nbsp; (instead of numerical comparisons).
:::* &nbsp; used a consistent spelling of REXX keywords.
Line 811 ⟶ 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.
:::* &nbsp; used a consistent indentation for &nbsp; '''do''' &nbsp; groups and/or loops.
:::* &nbsp; aligned statements within &nbsp; '''do''' &nbsp; groups and/or loops.
:::* &nbsp; didn't split &nbsp; '''then''' &nbsp; '''else''' &nbsp; clauses.
:::* &nbsp; used more compound statements &nbsp; (so as to possibly have all the REXX code on one screen).
Line 823 ⟶ 1,383:
:::* &nbsp; added more comments to explain what the statements are doing.
:::* &nbsp; programmatically determined the length of the largest number that's displayed.
:::* &nbsp; used &nbsp; '''for''' &nbsp; in &nbsp; '''do''' &nbsp; loops instead of &nbsp; '''to''' &nbsp; (faster).
:::* &nbsp; added a title and indices for the capabilityboxed output to simulateindicate awhat Cricketis Worldbeing Cupdisplayed.
:::* &nbsp; used an idiomatic value for the &nbsp; ''number'' &nbsp; of columns for the "point" numbers.
............................................................................................................. !-->
:::* &nbsp; used an idiomatic method to show the place winners.
<lang rexx>/*REXX pgm calculates world cup standings based on the number of games won by the teams.*/
:::* &nbsp; added the capability to simulate a &nbsp; ''Cricket World Cup''.
<syntaxhighlight 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.*/
if win=='' | win=="," then win= 3 /* " " " " " " */
sets= 0; gs= /*the number of sets (so far). */
do j=1 for teams
do k=j+1 to teams; sets= sets +1 1 /*bump the number of game sets. */
games.sets= j || k; gs= gs j || k /*generate the game combinations. */
end /*j*/
end /*k*/
z = 1; setLimit= copies(2, sets) /*Z: max length of any number shown. */
say teams ' teams, ' sets " game sets: " gs /*display what's being used for calcs. */
results = copies(0, sets); say say /*start with left-most teams all losing*/
points. = 0 /*zero all the team's point. */
do until \nextResult(results); @.= 0
do j=1 for sets; r= substr( results, j, 1)
parse var games.j A +1 B /*get the A and B teams*/
if r==0 then @.B= @.B + win /*win for right─most team.*/
if r==1 then do; @.A= @.A + 1; @.B= @.B + 1; end /*draw for both teams*/
Line 848 ⟶ 1,410:
end /*j*/
call sort teams
do t=1 for teams; tm= t - 1; _= @.t
points.tm._ = points.tm._ + 1; z= max(z, length( points.tm._) )
end /*t*/
end /*until*/
$.=
do j=0 for teams+6; do k=0 for teams; $.k= $.k || right( points.k.j, z)'│ '; end
end /*j*/
do k=0 for teams; $.k= $.k || right(points.k.j, z)'│ '; end /*k*/
say /* [↓] build grid line for the box*/
end /*j*/
L= length($.1) -2; $$= translate( translate( left($.1, L), , 0123456789), '─', " ")
say; /* [↓] build grid line for the box*/
say left('', 15) center("points", L) /*display the boxed title. */
L=length($.1) -2; $$= translate( translate( left($.1, L), , 0123456789), '─', " ")
say left('', 15) center('points', L) /*display the boxed title. */
say left('', 15) "╔"translate($$, '═╤', "─│")'╗' /*display the bottom sep for title.*/
p= 0
do m=teams-1 by -1 for teams; p = p + 1 /*bump the place holder (counter)*/
say right('('th(p) "place)", 14) " ║"left($.m, L)'║'
if m>0 then say right(' ', 14) " ╟"translate($$, '┼', "│")'╢'
end /*m*/
say left('', 15) "╚"translate( $$, '═╧', "─│")'╝' /*display the bottom sep for title.*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
nextResult: procedure exposeif results==setLimit sets; then return 0 /* if[↓] results==copies(2, sets)do arithmetic thenin returnbase 0three. */
res= 0; do k=1 for sets; res= res * 3 + substr( results, k, /* [↓] do arithmetic in base three.*/1)
end do j=1 for sets; res= res /* 3 + substr(results, j, 1)k*/
results=; end /*j*/ res= res + 1
do sets; results= res // 3 || results; res= res +% 13
results=; do sets; end results= res /*sets*/; 3 || results; res= res %return 31
end /*sets*/
return 1
/*──────────────────────────────────────────────────────────────────────────────────────*/
sort: procedure expose @.; arg #; do j=1 for #-1 /*a bubble sort, ascending order.*/
Line 883 ⟶ 1,442:
end /*j*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
th: arg th; th=return (th/1; return) th|| 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.
 
 
{{out|output|text=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 4 </tt>}}
<pre>
Line 941 ⟶ 1,503:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">teams = [:a, :b, :c, :d]
matches = teams.combination(2).to_a
outcomes = [:win, :draw, :loss]
Line 967 ⟶ 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 975 ⟶ 1,537:
3 : 0 18 136 273 290 4 8 0 0 0
4 : 108 306 184 125 6 0 0 0 0 0</pre>
 
=={{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,014 ⟶ 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,047 ⟶ 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,055 ⟶ 1,618:
Fourth place: 108, 306, 184, 125, 6, 0, 0, 0, 0, 0
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C++}}
<syntaxhighlight lang="vbnet">Imports System.Text
 
Module Module1
 
Dim games As New List(Of String) From {"12", "13", "14", "23", "24", "34"}
Dim results = "000000"
 
Function FromBase3(num As String) As Integer
Dim out = 0
For Each c In num
Dim d = Asc(c) - Asc("0"c)
out = 3 * out + d
Next
Return out
End Function
 
Function ToBase3(num As Integer) As String
Dim ss As New StringBuilder
 
While num > 0
Dim re = num Mod 3
num \= 3
ss.Append(re)
End While
 
Return New String(ss.ToString().Reverse().ToArray())
End Function
 
Function NextResult() As Boolean
If results = "222222" Then
Return False
End If
 
Dim res = FromBase3(results)
 
Dim conv = ToBase3(res + 1)
results = conv.PadLeft(6, "0"c)
 
Return True
End Function
 
Sub Main()
Dim points(0 To 3, 0 To 9) As Integer
Do
Dim records(0 To 3) As Integer
For index = 0 To games.Count - 1
Select Case results(index)
Case "2"c
records(Asc(games(index)(0)) - Asc("1"c)) += 3
Case "1"c
records(Asc(games(index)(0)) - Asc("1"c)) += 1
records(Asc(games(index)(1)) - Asc("1"c)) += 1
Case "0"c
records(Asc(games(index)(1)) - Asc("1"c)) += 3
End Select
Next
 
Array.Sort(records)
For index = 0 To records.Length - 1
Dim t = records(index)
points(index, t) += 1
Next
Loop While NextResult()
 
Console.WriteLine("POINTS 0 1 2 3 4 5 6 7 8 9")
Console.WriteLine("-------------------------------------------------------------")
Dim places As New List(Of String) From {"1st", "2nd", "3rd", "4th"}
For i = 0 To places.Count - 1
Console.Write("{0} place", places(i))
For j = 0 To 9
Console.Write("{0,5}", points(3 - i, j))
Next
Console.WriteLine()
Next
End Sub
 
End Module</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|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
{{libheader|Wren-sort}}
<syntaxhighlight lang="wren">import "./fmt" for Conv, Fmt
import "./sort" for Sort
 
var games = ["12", "13", "14", "23", "24", "34"]
var results = "000000"
 
var nextResult = Fn.new {
if (results == "222222") return false
var res = Conv.atoi(results, 3) + 1
results = Fmt.swrite("$06t", res)
return true
}
 
var points = List.filled(4, null)
for (i in 0..3) points[i] = List.filled(10, 0)
while (true) {
var records = List.filled(4, 0)
for (i in 0..5) {
var g0 = Num.fromString(games[i][0]) - 1
var g1 = Num.fromString(games[i][1]) - 1
if (results[i] == "2") {
records[g0] = records[g0] + 3
} else if (results[i] == "1") {
records[g0] = records[g0] + 1
records[g1] = records[g1] + 1
} else if (results[i] == "0") {
records[g1] = records[g1] + 3
}
}
Sort.insertion(records)
for (i in 0..3) points[i][records[i]] = points[i][records[i]] +1
if (!nextResult.call()) break
}
System.print("POINTS 0 1 2 3 4 5 6 7 8 9")
System.print("---------------------------------------------------------------")
for (i in 0..3) {
Fmt.write("$r place ", i+1)
points[3-i].each { |p| Fmt.write("$5d", p) }
System.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|Yabasic}}==
{{trans|Java}}
<syntaxhighlight lang="yabasic">data "12", "13", "14", "23", "24", "34"
 
dim game$(6)
 
for i = 1 to 6 : read game$(i) : next
 
result$ = "000000"
 
 
sub ParseInt(number$, base)
local x, i, pot, digits
digits = len(number$)
 
for i = digits to 1 step -1
x = x + base^pot * dec(mid$(number$, i, 1))
pot = pot + 1
next
 
return x
end sub
 
 
sub Format$(decimal, base)
local cociente, i, j, conv$
 
repeat
cociente = int(decimal / base)
conv$ = str$(mod(decimal, base)) + conv$
decimal = cociente
i = i + 1
until(cociente = 0)
 
return conv$
end sub
 
sub nextResult()
if result$ = "222222" return false
res = ParseInt(result$, 3)
result$ = Format$(res+1, 3)
while(len(result$) < 6) result$ = "0" + result$ wend
return true
end sub
 
 
sub Sort(array())
local n, i, t, sw
n = arraysize(array(), 1)
 
repeat
sw = false
for i = 0 to n - 1
if array(i) > array(i + 1) then
sw = true
t = array(i)
array(i) = array(i + 1)
array(i + 1) = t
end if
next
until(not sw)
end sub
 
dim points(4, 10)
 
sub compute()
local records(4), i, t
for i = 1 to arraysize(game$(), 1)
switch mid$(result$, i, 1)
case "2":
t = val(mid$(game$(i), 1, 1))
records(t) = records(t) + 3
break
case "1":
t = val(mid$(game$(i), 1, 1))
records(t) = records(t) + 1
t = val(mid$(game$(i), 2, 1))
records(t) = records(t) + 1
break
case "0":
t = val(mid$(game$(i), 2, 1))
records(t) = records(t) + 3
break
end switch
next
Sort(records())
for i = 1 to 4
points(i, records(i)) = points(i, records(i)) + 1
next
if not nextResult() return false
return true
end sub
 
repeat until(not compute())
 
print "POINTS 0 1 2 3 4 5 6 7 8 9"
print "-------------------------------------------------------------"
 
dim place$(4)
 
data "1st", "2nd", "3rd", "4th"
for i = 1 to 4 : read place$(i) : next
 
for i = 1 to 4
print place$(i), " place ";
for j = 0 to 9
print points(5 - i, j) using "%-4.0f";
next
print
next</syntaxhighlight>
 
=={{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,071 ⟶ 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,476

edits