3v4l.org

run code in 300+ PHP versions simultaneously
<?php $candiesTests = [ [1, 1, 2, 3], // Test1 => 1,3 [1, 1, 2, 3, 4], // Test2 => 1,4 [1, 1, 2, 2, 3, 4, 5, 5], // Test3 => 1,2,5,5 [1, 1, 2, 2, 3, 4, 5, 5, 1, 1, 5], // Test4 => 1,1,1,2,5 ]; function distributeCandies(array $candies): array { $quota = ceil(count($candies) / 2); // Berit's share $adam = []; // fill first $berit = []; // the leftovers $candyCounts = array_count_values($candies); ksort($candyCounts); while ($candyCounts) { foreach ($candyCounts as $candy => &$count) { $adam[] = $candy; --$count; if (!$count) { unset($candyCounts[$candy]); } if (count($adam) == $quota) { break 2; } } } foreach ($candyCounts as $candy => $count) { array_push($berit, ...array_fill(0, $count, $candy)); } return [$adam, $berit]; } foreach ($candiesTests as $candies) { $distribution = distributeCandies($candies); echo 'Adams candies: ' . implode(', ', $distribution[0]) . PHP_EOL . 'Berits candies: ' . implode(', ', $distribution[1]) . PHP_EOL . '---' . PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 26
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 26
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
filename:       /in/YMD7q
function name:  (null)
number of ops:  28
compiled vars:  !0 = $candiesTests, !1 = $candies, !2 = $distribution
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   38     1      > FE_RESET_R                                       $4      !0, ->26
          2    > > FE_FETCH_R                                               $4, !1, ->26
   39     3    >   INIT_FCALL                                               'distributecandies'
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $5      
          6        ASSIGN                                                   !2, $5
   41     7        INIT_FCALL                                               'implode'
          8        SEND_VAL                                                 '%2C+'
          9        FETCH_DIM_R                                      ~7      !2, 0
         10        SEND_VAL                                                 ~7
         11        DO_ICALL                                         $8      
         12        CONCAT                                           ~9      'Adams+candies%3A++', $8
   42    13        CONCAT                                           ~10     ~9, '%0A'
   43    14        CONCAT                                           ~11     ~10, 'Berits+candies%3A+'
   44    15        INIT_FCALL                                               'implode'
         16        SEND_VAL                                                 '%2C+'
         17        FETCH_DIM_R                                      ~12     !2, 1
         18        SEND_VAL                                                 ~12
         19        DO_ICALL                                         $13     
         20        CONCAT                                           ~14     ~11, $13
   45    21        CONCAT                                           ~15     ~14, '%0A'
         22        CONCAT                                           ~16     ~15, '---'
         23        CONCAT                                           ~17     ~16, '%0A'
         24        ECHO                                                     ~17
   38    25      > JMP                                                      ->2
         26    >   FE_FREE                                                  $4
   46    27      > RETURN                                                   1

Function distributecandies:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 17
Branch analysis from position: 34
2 jumps found. (Code = 77) Position 1 = 35, Position 2 = 48
Branch analysis from position: 35
2 jumps found. (Code = 78) Position 1 = 36, Position 2 = 48
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
Branch analysis from position: 17
2 jumps found. (Code = 125) Position 1 = 18, Position 2 = 32
Branch analysis from position: 18
2 jumps found. (Code = 126) Position 1 = 19, Position 2 = 32
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 26
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 31
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 26
Branch analysis from position: 32
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 17
Branch analysis from position: 34
Branch analysis from position: 17
Branch analysis from position: 32
filename:       /in/YMD7q
function name:  distributeCandies
number of ops:  55
compiled vars:  !0 = $candies, !1 = $quota, !2 = $adam, !3 = $berit, !4 = $candyCounts, !5 = $count, !6 = $candy
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
   13     1        INIT_FCALL                                               'ceil'
          2        COUNT                                            ~7      !0
          3        DIV                                              ~8      ~7, 2
          4        SEND_VAL                                                 ~8
          5        DO_ICALL                                         $9      
          6        ASSIGN                                                   !1, $9
   15     7        ASSIGN                                                   !2, <array>
   16     8        ASSIGN                                                   !3, <array>
   18     9        INIT_FCALL                                               'array_count_values'
         10        SEND_VAR                                                 !0
         11        DO_ICALL                                         $13     
         12        ASSIGN                                                   !4, $13
   19    13        INIT_FCALL                                               'ksort'
         14        SEND_REF                                                 !4
         15        DO_ICALL                                                 
   20    16      > JMP                                                      ->33
   21    17    > > FE_RESET_RW                                      $16     !4, ->32
         18    > > FE_FETCH_RW                                      ~17     $16, !5, ->32
         19    >   ASSIGN                                                   !6, ~17
   22    20        ASSIGN_DIM                                               !2
         21        OP_DATA                                                  !6
   23    22        PRE_DEC                                                  !5
   24    23        BOOL_NOT                                         ~21     !5
         24      > JMPZ                                                     ~21, ->26
   25    25    >   UNSET_DIM                                                !4, !6
   27    26    >   COUNT                                            ~22     !2
         27        IS_EQUAL                                                 !1, ~22
         28      > JMPZ                                                     ~23, ->31
   28    29    >   FE_FREE                                                  $16
         30      > JMP                                                      ->34
   21    31    > > JMP                                                      ->18
         32    >   FE_FREE                                                  $16
   20    33    > > JMPNZ                                                    !4, ->17
   32    34    > > FE_RESET_R                                       $24     !4, ->48
         35    > > FE_FETCH_R                                       ~25     $24, !5, ->48
         36    >   ASSIGN                                                   !6, ~25
   33    37        INIT_FCALL                                               'array_push'
         38        SEND_REF                                                 !3
         39        INIT_FCALL                                               'array_fill'
         40        SEND_VAL                                                 0
         41        SEND_VAR                                                 !5
         42        SEND_VAR                                                 !6
         43        DO_ICALL                                         $27     
         44        SEND_UNPACK                                              $27
         45        CHECK_UNDEF_ARGS                                         
         46        DO_ICALL                                                 
   32    47      > JMP                                                      ->35
         48    >   FE_FREE                                                  $24
   35    49        INIT_ARRAY                                       ~29     !2
         50        ADD_ARRAY_ELEMENT                                ~29     !3
         51        VERIFY_RETURN_TYPE                                       ~29
         52      > RETURN                                                   ~29
   36    53*       VERIFY_RETURN_TYPE                                       
         54*     > RETURN                                                   null

End of function distributecandies

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.61 ms | 1030 KiB | 20 Q