0% found this document useful (0 votes)
8 views3 pages

Day 30 Python Answers - 60515687 - 2025 - 05 - 13 - 08 - 53

The document covers data structures in Python, specifically focusing on stacks and their operations. It includes two programming tasks: evaluating a postfix expression and implementing a Min Stack that supports various operations. Both tasks are accompanied by example code and expected outputs.

Uploaded by

shalugarg8679
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Day 30 Python Answers - 60515687 - 2025 - 05 - 13 - 08 - 53

The document covers data structures in Python, specifically focusing on stacks and their operations. It includes two programming tasks: evaluating a postfix expression and implementing a Min Stack that supports various operations. Both tasks are accompanied by example code and expected outputs.

Uploaded by

shalugarg8679
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

HPSC PGT(CS)- Python Subjective Series

Day 30
Syllabus Coverage

Data Structures in Python

- Stack: operations (`push`, `pop`), implementation using lists.

Programs of the day:


1. Evaluate Postfix Expression using Python 10 M
Write a python program with a function evaluate_postfix(expr: str) ->
int that evaluates a postfix expression (Reverse Polish Notation) using a
stack. Assume valid input with integers and operators (+, -, *, /).
Example:
evaluate_postfix("23+5*") → 25 # (2+3)*5

2. Implement a Min Stack using Python 10 M


Design a stack in python that supports push, pop, top,
and get_min (returns the minimum element in the stack) in constant
time.
Example:
stack = MinStack()
stack.push(-2)
stack.push(0)
stack.push(-3)
stack.get_min() → -3
stack.pop()
stack.get_min() → -2
1. Evaluate Postfix Expression

def evaluate_postfix(expr: str) -> int:

stack = []

for char in expr:

if char.isdigit():

stack.append(int(char))

else:

b = stack.pop()

a = stack.pop()

if char == '+':

stack.append(a + b)

elif char == '-':

stack.append(a - b)

elif char == '*':

stack.append(a * b)

elif char == '/':

stack.append(a // b)

return stack.pop()

print(evaluate_postfix("23+5*")) # Output: 25

Output:

25

2. Implement a Min Stack

stack = []

min_stack = []

def push(val: int) -> None:

stack.append(val)

if not min_stack or val <= min_stack[-1]:

min_stack.append(val)
def pop() -> None:

if stack.pop() == min_stack[-1]:

min_stack.pop()

def top() -> int:

return stack[-1]

def get_min() -> int:

return min_stack[-1]

# Test

push(-2)

push(0)

push(-3)

print(get_min()) # Output: -3

pop()

print(get_min()) # Output: -2

Output:

-3

-2

You might also like