Stack Class

Lab Python Intermediate 20 points

Implement a Stack with push, pop (returns None when empty), peek and __len__. Process commands push X, pop, peek, size; print the result of each pop/peek/size.

Input

Line 1: n. Then n commands.

Output

One line per pop/peek/size.

Example

Input

6
push a
push b
peek
pop
size
pop

Output

b
b
1
a
Sample input available Sample output available
Editor
Python

Hints

Use a list internally: append to push, pop() to pop.

__len__ makes len(stack) work.

Solution explanation

Wrapping a list in a class exposes only the operations a stack should allow.

Show solution
class Stack:
    def __init__(self):
        self._items = []

    def push(self, item):
        self._items.append(item)

    def pop(self):
        return self._items.pop() if self._items else None

    def peek(self):
        return self._items[-1] if self._items else None

    def __len__(self):
        return len(self._items)


stack = Stack()
for _ in range(int(input())):
    parts = input().split()
    if parts[0] == "push":
        stack.push(parts[1])
    elif parts[0] == "pop":
        print(stack.pop())
    elif parts[0] == "peek":
        print(stack.peek())
    else:
        print(len(stack))
Output
Answer
HOME LEARN COMMUNITY DASHBOARD