3v4l.org

run code in 300+ PHP versions simultaneously
<?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, ] ) );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fTeqPT
function name:  (null)
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   INIT_FCALL                                               'var_export'
   52     1        INIT_FCALL                                               'randomizeteam'
   53     2        SEND_VAL                                                 11
   55     3        SEND_VAL                                                 <array>
   61     4        SEND_VAL                                                 <array>
   67     5        SEND_VAL                                                 <array>
   52     6        DO_FCALL                                      0  $0      
   67     7        SEND_VAR                                                 $0
   51     8        DO_ICALL                                                 
   73     9      > RETURN                                                   1

Function randomizeteam:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 22
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 22
Branch analysis from position: 6
2 jumps found. (Code = 47) Position 1 = 13, Position 2 = 16
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 21
Branch analysis from position: 17
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 16
Branch analysis from position: 22
2 jumps found. (Code = 77) Position 1 = 24, Position 2 = 38
Branch analysis from position: 24
2 jumps found. (Code = 78) Position 1 = 25, Position 2 = 38
Branch analysis from position: 25
2 jumps found. (Code = 46) Position 1 = 31, Position 2 = 34
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 37
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 37
Branch analysis from position: 34
Branch analysis from position: 38
2 jumps found. (Code = 125) Position 1 = 42, Position 2 = 57
Branch analysis from position: 42
2 jumps found. (Code = 126) Position 1 = 43, Position 2 = 57
Branch analysis from position: 43
2 jumps found. (Code = 43) Position 1 = 53, Position 2 = 54
Branch analysis from position: 53
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
Branch analysis from position: 54
Branch analysis from position: 57
1 jumps found. (Code = 42) Position 1 = 72
Branch analysis from position: 72
2 jumps found. (Code = 46) Position 1 = 74, Position 2 = 75
Branch analysis from position: 74
2 jumps found. (Code = 44) Position 1 = 76, Position 2 = 59
Branch analysis from position: 76
2 jumps found. (Code = 43) Position 1 = 81, Position 2 = 85
Branch analysis from position: 81
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 85
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 59
2 jumps found. (Code = 43) Position 1 = 70, Position 2 = 71
Branch analysis from position: 70
2 jumps found. (Code = 46) Position 1 = 74, Position 2 = 75
Branch analysis from position: 74
Branch analysis from position: 75
Branch analysis from position: 71
Branch analysis from position: 75
Branch analysis from position: 57
Branch analysis from position: 38
Branch analysis from position: 22
filename:       /in/fTeqPT
function name:  randomizeTeam
number of ops:  89
compiled vars:  !0 = $teamSize, !1 = $positionPool, !2 = $positionMins, !3 = $positionMaxs, !4 = $min, !5 = $pos, !6 = $max, !7 = $team, !8 = $sum, !9 = $pool
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <array>
          3        RECV_INIT                                        !3      <array>
   10     4      > FE_RESET_R                                       $10     !2, ->22
          5    > > FE_FETCH_R                                       ~11     $10, !4, ->22
          6    >   ASSIGN                                                   !5, ~11
   11     7        INIT_FCALL                                               'key_exists'
          8        SEND_VAR                                                 !5
          9        SEND_VAR                                                 !1
         10        DO_ICALL                                         $13     
         11        BOOL_NOT                                         ~14     $13
         12      > JMPNZ_EX                                         ~14     ~14, ->16
         13    >   FETCH_DIM_R                                      ~15     !1, !5
         14        IS_SMALLER                                       ~16     ~15, !4
         15        BOOL                                             ~14     ~16
         16    > > JMPZ                                                     ~14, ->21
   12    17    >   NEW                                              $17     'Exception'
         18        SEND_VAL_EX                                              'Position+pool+cannot+accommodate+minimum+required+positions'
         19        DO_FCALL                                      0          
         20      > THROW                                         0          $17
   10    21    > > JMP                                                      ->5
         22    >   FE_FREE                                                  $10
   17    23      > FE_RESET_R                                       $19     !3, ->38
         24    > > FE_FETCH_R                                       ~20     $19, !6, ->38
         25    >   ASSIGN                                                   !5, ~20
   18    26        INIT_FCALL                                               'key_exists'
         27        SEND_VAR                                                 !5
         28        SEND_VAR                                                 !1
         29        DO_ICALL                                         $22     
         30      > JMPZ_EX                                          ~23     $22, ->34
         31    >   FETCH_DIM_R                                      ~24     !1, !5
         32        IS_SMALLER                                       ~25     !6, ~24
         33        BOOL                                             ~23     ~25
         34    > > JMPZ                                                     ~23, ->37
   19    35    >   ASSIGN_DIM                                               !1, !5
         36        OP_DATA                                                  !6
   17    37    > > JMP                                                      ->24
         38    >   FE_FREE                                                  $19
   23    39        ASSIGN                                                   !7, <array>
   24    40        ASSIGN                                                   !8, 0
   25    41      > FE_RESET_RW                                      $29     !1, ->57
         42    > > FE_FETCH_RW                                      ~30     $29, !9, ->57
         43    >   ASSIGN                                                   !5, ~30
   26    44        FETCH_DIM_IS                                     ~33     !2, !5
         45        COALESCE                                         ~34     ~33
         46        QM_ASSIGN                                        ~34     0
         47        ASSIGN_DIM                                               !7, !5
         48        OP_DATA                                                  ~34
   27    49        FETCH_DIM_R                                      ~35     !7, !5
         50        ASSIGN_OP                                     2          !9, ~35
   28    51        BOOL_NOT                                         ~37     !9
         52      > JMPZ                                                     ~37, ->54
   29    53    >   UNSET_DIM                                                !1, !5
   31    54    >   FETCH_DIM_R                                      ~38     !7, !5
         55        ASSIGN_OP                                     1          !8, ~38
   25    56      > JMP                                                      ->42
         57    >   FE_FREE                                                  $29
   34    58      > JMP                                                      ->72
   35    59    >   INIT_FCALL                                               'array_rand'
         60        SEND_VAR                                                 !1
         61        DO_ICALL                                         $40     
         62        ASSIGN                                                   !5, $40
   36    63        FETCH_DIM_RW                                     $42     !7, !5
         64        PRE_INC                                                  $42
   37    65        FETCH_DIM_RW                                     $44     !1, !5
         66        PRE_DEC                                                  $44
   38    67        FETCH_DIM_R                                      ~46     !1, !5
         68        BOOL_NOT                                         ~47     ~46
         69      > JMPZ                                                     ~47, ->71
   39    70    >   UNSET_DIM                                                !1, !5
   41    71    >   PRE_INC                                                  !8
   34    72    >   IS_SMALLER                                       ~49     !8, !0
         73      > JMPZ_EX                                          ~49     ~49, ->75
         74    >   BOOL                                             ~49     !1
         75    > > JMPNZ                                                    ~49, ->59
   44    76    >   INIT_FCALL                                               'array_sum'
         77        SEND_VAR                                                 !7
         78        DO_ICALL                                         $50     
         79        IS_SMALLER                                               $50, !0
         80      > JMPZ                                                     ~51, ->85
   45    81    >   NEW                                              $52     'Exception'
         82        SEND_VAL_EX                                              'Position+pool+was+exhausted+before+filling+team'
         83        DO_FCALL                                      0          
         84      > THROW                                         0          $52
   48    85    >   VERIFY_RETURN_TYPE                                       !7
         86      > RETURN                                                   !7
   49    87*       VERIFY_RETURN_TYPE                                       
         88*     > RETURN                                                   null

End of function randomizeteam

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
172.62 ms | 1025 KiB | 18 Q