1
votes

I am learning python and have an assignment to better my understanding of "class" and using "stack."

The requirements are as follows:

-Define a class which implements stack for numerical values.

-Cannot use built-in pop function

-Function push should check if value is numerical

-Function print_stack should print values in stack, most recent (on top) first

-Function IsEmpty should return True if stack is empty, false otherwise

Here is my work so far:

class stack():
    def __init__(self):
        self.items = []

    def push(self, item):
        self.items.append(item)

    def isEmpty(self):
        return (self.items == [])  #can also use return not self i think?

    def print_stack(self):
        print self.items  

This is my very first class in programming so I'm sorry if my understanding is poor. I'm not looking for anyone to outright write this for me. I really want to understand how to go about this and receive some pointers on what I need to do as well as what I am lacking in understanding if it is obvious.

My questions are as follows:

1) How can I test if I am pushing a numerical value? On first thought, could I use try/except?

2) What is the best way to go about creating a pop function without using the one that's built in? This is really giving me a hard time. From my understanding I need to write something that will retrieve the last item from the list and return it.

3) to test my code would I do something like this?

test = stack()
test.push(1)
test.print_stack()
test.pop()  #whenever I learn how to make a pop function
test.isEmpty
1

1 Answers

2
votes

How can I test if I am pushing a numerical value? On first thought, could I use try/except?

You don't need to test it. Just convert it to int or whatever datatype you are working on at that point. for e.g. if you are working with int

def push(self, item):
    self.items.append(int(item))

if you are working with float

def push(self, item):
    self.items.append(float(item))

if you are working with string

def push(self, item):
    self.items.append(str(item))


What is the best way to go about creating a pop function without using the one that's built in? This is really giving me a hard time. From my understanding I need to write something that will retrieve the last item from the list and return it.

There are multiple ways,

  1. use del

    def pop(self):
        n = self.items[0]
        del self.items[0]
        return n
    
  2. use slicing

    def pop(self):
        n = self.items[0]
        self.items = self.items[1:]
        return n
    

    note that this would return a subset of the original but not modify it.

  3. use popleft()

    def pop(self):
        return self.items.popleft()
    

One suggestion You can also use len() to check length of your list and use it to check whether stack is empty or not,

def isEmpty(self):
    return (len(self.items) == 0)

Note: before popping use your isEmpty() function to check if stack is empty.


to test my code would I do something like this ?

just try executing every function, If you think that there is a problem then print stack after every operation. you will see how exactly values are getting updated in the stack.