Ask Question
26 August, 22:59

In each plastic container of Pez, candy, the colors are stored in random order. Your little brother Phil likes only the yellow ones, so he painstakingly takes out all the candies one by one, eats the yellow ones, and keeps the others in order, so that the can return them to the container in exactly the same order as before-minus the yellow candies, of course. Write the algorithm to simulate this process. (You may use any of the stack operations defined in the Stack ADT, but may not assume any knowledge of how the stack is implemented.)

+5
Answers (1)
  1. 27 August, 01:19
    0
    The algorithm for the process is explained below

    Explanation:

    EatYelloCandy:

    initialise stack s

    container is stack with candy

    (this will work if container is list also just take first element out of list (which is top of stackif stack))

    (similarly while pushing just append to list), but naturally as per statement container is stack

    while container not empty

    candy = container. pop ()

    if candy = = yellow:

    eat candy

    else:

    s. push (candy)

    / / NOw all candy from container are removed and all yellow candy have been eaten

    while s is not empty:

    candy = s. pop ()

    container. push (candy)

    return container

    This algorithm works in O (n) time where n is total number of candies.

    This algorithm work correctly as it preserve the order of candies using stack which will have element in just reverse order of original container.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “In each plastic container of Pez, candy, the colors are stored in random order. Your little brother Phil likes only the yellow ones, so he ...” in 📙 Engineering if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers