Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

my pop( ) function does not delete the top element of a list, but rather a random one

I have created the following pop function:

def pop(arr):
    if not isempty(arr):
        topelement = arr[len(arr)-1]
        arr.remove(topelement)
        return topelement

And it worked correctly until I used it in order to reverse order of a stack of numbers and operators:

"3 6 2 + * 14 3 4 + + /" 

into

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"/ + + 4 3 14 * + 2 6 3". 

In first iteration of while loop shown below it pushed "/" operator to the auxiliary stack and deleted it from the top of entry, which is OK – but in the second iteration it deleted "+" sign from the middle of the entry instead of the plus sign with the highest index.

def evaluation(eq):
    entryunsplit = errpostfix(eq)
    entry = entryunsplit.split()
    lengthofentry = len(entry)
    stack = [] 
    
    while len(stack) != lengthofentry:
        push(stack,pop(entry))

Could someone please explain me why didn’t it delete the last element and how can I avoid that error?

I am putting the whole code below in case some other element turns out to be significant.

stack1 = []
max_size = 30

def push(arr,a):
    if len(arr) < max_size:
        arr.append(a)


def isempty(arr):
    if len(arr) == 0:
        return True
    else:
        return False

def top(arr):
    if not isempty(arr):
        return arr[len(arr)-1]

def pop(arr):
    if not isempty(arr):
        topelement = arr[len(arr)-1]
        arr.remove(topelement)
        return topelement
       
def errpostfix(eq):
    entry = eq.split()
    opstack = []
    exit = []
    priorities = { "+": 1, "-": 1, "*": 0, "/": 0 }
    for token in entry:
        if token == "(":
            push(opstack,token)
        elif token == "*" or token == "/" or token == "+" or token == "-":
            if top(opstack) == "*" or top(opstack) == "/" or top(opstack) == "+" or top(opstack) == "-": 

                while not isempty(opstack) and priorities[top(opstack)] >= priorities[token]:
                    push(exit,pop(opstack))
                    isempty(opstack)
            push(opstack,token)
        elif token == ")":
            while top(opstack) != "(":
                push(exit,pop(opstack))
            pop(opstack)
        else:
            push(exit,token)
        
    while not isempty(opstack):
        push(exit, pop(opstack))

    output = " ".join(exit)
    return output



def isop(ch):
    if ch == "+" or ch == "-" or ch == "*" or ch == "/":
        return True
    else:
        return False

def evaluation(eq):
    entryunsplit = "3 6 2 + * 14 3 4 + + /"
    entry = entryunsplit.split()
    lengthofentry = len(entry)
    stack = []
    
    while len(stack) != lengthofentry:
        push(stack,pop(entry))

>Solution :

The remove() operation on a list will delete the first occurrence of an item in that list, is not "random" (check the docs). If there are several repeated elements, the first one encountered will be the one that gets deleted. To delete the last element, simply use the built-in pop() method:

def pop(arr):
    if not isempty(arr):
        topelement = arr[-1]
        arr.pop()
        return topelement
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading