3v4l.org

run code in 300+ PHP versions simultaneously
<?php function seqCheck(array $arr, int $maxMis): bool { $oos = 0; $mis = 0; foreach ($arr as $k => $v) { if ($k > 0 && $v < $arr[$k - 1]) { $oos++; // number out of sequence } if ($k > 0 && $v !== $arr[$k - 1] + 1) { $mis++; // number missing in sequence } } if ($oos > 0 || $mis > $maxMis) { return false; } else { return true; } } $tests = [ [[2, 3, 5, 6], 0], [[2, 3, 5, 6], 1], [[2, 5, 6, 8], 1], [[3, 6, 10, 16], 1], [[5, 6, 7, 8, 9, 10, 11], 1], [[1, 3, 5], 2], [[1, 5, 3], 2], ]; foreach ($tests as [$array, $timesForgivable]) { printf( "Test: %-17s,\tMax Forgiven: %d,\tOutcome: %s\n", json_encode($array), $timesForgivable, json_encode(seqCheck($array, $timesForgivable)) ); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 25
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 25
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
filename:       /in/0qL8h
function name:  (null)
number of ops:  27
compiled vars:  !0 = $tests, !1 = $array, !2 = $timesForgivable
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   ASSIGN                                                   !0, <array>
   31     1      > FE_RESET_R                                       $4      !0, ->25
          2    > > FE_FETCH_R                                               $4, $5, ->25
          3    >   FETCH_LIST_R                                     $6      $5, 0
          4        ASSIGN                                                   !1, $6
          5        FETCH_LIST_R                                     $8      $5, 1
          6        ASSIGN                                                   !2, $8
          7        FREE                                                     $5
   32     8        INIT_FCALL                                               'printf'
   33     9        SEND_VAL                                                 'Test%3A+%25-17s%2C%09Max+Forgiven%3A+%25d%2C%09Outcome%3A+%25s%0A'
   34    10        INIT_FCALL                                               'json_encode'
         11        SEND_VAR                                                 !1
         12        DO_ICALL                                         $10     
         13        SEND_VAR                                                 $10
   35    14        SEND_VAR                                                 !2
   36    15        INIT_FCALL                                               'json_encode'
         16        INIT_FCALL                                               'seqcheck'
         17        SEND_VAR                                                 !1
         18        SEND_VAR                                                 !2
         19        DO_FCALL                                      0  $11     
         20        SEND_VAR                                                 $11
         21        DO_ICALL                                         $12     
         22        SEND_VAR                                                 $12
   32    23        DO_ICALL                                                 
   31    24      > JMP                                                      ->2
         25    >   FE_FREE                                                  $4
   38    26      > RETURN                                                   1

Function seqcheck:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 25
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 25
Branch analysis from position: 6
2 jumps found. (Code = 46) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
2 jumps found. (Code = 46) Position 1 = 17, Position 2 = 22
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 24
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 24
Branch analysis from position: 22
Branch analysis from position: 15
Branch analysis from position: 13
Branch analysis from position: 25
2 jumps found. (Code = 47) Position 1 = 28, Position 2 = 30
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 33
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
Branch analysis from position: 25
filename:       /in/0qL8h
function name:  seqCheck
number of ops:  36
compiled vars:  !0 = $arr, !1 = $maxMis, !2 = $oos, !3 = $mis, !4 = $v, !5 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        ASSIGN                                                   !2, 0
    6     3        ASSIGN                                                   !3, 0
    7     4      > FE_RESET_R                                       $8      !0, ->25
          5    > > FE_FETCH_R                                       ~9      $8, !4, ->25
          6    >   ASSIGN                                                   !5, ~9
    8     7        IS_SMALLER                                       ~11     0, !5
          8      > JMPZ_EX                                          ~11     ~11, ->13
          9    >   SUB                                              ~12     !5, 1
         10        FETCH_DIM_R                                      ~13     !0, ~12
         11        IS_SMALLER                                       ~14     !4, ~13
         12        BOOL                                             ~11     ~14
         13    > > JMPZ                                                     ~11, ->15
    9    14    >   PRE_INC                                                  !2
   11    15    >   IS_SMALLER                                       ~16     0, !5
         16      > JMPZ_EX                                          ~16     ~16, ->22
         17    >   SUB                                              ~17     !5, 1
         18        FETCH_DIM_R                                      ~18     !0, ~17
         19        ADD                                              ~19     ~18, 1
         20        IS_NOT_IDENTICAL                                 ~20     !4, ~19
         21        BOOL                                             ~16     ~20
         22    > > JMPZ                                                     ~16, ->24
   12    23    >   PRE_INC                                                  !3
    7    24    > > JMP                                                      ->5
         25    >   FE_FREE                                                  $8
   15    26        IS_SMALLER                                       ~22     0, !2
         27      > JMPNZ_EX                                         ~22     ~22, ->30
         28    >   IS_SMALLER                                       ~23     !1, !3
         29        BOOL                                             ~22     ~23
         30    > > JMPZ                                                     ~22, ->33
   16    31    > > RETURN                                                   <false>
   15    32*       JMP                                                      ->34
   18    33    > > RETURN                                                   <true>
   20    34*       VERIFY_RETURN_TYPE                                       
         35*     > RETURN                                                   null

End of function seqcheck

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.82 ms | 1006 KiB | 16 Q