World Cup group stage: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (added whitespace and section headers (;Task;)) |
m (→{{header|Wren}}: Minor tidy) |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 27:
{{trans|Go}}
<
V results = ‘000000’
Line 73:
L(j) 10
print(‘#<5’.format(points[3 - i][j]), end' ‘’)
print()</
{{out}}
Line 87:
=={{header|C}}==
{{trans|C++}}
<
#include <stdio.h>
#include <stdlib.h>
Line 183:
return 0;
}</
{{out}}
<pre>POINTS 0 1 2 3 4 5 6 7 8 9
Line 194:
=={{header|C++}}==
{{trans|Go}}
<
#include <array>
#include <iomanip>
Line 283:
}
return 0;
}</
{{out}}
<pre>POINTS 0 1 2 3 4 5 6 7 8 9
Line 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.
<
using System.Collections.Generic;
using System.Linq;
Line 378:
}
}
</syntaxhighlight>
Produces:
<pre>
Line 391:
=={{header|Common Lisp}}==
{{works with|SBCL|1.4.9}}
<
(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 423:
(dotimes (i (length el))
(format t "~3D " (aref el i)))
(format t "~%"))</
{{out}}
<pre>
Line 435:
This imports the module of the third D solution of the Combinations Task.
{{trans|Python}}
<
import std.stdio, std.range, std.array, std.algorithm, combinations3;
Line 454:
}
writefln("%(%s\n%)", histo[].retro);
}</
{{out}}
<pre>[0, 0, 0, 1, 14, 148, 152, 306, 0, 108]
Line 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.
<
immutable uint[2][6] combs = 4u.iota.array.combinations(2).array;
Line 491:
printf("\n");
}
}</
{{out}}
<pre>0 0 0 1 14 148 152 306 0 108
Line 500:
=={{header|Elena}}==
{{trans|Java}}
ELENA
<
import extensions;
Line 521:
}
{
var points := IntMatrix.allocate(4, 10);
Line 530:
var records := new int[]{0,0,0,0};
for(int i := 0
{
var r := results[i];
Line 544:
records := records.ascendant();
for(int i := 0
{
points[i][records[i]] := points[i][records[i]] + 1
Line 556:
});
}
}</
{{out}}
<pre>
Line 567:
=={{header|Elixir}}==
{{trans|Erlang}}
<
def group_stage do
results = [[3,0],[1,1],[0,3]]
Line 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)</
{{out}}
Line 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].
<
-module(world_cup).
Line 647:
combinations([H|T],List2) ->
[ [{H,Item} | Comb] || Item <- List2, Comb <- combinations(T,List2)].
</syntaxhighlight>
Output:
Line 659:
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 712:
fmt.Println()
}
}</
{{out}}
Line 728:
There might be a more elegant way of expressing this.
<
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</
Here, standings represents all the possible outcomes:
<
729 4</
Of course, not all of them are distinct:
<
556 4</
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):
<
1 2 3 4 6</
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:
<
4 4 4 4</
That's where every team gets 4 standing.
Line 754:
How about outcomes which can be achieved four different ways?
<
6 6 3 3
6 3 3 6
Line 760:
3 6 6 3
3 6 3 6
3 3 6 6</
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:
<
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</
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...):
<
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</
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 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.
<
public class GroupStage{
Line 825:
System.out.println("Fourth place: " + Arrays.toString(points[0]));
}
}</
{{out}}
<pre>First place: [0, 0, 0, 1, 14, 148, 152, 306, 0, 108]
Line 834:
=={{header|Julia}}==
{{trans|Java}}
<
games = ["12", "13", "14", "23", "24", "34"]
results = "000000"
Line 874:
worldcupstages()
</
<pre>
First place: [0, 0, 0, 1, 14, 148, 152, 306, 0, 108]
Line 884:
=={{header|Kotlin}}==
{{trans|Java}}
<
val games = arrayOf("12", "13", "14", "23", "24", "34")
Line 919:
println()
}
}</
{{out}}
Line 933:
=={{header|Lua}}==
{{trans|C++}}
<
local m = {}
for i=1,a do
Line 1,019:
end
print()
end</
{{out}}
<pre>POINTS 0 1 2 3 4 5 6 7 8 9
Line 1,031:
{{trans|Python}}
{{libheader|itertools}}
<
import itertools
Line 1,047:
for x in reversed(histo):
echo x.mapIt(($it).align(3)).join(" ")</
{{out}}
Line 1,057:
=={{header|Perl}}==
{{trans|Raku}}
<
@scoring = (0, 1, 3);
Line 1,077:
$fmt = ('%3d ') x 10 . "\n";
printf $fmt, @$_ for reverse @histo;</
{{out}}
<pre> 0 0 0 1 14 148 152 306 0 108
Line 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.
<!--<
<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>
Line 1,136:
<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>
<!--</
{{out}}
<pre>
Line 1,148:
=={{header|Python}}==
<
scoring = [0, 1, 3]
Line 1,163:
for x in reversed(histo):
print x</
{{out}}
<pre>[0, 0, 0, 1, 14, 148, 152, 306, 0, 108]
Line 1,171:
=={{header|Racket}}==
<
;; Tim Brown 2014-09-15
(define (sort-standing stndg#)
Line 1,230:
4 "Sack the team!"))
(show-histogram histogram captions)</
{{out}}
Line 1,243:
{{Works with|rakudo|2016.12}}
{{trans|Python}}
<syntaxhighlight lang="raku"
my @histo = [0 xx 10] xx 4;
Line 1,259:
}
say .fmt('%3d',' ') for @histo.reverse;</
{{out}}
<pre> 0 0 0 1 14 148 152 306 0 108
Line 1,269:
===version 1, static game sets===
{{trans|Java}}
<
results = '000000' /*start with left teams all losing */
games = '12 13 14 23 24 34'
Line 1,341:
End
End
Return</
{{out}}
<pre>[0, 0, 0, 1, 14, 148, 152, 306, 0, 108]
Line 1,371:
:::* used lowercase spellings of REXX keywords for easier reading.
:::* removed some deadcode (code not needed or code not used).
:::* elided unnecessary/superfluous '''do''' groups and/or loops.
:::* aligned the numbers in the output (used a consisted width/length).
:::* added whitespace within assignments and other REXX clauses.
Line 1,388:
:::* used an idiomatic method to show the place winners.
:::* added the capability to simulate a ''Cricket World Cup''.
<
parse arg teams win . /*obtain optional argument from the CL.*/
if teams=='' | teams=="," then teams= 4 /*Not specified? Then use the default.*/
Line 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))</
Programming note: additional code was add to support a nice-looking grid for displaying the output.
Line 1,503:
=={{header|Ruby}}==
<
matches = teams.combination(2).to_a
outcomes = [:win, :draw, :loss]
Line 1,529:
puts fmt % [" ", *0..9]
puts fmt % ["-", *["---"]*10]
places_histogram.each.with_index(1) {|hist,place| puts fmt % [place, *hist]}</
{{out}}
<pre> : 0 1 2 3 4 5 6 7 8 9
Line 1,539:
=={{header|Scala}}==
<
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,577:
println("Fourth place: " + points(0).mkString("[",", ","]"))
}</
=={{header|Tcl}}==
{{trans|Java}}
{{works with|Tcl|8.6}}
<
proc groupStage {} {
foreach n {0 1 2 3} {
Line 1,610:
foreach nth {First Second Third Fourth} nums [groupStage] {
puts "$nth place:\t[join [lmap n $nums {format %3s $n}] {, }]"
}</
{{out}}
<pre>
Line 1,621:
=={{header|Visual Basic .NET}}==
{{trans|C++}}
<
Module Module1
Line 1,697:
End Sub
End Module</
{{out}}
<pre>POINTS 0 1 2 3 4 5 6 7 8 9
Line 1,710:
{{libheader|Wren-fmt}}
{{libheader|Wren-sort}}
<
import "./sort" for Sort
var games = ["12", "13", "14", "23", "24", "34"]
Line 1,749:
points[3-i].each { |p| Fmt.write("$5d", p) }
System.print()
}</
{{out}}
Line 1,763:
=={{header|Yabasic}}==
{{trans|Java}}
<
dim game$(6)
Line 1,875:
next
print
next</
=={{header|zkl}}==
{{trans|D}}{{trans|Python}}
<
scoring:=T(0,1,3);
histo :=(0).pump(4,List().write,(0).pump(10,List().write,0).copy); //[4][10] of zeros
Line 1,892:
foreach h,v in (histo.zip(s.sort())){ h[v]+=1; }
}
foreach h in (histo.reverse()){ println(h.apply("%3d ".fmt).concat()) }</
{{out}}
<pre>
|