3v4l.org

run code in 300+ PHP versions simultaneously
<?php $pool = [ 1703 => 15916.19738, 5129 => 11799.15419, 33 => 11173.49945, 1914 => 8439.45987, 2291 => 6284.22271, 5134 => 5963.14065, 5509 => 5169.85755, 4355 => 5153.80867, 2078 => 3932.79341, 31 => 3924.09928, 5433 => 2718.7711, 3172 => 2146.1932, 1896 => 2141.36021, 759 => 1453.5501, 2045 => 1320.74681, 5873 => 1222.7448, 2044 => 1194.4903, 6479 => 1074.1714, 5299 => 950.872, 3315 => 878.06602, 6193 => 847.3372, 1874 => 813.816, 1482 => 330.6422, 6395 => 312.1545, 6265 => 165.9224, 6311 => 122.8785, 6288 => 26.5426, ]; function balanceTeams(array $pool, int $maxTrades = 10): array { arsort($pool); $teams = [[], []]; $i = 1; foreach ($pool as $k => $v) { $teams[$i = 1 - $i][$k] = $v; } $getDiff = fn($a, $b) => array_sum($a) - array_sum($b); for ($i = 0; $i < $maxTrades; ++$i) { $diff = $getDiff(...$teams); //echo "Diff: $diff\n"; if (!$diff) { break; } foreach ($teams[0] as $idA => $valA) { foreach ($teams[1] as $idB => $valB) { if (abs($valA - $valB) < abs($diff)) { $teams[0][$idB] = $valB; $teams[1][$idA] = $valA; unset($teams[0][$idA], $teams[1][$idB]); arsort($teams[0]); arsort($teams[1]); //printf("Trade %d: %s for %s\n", $i + 1, $idA, $idB); continue 3; } } } break; } return $teams; } $teams = balanceTeams($pool); var_export($teams); printf( "Summary:\nAverage: %f\nTeam1 Sum: %f\nTeam2 Sum: %f", array_sum($pool) / 2, array_sum($teams[0]), array_sum($teams[1]) );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8YRRt
function name:  (null)
number of ops:  27
compiled vars:  !0 = $pool, !1 = $teams
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   70     1        INIT_FCALL                                               'balanceteams'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $3      
          4        ASSIGN                                                   !1, $3
   71     5        INIT_FCALL                                               'var_export'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                                 
   73     8        INIT_FCALL                                               'printf'
   74     9        SEND_VAL                                                 'Summary%3A%0AAverage%3A+%25f%0ATeam1+Sum%3A+%25f%0ATeam2+Sum%3A+%25f'
   75    10        INIT_FCALL                                               'array_sum'
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                         $6      
         13        DIV                                              ~7      $6, 2
         14        SEND_VAL                                                 ~7
   76    15        INIT_FCALL                                               'array_sum'
         16        FETCH_DIM_R                                      ~8      !1, 0
         17        SEND_VAL                                                 ~8
         18        DO_ICALL                                         $9      
         19        SEND_VAR                                                 $9
   77    20        INIT_FCALL                                               'array_sum'
         21        FETCH_DIM_R                                      ~10     !1, 1
         22        SEND_VAL                                                 ~10
         23        DO_ICALL                                         $11     
         24        SEND_VAR                                                 $11
   73    25        DO_ICALL                                                 
   78    26      > RETURN                                                   1

Function balanceteams:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 16
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 16
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
2 jumps found. (Code = 44) Position 1 = 75, Position 2 = 21
Branch analysis from position: 75
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 29
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
Branch analysis from position: 29
2 jumps found. (Code = 77) Position 1 = 31, Position 2 = 70
Branch analysis from position: 31
2 jumps found. (Code = 78) Position 1 = 32, Position 2 = 70
Branch analysis from position: 32
2 jumps found. (Code = 77) Position 1 = 35, Position 2 = 68
Branch analysis from position: 35
2 jumps found. (Code = 78) Position 1 = 36, Position 2 = 68
Branch analysis from position: 36
2 jumps found. (Code = 43) Position 1 = 46, Position 2 = 67
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 72
Branch analysis from position: 72
2 jumps found. (Code = 44) Position 1 = 75, Position 2 = 21
Branch analysis from position: 75
Branch analysis from position: 21
Branch analysis from position: 67
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
Branch analysis from position: 68
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
Branch analysis from position: 68
Branch analysis from position: 70
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
Branch analysis from position: 70
Branch analysis from position: 16
filename:       /in/8YRRt
function name:  balanceTeams
number of ops:  79
compiled vars:  !0 = $pool, !1 = $maxTrades, !2 = $teams, !3 = $i, !4 = $v, !5 = $k, !6 = $getDiff, !7 = $diff, !8 = $valA, !9 = $idA, !10 = $valB, !11 = $idB
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      10
   35     2        INIT_FCALL                                               'arsort'
          3        SEND_REF                                                 !0
          4        DO_ICALL                                                 
   36     5        ASSIGN                                                   !2, <array>
   37     6        ASSIGN                                                   !3, 1
   38     7      > FE_RESET_R                                       $15     !0, ->16
          8    > > FE_FETCH_R                                       ~16     $15, !4, ->16
          9    >   ASSIGN                                                   !5, ~16
   39    10        SUB                                              ~18     1, !3
         11        ASSIGN                                           ~19     !3, ~18
         12        FETCH_DIM_W                                      $20     !2, ~19
         13        ASSIGN_DIM                                               $20, !5
         14        OP_DATA                                                  !4
   38    15      > JMP                                                      ->8
         16    >   FE_FREE                                                  $15
   42    17        DECLARE_LAMBDA_FUNCTION                          ~22     [0]
         18        ASSIGN                                                   !6, ~22
   44    19        ASSIGN                                                   !3, 0
         20      > JMP                                                      ->73
   45    21    >   INIT_DYNAMIC_CALL                                        !6
         22        SEND_UNPACK                                              !2
         23        CHECK_UNDEF_ARGS                                         
         24        DO_FCALL                                      1  $25     
         25        ASSIGN                                                   !7, $25
   47    26        BOOL_NOT                                         ~27     !7
         27      > JMPZ                                                     ~27, ->29
   48    28    > > JMP                                                      ->75
   51    29    >   FETCH_DIM_R                                      ~28     !2, 0
         30      > FE_RESET_R                                       $29     ~28, ->70
         31    > > FE_FETCH_R                                       ~30     $29, !8, ->70
         32    >   ASSIGN                                                   !9, ~30
   52    33        FETCH_DIM_R                                      ~32     !2, 1
         34      > FE_RESET_R                                       $33     ~32, ->68
         35    > > FE_FETCH_R                                       ~34     $33, !10, ->68
         36    >   ASSIGN                                                   !11, ~34
   53    37        INIT_FCALL                                               'abs'
         38        SUB                                              ~36     !8, !10
         39        SEND_VAL                                                 ~36
         40        DO_ICALL                                         $37     
         41        INIT_FCALL                                               'abs'
         42        SEND_VAR                                                 !7
         43        DO_ICALL                                         $38     
         44        IS_SMALLER                                               $37, $38
         45      > JMPZ                                                     ~39, ->67
   54    46    >   FETCH_DIM_W                                      $40     !2, 0
         47        ASSIGN_DIM                                               $40, !11
         48        OP_DATA                                                  !10
   55    49        FETCH_DIM_W                                      $42     !2, 1
         50        ASSIGN_DIM                                               $42, !9
         51        OP_DATA                                                  !8
   56    52        FETCH_DIM_UNSET                                  $44     !2, 0
         53        UNSET_DIM                                                $44, !9
         54        FETCH_DIM_UNSET                                  $45     !2, 1
         55        UNSET_DIM                                                $45, !11
   57    56        INIT_FCALL                                               'arsort'
         57        FETCH_DIM_W                                      $46     !2, 0
         58        SEND_REF                                                 $46
         59        DO_ICALL                                                 
   58    60        INIT_FCALL                                               'arsort'
         61        FETCH_DIM_W                                      $48     !2, 1
         62        SEND_REF                                                 $48
         63        DO_ICALL                                                 
   60    64        FE_FREE                                                  $33
         65        FE_FREE                                                  $29
         66      > JMP                                                      ->72
   52    67    > > JMP                                                      ->35
         68    >   FE_FREE                                                  $33
   51    69      > JMP                                                      ->31
         70    >   FE_FREE                                                  $29
   64    71      > JMP                                                      ->75
   44    72    >   PRE_INC                                                  !3
         73    >   IS_SMALLER                                               !3, !1
         74      > JMPNZ                                                    ~51, ->21
   67    75    >   VERIFY_RETURN_TYPE                                       !2
         76      > RETURN                                                   !2
   68    77*       VERIFY_RETURN_TYPE                                       
         78*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8YRRt
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        INIT_FCALL                                               'array_sum'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $2      
          5        INIT_FCALL                                               'array_sum'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $3      
          8        SUB                                              ~4      $2, $3
          9      > RETURN                                                   ~4
         10*     > RETURN                                                   null

End of Dynamic Function 0

End of function balanceteams

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.04 ms | 1017 KiB | 19 Q