World Cup group stage: Difference between revisions

Line 644:
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.
<lang Phix>function game_combinations(sequence res, integer pool, needed, sequence chosen={})
if needed=0 then
res = append(res,chosen) -- collect the full sets
else
for i=iff(length(chosen)=0?1:chosen[$]+1) to pool do
res = game_combinations(res,pool,needed-1,append(chosen,i))
end for
end if
return res
end function
 
constant games = game_combinations({},4,2) -- ie {{1,2},{1,3},{1,4},{2,3},{2,4},{3,4}}
 
constant scores = {{3,0},{1,1},{0,3}} -- ie win/draw/lose
 
sequence points = repeat(repeat(0,10),4) -- 1st..4th place, 0..9 points
 
procedure result_combinations(integer pool, needed, sequence chosen={})
if needed=0 then
-- (here, chosen is {1,1,1,1,1,1}..{3,3,3,3,3,3}, 729 in all)
sequence results = repeat(0,4)
for i=1 to length(chosen) do
integer {team1,team2} = games[i],
{points1,points2} = scores[chosen[i]]
results[team1] += points1
results[team2] += points2
end for
results = sort(results)
for i=1 to 4 do points[i][results[i]+1] += 1 end for
else
for i=1 to pool do
result_combinations(pool,needed-1,append(chosen,i))
end for
end if
end procedure
 
-- accumulate the results of all possible outcomes (1..3) of 6 games:
result_combinations(3,6) -- (the result ends up in points)
--result_combinations(length(scores),length(games)) -- (equivalent)
 
constant fmt = join(repeat("%5d",10))&"\n",
cardinals = {"st","nd","rd","th"}
printf(1," points "&fmt&repeat('-',69)&"\n",tagset(9,0))
for i=1 to 4 do
printf(1,"%d%s place "&fmt,{i,cardinals[i]}&points[5-i])
end for</lang>
{{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}}==
7,805

edits