Count of packets placed in each box after performing given operations | Function to print final array after performing all the operations ; Initialize variables ; Traverse through all operations ; Operation Type ; Move left ; Move right ; Pick a packet ; Drop a packet ; Exit ; Print final array ; Driver Code ; Given capacity ; Given array with initial values ; Array size ; Operations ; Number of operations ; Function call
def printFinalArray ( a , n , operations , p , capacity ) : NEW_LINE INDENT curr = 0 NEW_LINE picked = False NEW_LINE for i in range ( p ) : NEW_LINE INDENT s = operations [ i ] NEW_LINE flag = False NEW_LINE if ( curr != 0 ) : NEW_LINE INDENT curr -= 1 NEW_LINE break NEW_LINE DEDENT if ( curr != n - 1 ) : NEW_LINE INDENT curr += 1 NEW_LINE break NEW_LINE DEDENT if ( picked == False and a [ curr ] != 0 ) : NEW_LINE INDENT picked = True NEW_LINE a [ curr ] -= 1 NEW_LINE break NEW_LINE DEDENT if ( picked == True and a [ curr ] != capacity ) : NEW_LINE INDENT picked = False NEW_LINE a [ curr ] += 1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT flag = True NEW_LINE DEDENT if ( flag == True ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT capacity = 5 NEW_LINE a = [ 2 , 5 , 2 ] NEW_LINE N = len ( a ) NEW_LINE operations = [ 3 , 2 , 4 , 1 , 4 , 5 ] NEW_LINE M = len ( operations ) NEW_LINE printFinalArray ( a , N , operations , M , capacity ) NEW_LINE DEDENT