The ice cream Stack

In this blog post, we will investigate the use of a Stack data structure to store the different flavours of the different scoops of an ice cream!

A stack is a FILO data structure: First In Last Out and seems to be a suitable data structure for our ice cream, where the first scoop of ice-cream being added on the cone, will be the last one to be eaten!

With a Stack data structure you can either:

  • Push new data at the end of the stack.
  • Pop the last value of the stack.

ice-cream-stack

In order to implement our stack will use a 1D array of 5 values, assuming that you cannot make an ice cream with more than 5 scoops! We will use a pointer to store the index of the next empty cell of our array where data can be added.

Using OOP programming, we will create an IceCream class to implement our ice cream as a stack. We will encapsulate both the array data structure and the stack pointer as private properties of the IceCream class and we will use two public methods to push() and pop() scoops of different flavours to/from our IceCream!

Let’s look at the code of our class:

#A class to implement a Stack that can hold up to 5 values
class IceCream():
    #Constructor...
    def __init__(self):
        self.__array = [None,None,None,None,None] #__Private Property
        self.__pointer = 0  #__Private Property
        self.__maxCapacity = len(self.__array) #__Private Property

    #A method to push a new scoop/flavour to the ice cream
    def push(self, flavour):
        #Let's first check that we have not reached the maximum capacity (5 scoops max)
        if self.__pointer < self.__maxCapacity:
             self.__array[self.__pointer] = flavour
             self.__pointer = self.__pointer + 1
             print(flavour + " added!")
        else:
             print("Cannot add new favour! Ice cream is full!")

    #A method to pop the last scoop/flavour of the ice cream
    def pop(self):
        #Let's first check that the ice cream is not empty (has at least 1 scoop)
        if self.__pointer > 0:
             self.__pointer = self.__pointer - 1
             flavour = self.__array[self.__pointer]
             self.__array[self.__pointer] = ""
             return flavour
        else:
             print("Ice cream is empty!")
             return False

Now that we have an IceCream class, we can instantiate any ice cream object and construct each ice cream by pushing (and popping/eating) different flavours!

myIceCream = IceCream()

myIceCream.push("Strawberry")
myIceCream.push("Chocolate")
myIceCream.push("Pistachio")

flavour = myIceCream.pop()
print("This scoop of " + flavour + " was delicious!")

myIceCream.push("Blueberry")

Full Python Code

Let’s test this code… You can customise this code to create any ice cream based on your favourite flavours!!!

Did you like this challenge?

Click on a star to rate it!

Average rating 4.1 / 5. Vote count: 15

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!