
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to check final answer by performing given stack operations in Python
Suppose we have a list of string called ops where each element is any one of these operations like below −
- A non-negative integer value that will be pushed into a stack
- "POP" to delete top most element from the stack
- "DUP" to insert top element again into the stack, to make it duplicate
- "+" to pop out the top two elements and push the sum value
- "-" to pop out the top two elements and push the result of (top element - element just below top)
So we have to find the top mot element in the stack after applying all of these operations. If some operations are not valid then return -1.
So, if the input is like ops = ["5", "2", "POP", "DUP", "3", "+", "15", "-"], then the output will be 7 because initially using first two operations, insert 5 and 2 so stack is like [5, 2], then pop one so current stack is like [5]. After that for DUP, the 5 will be duplicated, so stack is like [5, 5], then add 3 [5, 5, 3], then for addition operation it will be [5, 8], then insert 15, so [5, 8, 15] after that for subtract operation the stack will be [5, (15-8)] = [5, 7]. So top most element is 7.
To solve this, we will follow these steps −
- stack := a new stack
- for each i in ops, do
- if i is a number, then
- push i into stack
- otherwise when size of stack >= 1 and i is "POP", then
- pop top element from stack
- otherwise when size of stack >= 1 and i is same as "DUP", then
- pop top-most element from stack into p
- and insert p twice
- otherwise when size of stack >= 2 and i is same as "+", then
- pop top-most element from stack into a
- pop top-most element from stack into b
- push (a + b) into stack
- otherwise when size of stack >= 2 and i is same as "-", then
- pop top-most element from stack into a
- pop top-most element from stack into b
- push (a - b) into stack
- otherwise,
- return -1
- if i is a number, then
- return top element from stack
Example
Let us see the following implementation to get better understanding −
def solve(ops): stack = [] for i in ops: if i.isnumeric() == True: stack.append(int(i)) elif len(stack) >= 1 and i == "POP": stack.pop() elif len(stack) >= 1 and i == "DUP": p = stack.pop() stack.append(p) stack.append(p) elif len(stack) >= 2 and i == "+": a = stack.pop() b = stack.pop() stack.append(a + b) elif len(stack) >= 2 and i == "-": a = stack.pop() b = stack.pop() stack.append(a - b) else: return -1 return stack.pop() ops = ["5", "2", "POP", "DUP", "3", "+", "15", "-"] print(solve(ops))
Input
["5", "2", "POP", "DUP", "3", "+", "15", "-"]
Output
7
- Related Articles
- Final string after performing given operations in C++
- Program to find expected sum of subarrays of a given array by performing some operations in Python
- Reduce a number to 1 by performing given operations in C++
- Program to find final text in an editor by performing typing and backspacing in Python
- Program to find maximum sum by performing at most k negate operations in Python
- Program to construct Maximum Stack with given operations in C++
- Python Program to Create a class performing Calculator Operations
- Program to find maximum score from performing multiplication operations in Python
- Program to make all elements equal by performing given operation in Python
- Maximum Possible Product in Array after performing given Operations in C++
- Python Program to Check String is Palindrome using Stack
- C# Program to Implement Stack with Push and Pop operations
- Performing Bitwise Operations with BigInteger in Java
- Maximum count of equal numbers in an array after performing given operations in C++
- Program to find number of given operations required to reach Target in Python
