- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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))

["5", "2", "POP", "DUP", "3", "+", "15", "-"]

7

- Related Questions & Answers
- 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
- Program to construct Maximum Stack with given operations in C++
- Program to find maximum sum by performing at most k negate operations in Python
- Python Program to Create a class performing Calculator Operations
- Program to find final text in an editor by performing typing and backspacing in Python
- Program to make all elements equal by performing given operation in Python
- Program to find maximum score from performing multiplication operations 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
- Data Structures Stack Primitive Operations
- 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

Advertisements