World Cup group stage: Difference between revisions

→‎{{header|REXX}}: added a second REXX version.
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, &nbsp; and
<br>also generates the character string used for
the &nbsp; ''list of games'' &nbsp; to be played &nbsp; (game sets).
 
<!-- ........................................................................................................
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 generate &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 hardcoded).
:::* &nbsp; used exact comparisons &nbsp; (instead of numerical comparisons).
:::* &nbsp; used a consistent spelling of REXX keywords.
:::* &nbsp; used idiomatic variable names instead of hardcoding the indices for arrays.
:::* &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 &nbsp; '''do''' 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''' groups and/or loops.
:::* &nbsp; aligned statements within &nbsp; '''do''' 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).
:::* &nbsp; added &nbsp; '''do─end''' &nbsp; indices comments.
:::* &nbsp; made more idiomatic by using the number of teams instead of hardcoding the #.
:::* &nbsp; used a &nbsp; '''do''' &nbsp; loop instead of using discrete element numbers.
:::* &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 '''to''' &nbsp; (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=&nbsp; when using the default input of: &nbsp; &nbsp; <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=&nbsp; when using the input of: &nbsp; &nbsp; <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=&nbsp; when using the input of: &nbsp; &nbsp; <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}}==