Friday Puzzler: ADT - Can you stand the excitement?

After last week’s Fish Killing puzzle, I thought it was time to take it down a notch and try something a bit less exciting (and harmful). Today’s puzzle will have you build an Abstract Data Type, basically, some code that will act like a built in data type (think struct, array, etc), but with distinctive behavior.

Build a simple CFC that handles an array of data. The CFC will have these methods:

Push - This adds a new item to the array.
Pull - This gets (and removes) an item from the array.
Length - Can you guess?
IsEmpty - Can you guess?

Here is the catch: The Pull method has to follow a special rule when getting the data. Your CFC should have an Init() method that takes one argument. The argument is a string that determines the Pull behavior. You can pass in FIFO, FILO, or RANDOM.

If you set the CFC to FIFO mode, the Pull method will return the item that was first entered in the array. Your code should remove the item from the array so that the next Pull also gets the first item.

FILO mode will return the last item entered in the array.

And lastly, RANDOM will return a random item.

Nice and simple and not terribly useless. You may actually have a need to store data and determine at runtime if FIFO or FILO should be used. What is cool is that your code outside the CFC need not know. It just pushes and pulls.

Enjoy!

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments