Esempio n. 1
0
def infix_to_rpn(input_data):
    stack = Stack()
    temp = ""
    output = []
    dictionary = {'^': 3, '*': 2, '/': 2, '+': 1, '-': 1, '(': 0}
    for item in input_data:
        for char in item:
            if char in dictionary and char != '(':
                if stack.isEmpty():
                    stack.Push(char)
                elif dictionary.get(char) <= dictionary.get(stack.Top()):
                    temp += stack.Top()
                    stack.Pop()
                    stack.Push(char)
                elif dictionary.get(char) > dictionary.get(stack.Top()):
                    stack.Push(char)
            elif char == '(':
                stack.Push(char)
            elif char == ')':
                temp = clear_stack(stack, temp)
            elif char == '=':
                temp = clear_stack(stack, temp)
                output.append(temp)
                temp = ""
            else:
                temp += char
    return output
Esempio n. 2
0
 def test_Push_Pop_Stack(self):
     expectedResult = 3
     testStack = Stack()
     testStack.Push(1)
     testStack.Push(2)
     testStack.Push(3)
     testResult = testStack.Pop()
     self.assertEqual(testResult, expectedResult)
Esempio n. 3
0
def main():
    s = Stack()
    print('Empty?: %s' % s.IsEmpty())
    s.Push('Dazed and Confused')
    s.Push('I Can\'t Quit You Baby')
    s.Push('Wearing and Tearing')
    print(s.Peek())
    print(s.Peek())
    print('Size: %s' % s.Size())
    s.Pop()

    print('Size: %s' % s.Size())
Esempio n. 4
0
def delete_last_10():
    blogPosts = BlogPost.query.all()

    s = Stack()

    for post in blogPosts:
        s.Push(post)

    for _ in range(10):
        postToDel = s.Pop()
        db.session.delete(postToDel.data)
        db.session.commit()

    return jsonify({"message":
                    "successfully deleted the last 10 blog posts"}), 200
Esempio n. 5
0
#-----------------------------------------------------------#

from Stack import Stack
from Queue import Queue


#--------------------STACK METHODS--------------------#
testStack = Stack()
# .Push()
testStack.Push(5)
print(testStack.items)
testStack.Push(76)
print(testStack.items)

# .Pop()
print(testStack.Pop())

# .Contains()
print(testStack.Contains(5))
print(testStack.Contains(76))
print()


#--------------------QUEUE METHODS--------------------#
testQueue = Queue()
# .Append()
testQueue.Append("Dog")
testQueue.Append("Cat")
testQueue.Append("Turtle")
print(testQueue.items)
Esempio n. 6
0
from Stack import Stack

The_Stack = Stack()

The_Stack.Push('Hello')
The_Stack.Push('World')
The_Stack.Push('Hady')

print(The_Stack.Top())

print(The_Stack.Pop())
print(The_Stack.Pop())
print(The_Stack.Pop())
Esempio n. 7
0
from Stack import Stack

myStack = Stack()

while True:
    print('push <value>')
    print('pop')
    print('quit')
    do = input('What would you like to do?').split()

    operation = do[0].strip().lower()
    if operation == 'push':
        myStack.Push(int(do[1]))
    elif operation == 'pop':
        popped = myStack.Pop()
        if popped is None:
            print('Stack is empty.')
        else:
            print('Popped value: ', int(popped))
    elif operation == 'quit':
        break
Esempio n. 8
0
choice = "Yes"

while choice:
    print(a)
    b = int(input('Enter your choice: '))
    if b == 1:
        c = int(input('Enter the element you want to push: '))
        stack.Push(c)
        print(stack.display())

    elif b == 2:
        if stack.Empty():
            print('Stack is empty')
        else:
            print('Popped element {} from the stack {}'.format(
                stack.Pop(), stack.display()))

    elif b == 3:
        print("The top element in the stack is {}", stack.Top())

    elif b == 4:
        if not stack.Empty():
            print('Stack is not empty')
        else:
            print('Stack is Empty')

    elif b == 5:
        print('Maximum element from the stack is {}', stack.max())
    else:
        print('Invalid choice')
    choice = input('Do you want to continue? Yes/No')