Simulate a Stack With a List

Easy ⏱ 8 min 83% acceptance ★★★★★ 4.5
Write run_stack_ops(operations) where operations is a list of tuples like ("push", value) or ("pop",). Simulate a stack using a plain Python list (append = push, pop = pop from the end). Return the final list representing the stack's contents from bottom to top. Popping an empty stack should be a no-op (do not error).

Examples

Example 1
Input
operations = [('push', 1), ('push', 2), ('pop',), ('push', 3)]
Output
[1, 3]
Explanation

Push 1, push 2, pop removes 2, push 3 leaves [1, 3].

Constraints

  • 0 <= len(operations) <= 10^4

Topics

ListsStack

Companies

CiscoServiceNow

Hints

Hint 1

.append() pushes onto the end (the "top").

Hint 2

.pop() removes from the end; guard against popping an empty list.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.