3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getBillsCountInMoneyAmount(int $moneyAmount, int $bill): int { $divisionRemain = $moneyAmount % $bill; if ($moneyAmount === $divisionRemain) { return 0; } return ($moneyAmount - $divisionRemain) / $bill; } function getDisposableBillsCount(int $bill, int $billsCount, array $billsInStock): int { $countInStock = 0; if (array_key_exists($bill, $billsInStock)) { $countInStock = $billsInStock[$bill]; } return min($billsCount, $countInStock); } function dispenseMoney(int $requestedMoney, array $billsInStock): array { $remainedToDispenseMoney = $requestedMoney; $result = []; foreach($billsInStock as $bill => $countInStock) { $billsCount = getBillsCountInMoneyAmount($remainedToDispenseMoney, $bill); $disposableBillsCount = getDisposableBillsCount($bill, $billsCount, $billsInStock); $result[$bill] = $disposableBillsCount; $remainedToDispenseMoney -= $disposableBillsCount * $bill; } if (0 < $remainedToDispenseMoney) { throw new Exception('Недостаточно купюр'); } return $result; } $billsInStock = [5000 => 3, 2000 => 4, 1000 => 10, 500 => 30, 200 => 10, 100 => 30]; $requestedMoney = 11600; var_dump(dispenseMoney($requestedMoney, $billsInStock));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/O07Xf
function name:  (null)
number of ops:  10
compiled vars:  !0 = $billsInStock, !1 = $requestedMoney
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   ASSIGN                                                   !0, <array>
   46     1        ASSIGN                                                   !1, 11600
   48     2        INIT_FCALL                                               'var_dump'
          3        INIT_FCALL                                               'dispensemoney'
          4        SEND_VAR                                                 !1
          5        SEND_VAR                                                 !0
          6        DO_FCALL                                      0  $4      
          7        SEND_VAR                                                 $4
          8        DO_ICALL                                                 
          9      > RETURN                                                   1

Function getbillscountinmoneyamount:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/O07Xf
function name:  getBillsCountInMoneyAmount
number of ops:  13
compiled vars:  !0 = $moneyAmount, !1 = $bill, !2 = $divisionRemain
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        MOD                                              ~3      !0, !1
          3        ASSIGN                                                   !2, ~3
    7     4        IS_IDENTICAL                                             !0, !2
          5      > JMPZ                                                     ~5, ->7
    8     6    > > RETURN                                                   0
   11     7    >   SUB                                              ~6      !0, !2
          8        DIV                                              ~7      ~6, !1
          9        VERIFY_RETURN_TYPE                                       ~7
         10      > RETURN                                                   ~7
   12    11*       VERIFY_RETURN_TYPE                                       
         12*     > RETURN                                                   null

End of function getbillscountinmoneyamount

Function getdisposablebillscount:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/O07Xf
function name:  getDisposableBillsCount
number of ops:  16
compiled vars:  !0 = $bill, !1 = $billsCount, !2 = $billsInStock, !3 = $countInStock
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   16     3        ASSIGN                                                   !3, 0
   18     4        ARRAY_KEY_EXISTS                                         !0, !2
          5      > JMPZ                                                     ~5, ->8
   19     6    >   FETCH_DIM_R                                      ~6      !2, !0
          7        ASSIGN                                                   !3, ~6
   22     8    >   INIT_FCALL                                               'min'
          9        SEND_VAR                                                 !1
         10        SEND_VAR                                                 !3
         11        DO_ICALL                                         $8      
         12        VERIFY_RETURN_TYPE                                       $8
         13      > RETURN                                                   $8
   23    14*       VERIFY_RETURN_TYPE                                       
         15*     > RETURN                                                   null

End of function getdisposablebillscount

Function dispensemoney:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 23
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 23
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 30
Branch analysis from position: 26
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/O07Xf
function name:  dispenseMoney
number of ops:  34
compiled vars:  !0 = $requestedMoney, !1 = $billsInStock, !2 = $remainedToDispenseMoney, !3 = $result, !4 = $countInStock, !5 = $bill, !6 = $billsCount, !7 = $disposableBillsCount
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   27     2        ASSIGN                                                   !2, !0
   28     3        ASSIGN                                                   !3, <array>
   30     4      > FE_RESET_R                                       $10     !1, ->23
          5    > > FE_FETCH_R                                       ~11     $10, !4, ->23
          6    >   ASSIGN                                                   !5, ~11
   31     7        INIT_FCALL                                               'getbillscountinmoneyamount'
          8        SEND_VAR                                                 !2
          9        SEND_VAR                                                 !5
         10        DO_FCALL                                      0  $13     
         11        ASSIGN                                                   !6, $13
   32    12        INIT_FCALL                                               'getdisposablebillscount'
         13        SEND_VAR                                                 !5
         14        SEND_VAR                                                 !6
         15        SEND_VAR                                                 !1
         16        DO_FCALL                                      0  $15     
         17        ASSIGN                                                   !7, $15
   33    18        ASSIGN_DIM                                               !3, !5
         19        OP_DATA                                                  !7
   34    20        MUL                                              ~18     !7, !5
         21        ASSIGN_OP                                     2          !2, ~18
   30    22      > JMP                                                      ->5
         23    >   FE_FREE                                                  $10
   37    24        IS_SMALLER                                               0, !2
         25      > JMPZ                                                     ~20, ->30
   38    26    >   NEW                                              $21     'Exception'
         27        SEND_VAL_EX                                              '%D0%9D%D0%B5%D0%B4%D0%BE%D1%81%D1%82%D0%B0%D1%82%D0%BE%D1%87%D0%BD%D0%BE+%D0%BA%D1%83%D0%BF%D1%8E%D1%80'
         28        DO_FCALL                                      0          
         29      > THROW                                         0          $21
   41    30    >   VERIFY_RETURN_TYPE                                       !3
         31      > RETURN                                                   !3
   42    32*       VERIFY_RETURN_TYPE                                       
         33*     > RETURN                                                   null

End of function dispensemoney

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.3 ms | 1012 KiB | 18 Q