<?php
function randomizeTeam(
int $teamSize,
array $positionPool,
array $positionMins = [],
array $positionMaxs = []
): array {
// guard conditions
foreach ($positionMins as $pos => $min) {
if (!key_exists($pos, $positionPool) || $positionPool[$pos] < $min) {
throw new Exception('Position pool cannot accommodate minimum required positions');
}
}
// input preparation
foreach ($positionMaxs as $pos => $max) {
if (key_exists($pos, $positionPool) && $positionPool[$pos] > $max) {
$positionPool[$pos] = $max;
}
}
$team = [];
$sum = 0;
foreach ($positionPool as $pos => &$pool) {
$team[$pos] = $positionMins[$pos] ?? 0;
$pool -= $team[$pos];
if (!$pool) {
unset($positionPool[$pos]); // remove exhausted pool
}
$sum += $team[$pos];
}
while ($sum < $teamSize && $positionPool) {
$pos = array_rand($positionPool);
++$team[$pos];
--$positionPool[$pos];
if (!$positionPool[$pos]) {
unset($positionPool[$pos]); // remove exhausted pool
}
++$sum;
}
if (array_sum($team) < $teamSize) {
throw new Exception('Position pool was exhausted before filling team');
}
return $team;
}
var_export(
randomizeTeam(
11,
[
'wicket_keeper' => 2,
'batsman' => 6,
'all_rounders' => 5,
'bowlers' => 5,
],
[
'wicket_keeper' => 1,
'batsman' => 3,
'all_rounders' => 1,
'bowlers' => 2,
],
[
'wicket_keeper' => 3,
'batsman' => 5,
'all_rounders' => 5,
'bowlers' => 4,
]
)
);
- Output for 8.0.1, 8.0.23 - 8.0.24, 8.1.3, 8.1.8, 8.1.12, 8.2.4, 8.2.7, 8.2.11
- array (
'wicket_keeper' => 2,
'batsman' => 4,
'all_rounders' => 2,
'bowlers' => 3,
)
- Output for 8.0.11, 8.2.10
- array (
'wicket_keeper' => 1,
'batsman' => 4,
'all_rounders' => 3,
'bowlers' => 3,
)
- Output for 8.0.6, 8.0.12, 8.0.22, 8.1.14, 8.2.6, 8.2.8 - 8.2.9
- array (
'wicket_keeper' => 2,
'batsman' => 5,
'all_rounders' => 2,
'bowlers' => 2,
)
- Output for 8.0.30, 8.1.15 - 8.1.16, 8.2.0, 8.2.5
- array (
'wicket_keeper' => 2,
'batsman' => 3,
'all_rounders' => 3,
'bowlers' => 3,
)
- Output for 8.0.3, 8.0.13, 8.0.20, 8.0.28, 8.2.3
- array (
'wicket_keeper' => 2,
'batsman' => 4,
'all_rounders' => 3,
'bowlers' => 2,
)
- Output for 8.0.25, 8.1.13, 8.1.22, 8.2.2
- array (
'wicket_keeper' => 1,
'batsman' => 5,
'all_rounders' => 3,
'bowlers' => 2,
)
- Output for 8.0.9, 8.0.14 - 8.0.15, 8.0.21, 8.0.27, 8.1.1, 8.1.4, 8.1.18, 8.2.1
- array (
'wicket_keeper' => 2,
'batsman' => 5,
'all_rounders' => 1,
'bowlers' => 3,
)
- Output for 8.0.8, 8.1.24
- array (
'wicket_keeper' => 1,
'batsman' => 3,
'all_rounders' => 4,
'bowlers' => 3,
)
- Output for 8.0.5, 8.0.29, 8.1.9 - 8.1.10, 8.1.20, 8.1.23
- array (
'wicket_keeper' => 1,
'batsman' => 4,
'all_rounders' => 2,
'bowlers' => 4,
)
- Output for 8.0.2, 8.1.21
- array (
'wicket_keeper' => 1,
'batsman' => 5,
'all_rounders' => 2,
'bowlers' => 3,
)
- Output for 8.0.26, 8.1.6 - 8.1.7, 8.1.17, 8.1.19
- array (
'wicket_keeper' => 2,
'batsman' => 3,
'all_rounders' => 2,
'bowlers' => 4,
)
- Output for 8.0.17, 8.0.19, 8.1.2, 8.1.11
- array (
'wicket_keeper' => 2,
'batsman' => 4,
'all_rounders' => 1,
'bowlers' => 4,
)
- Output for 8.0.16, 8.0.18, 8.1.0, 8.1.5
- array (
'wicket_keeper' => 2,
'batsman' => 3,
'all_rounders' => 4,
'bowlers' => 2,
)
- Output for 8.0.10
- array (
'wicket_keeper' => 1,
'batsman' => 5,
'all_rounders' => 1,
'bowlers' => 4,
)
- Output for 8.0.7
- array (
'wicket_keeper' => 1,
'batsman' => 3,
'all_rounders' => 3,
'bowlers' => 4,
)
preferences:
81.04 ms | 401 KiB | 71 Q