3v4l.org

run code in 300+ PHP versions simultaneously
<?php function sumCarries(array $array) { $columns = ['carries' => []]; // prepare matrix of 1-digit integers in columns -- ones, tens, hundreds, etc foreach ($array as $integer) { $columns[] = str_split(strrev($integer)); } // sum column values in ascending order and populate carry values // subsequent column sums need to include carried value for ($i = 0, $len = strlen(max($array)); $i < $len; ++$i) { $columns['carries'][$i + 1] = (int)(array_sum(array_column($columns, $i)) / 10); } // sum all populated carry values return array_sum($columns['carries']); } $tests = [ [123, 456], // no carries in any column [555, 555], // 1 ones, 1 tens, 1 hundreds [123, 594], // 1 tens [123, 123, 804], // 1 ones, 1 hundreds [99, 9, 99, 99, 99], // 4 ones, 4 hundreds [9,9,9,9,9,9,9,9,9,9,9,9], // 10 ones ]; var_export(array_map('sumCarries', $tests));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5X6hr
function name:  (null)
number of ops:  9
compiled vars:  !0 = $tests
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   ASSIGN                                                   !0, <array>
   30     1        INIT_FCALL                                               'var_export'
          2        INIT_FCALL                                               'array_map'
          3        SEND_VAL                                                 'sumCarries'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                                 
          8      > RETURN                                                   1

Function sumcarries:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 13
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 13
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 37, Position 2 = 21
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 37, Position 2 = 21
Branch analysis from position: 37
Branch analysis from position: 21
Branch analysis from position: 13
filename:       /in/5X6hr
function name:  sumCarries
number of ops:  43
compiled vars:  !0 = $array, !1 = $columns, !2 = $integer, !3 = $i, !4 = $len
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        ASSIGN                                                   !1, <array>
    7     2      > FE_RESET_R                                       $6      !0, ->13
          3    > > FE_FETCH_R                                               $6, !2, ->13
    8     4    >   INIT_FCALL                                               'str_split'
          5        INIT_FCALL                                               'strrev'
          6        SEND_VAR                                                 !2
          7        DO_ICALL                                         $8      
          8        SEND_VAR                                                 $8
          9        DO_ICALL                                         $9      
         10        ASSIGN_DIM                                               !1
         11        OP_DATA                                                  $9
    7    12      > JMP                                                      ->3
         13    >   FE_FREE                                                  $6
   13    14        ASSIGN                                                   !3, 0
         15        INIT_FCALL                                               'max'
         16        SEND_VAR                                                 !0
         17        DO_ICALL                                         $11     
         18        STRLEN                                           ~12     $11
         19        ASSIGN                                                   !4, ~12
         20      > JMP                                                      ->35
   14    21    >   ADD                                              ~15     !3, 1
         22        INIT_FCALL                                               'array_sum'
         23        INIT_FCALL                                               'array_column'
         24        SEND_VAR                                                 !1
         25        SEND_VAR                                                 !3
         26        DO_ICALL                                         $17     
         27        SEND_VAR                                                 $17
         28        DO_ICALL                                         $18     
         29        DIV                                              ~19     $18, 10
         30        CAST                                          4  ~20     ~19
         31        FETCH_DIM_W                                      $14     !1, 'carries'
         32        ASSIGN_DIM                                               $14, ~15
         33        OP_DATA                                                  ~20
   13    34        PRE_INC                                                  !3
         35    >   IS_SMALLER                                               !3, !4
         36      > JMPNZ                                                    ~22, ->21
   18    37    >   INIT_FCALL                                               'array_sum'
         38        FETCH_DIM_R                                      ~23     !1, 'carries'
         39        SEND_VAL                                                 ~23
         40        DO_ICALL                                         $24     
         41      > RETURN                                                   $24
   19    42*     > RETURN                                                   null

End of function sumcarries

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.29 ms | 1022 KiB | 20 Q