Anonymous user
World Cup group stage: Difference between revisions
→{{header|REXX}}: added a second REXX version.
Walterpachl (talk | contribs) m (add REXX) |
(→{{header|REXX}}: added a second REXX version.) |
||
Line 642:
=={{header|REXX}}==
===version 1, static game sets===
{{trans|Java}}
<lang rexx>/* REXX -------------------------------------------------------------------*/
Line 722 ⟶ 723:
[108, 306, 184, 125, 6, 0, 0, 0, 0, 0]</pre>
===version 2, generated game sets===
{{trans|Java}}
{{trans|REXX}}
This REXX version allows the number of teams to be specified to be used in the calculations, and
<br>also generates the character string used for
the ''list of games'' to be played (game sets).
<!-- ........................................................................................................
Programming notes: these are some of the changes from REXX version 1:
:::* the number of teams that are playing can be specified from the command line.
:::* the number of game sets are automatically generate (not static).
:::* used logical (boolean) values instead of integers when appropriate.
:::* elided the need for the '''select''' structure.
:::* ordered the cases (from '''select''') in numerical order.
:::* invoked the sort with a specific number of items to be sorted (not hardcoded).
:::* used exact comparisons (instead of numerical comparisons).
:::* used a consistent spelling of REXX keywords.
:::* used idiomatic variable names instead of hardcoding the indices for arrays.
:::* used lowercase spellings of REXX keywords for easier reading.
:::* removed some deadcode (code not needed or code not used).
:::* elided unnecessary '''do''' groups and/or loops.
:::* aligned the numbers in the output (used a consisted width/length).
:::* added whitespace within assignments and other REXX clauses.
:::* used a consistent indentation for '''do''' groups and/or loops.
:::* aligned statements within '''do''' groups and/or loops.
:::* didn't split '''then''' '''else''' clauses.
:::* used more compound statements (so as to possibly have all the REXX code on one screen).
:::* added '''do─end''' indices comments.
:::* made more idiomatic by using the number of teams instead of hardcoding the #.
:::* used a '''do''' loop instead of using discrete element numbers.
:::* added more comments to explain what the statements are doing.
:::* programmatically determined the length of the largest number that's displayed.
:::* used '''for''' in '''do''' loops instead of '''to''' (faster).
............................................................................................................. !-->
<lang rexx>/*REXX pgm calculates world cup standings based on the number of games won by the teams.*/
parse arg teams . /*obtain optional argument from the CL.*/
if teams=='' | teams=="," then teams= 4 /*Not specified? Then use the default.*/
sets=0; gs= /*the number of sets (so far). */
do j=1 for teams
do k=j+1 to teams; sets= sets+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 /*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 /*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 + 3 /*win for right─most team.*/
if r==1 then do; @.A= @.A + 1; @.B= @.B + 1; end /*draw for both teams*/
if r==2 then @.A= @.A + 3 /*win for left─most team. */
end /*j*/
call sort 4
do t=1 for 4; tm= t - 1; _= @.t
points.tm._ = points.tm._ + 1; z= max(z, length(points.tm._) )
end /*t*/
end /*until*/
$.=
do j=0 for 10; do k=0 for 4; $.k= $.k || right(points.k.j, z)', '; end /*k*/
end /*j*/ /* [↑] last value will have extra ", "*/
/* [↓] tidy up trailing commas, add []*/
do m=3 by -1 for 4; say ' ['strip( strip($.m, "T"), , ',')"]"; end /*m*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
nextResult: procedure expose results sets; if results==copies(2, sets) then return 0
res= 0 /* [↓] do arithmetic in base three.*/
do j=1 for sets; res= res * 3 + substr(results, j, 1)
end /*j*/
res= res + 1
results=; do sets; results= res // 3 || results; res= res % 3
end /*sets*/
return 1
/*──────────────────────────────────────────────────────────────────────────────────────*/
sort: procedure expose @.; arg #; do j=1 for #-1 /*a bubble sort, ascending order.*/
do k=j+1 to # /*swap two elements out of order.*/
if @.k<@.j then parse value @.j @.k with @.k @.j
end /*k*/
end /*j*/; return</lang>
{{out|output|text= when using the default input of: <tt> 4 </tt>}}
<pre>
4 teams, 6 game sets: 12 13 14 23 24 34
[ 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>
{{out|output|text= when using the input of: <tt> 5 </tt>}}
<pre>
5 teams, 10 game sets: 12 13 14 15 23 24 25 34 35 45
[ 0, 0, 0, 1, 37, 1522, 5049, 14496, 14106, 9420]
[ 0, 0, 12, 333, 4269, 14706, 15813, 18156, 3378, 2220]
[ 0, 162, 2406, 8241, 21483, 16566, 7821, 2334, 12, 24]
[ 2916, 11502, 15078, 14753, 12119, 2198, 477, 6, 0, 0]
</pre>
{{out|output|text= when using the input of: <tt> 6 </tt>}}
<pre>
6 teams, 15 game sets: 12 13 14 15 16 23 24 25 26 34 35 36 45 46 56
[ 0, 0, 0, 3, 330, 24741, 227610, 942366, 2364504, 2598813]
[ 0, 0, 108, 7011, 126666, 905661, 2219454, 3997638, 3749148, 1891485]
[ 0, 4374, 104760, 572085, 2028906, 3848769, 3608766, 2964474, 944640, 230391]
[ 236196, 1176606, 2257092, 2963841, 3748998, 2542905, 1030050, 362382, 27588, 3231]
</pre>
<!-- 7 teams with 21 game sets is just a bridge too far. !-->
=={{header|Ruby}}==
|