CBSE Python Data Structures Exercises
- Categories Python, Data Structures
CBSE Python Data Structures Exercises:
1.Evaluate the following postfix notation of expression :
20, 10, +, 5, 2, * , – ,10, /
Ans.
Scanned Elements | Operation | Stack Status |
---|---|---|
20 | Push | 20 |
10 | Push | 20, 10 |
+ | Pop twice 10+20= 30 Push | 30 |
5 | Push | 30, 5 |
2 | Push | 30, 5, 2 |
* | Pop twice 5*2= 10 Push | 20 |
– | Pop twice 30-10= 20 Push | 20 |
10 | Push | 20, 10 |
/ | Pop twice 20/10= 2 Push | 2 |
2.Convert the following infix expression to its equivalent postfix expression. Show stack contents for the conversion(A+B*(C-D) /E).
Ans.
Let us rewrite like(A+B*(C-D) / E)
Scanned Elements | Operation | Stack Status |
---|---|---|
( | ( | |
A | ( | A |
+ | ( + | A |
B | ( + | AB |
* | ( + * | AB |
( | ( + * ( | AB |
C | ( + * ( | ABC |
– | ( + * ( – | ABC |
D | ( + * ( – | ABCD |
) | ( + * | ABCD – |
/ | ( + / | ABCD – * |
E | ( + / | ABCD – * E |
) | ABCD – * E/+ |
3.Write a program to perform insert and display a Queue containing Members details as given in the following definition of each item:
MemberName String
Age integer
Ans.
def INSERTQ(Arr):
MemberName=input("enter data to be inserted: ")
age=int(input("enter data to be inserted: "))
data = [MemberName, age]
Arr.append(data)
def Display(Arr):
if Arr==[]:
print("Queue Empty!")
else:
front= 0
rear=len(Arr)-1
for a in range(1,rear):
print (Arr[a])
4.Write a program to implement a stack. Implement only push and display operations. The push function takes in stack and item to be pushed. Display takes in only stack.
Ans.
def Push(stack, item):
stack.append(item)
def Display(stack) :
if isEmpty(stack):
print("Stack empty")
else:
top=len(stack)-1
for a in range(top-1,-1,-1):
print(stack[a])
5.Change the following infix expression into postfix expression (A+B)*C + D/E –F
Ans. Let us rewrite like(( A+B)*C+D/E-F)
Scanned Elements | Operation | Stack Status |
---|---|---|
( | ( | |
( | ( ( | |
A | ( ( | A |
+ | ( ( + | A |
B | ( ( + | AB |
) | ( | AB+ |
* | ( * | AB+ |
C | ( * | AB+C |
+ | ( + | AB+C* |
D | ( + | AB+C*D |
/ | ( + / | AB+C*D |
E | ( + / | AB+C*DE |
– | ( – | AB+C*DE/+ |
F | ( – | AB+C*DE/+F |
) | AB+C*DE/+F- |
Ans. Output AB+C*DE/+F-
6.Write a program to delete an element from a sorted linear list.
Ans.
def Bsearch(AR,ITEM):
beg = 0
last = len(AR) - 1
while(beg <= last):
mid=(beg + last)//2
if(ITEM == AR[mid]):
return mid
elif(ITEM > AR[mid]):
beg = mid + 1
else:
last = mid - 1
else:
return False
myList=[10,20,30,40,50,60,70]
ITEM=int(input("Enter element to be deleted:"))
position=Bsearch(myList,ITEM)
if position:
del myList[position]
print("The list after deleting",ITEM,"is")
print(myList)
else:
print("Element not in the list")
7.Obtain the postfix notation for the following infix notation of expression showing the contents of the stack and postfix expression formed after each step of conversion. A*B+(C-D/F)
Ans.
Let us rewrite like (A*B +(C-D/F))
Scanned Elements | Operation | Stack Status |
---|---|---|
( | ( | |
A | ( | A |
* | ( * | A |
B | ( * | AB |
+ | ( * | AB* |
( | ( + ( | AB* |
C | ( + ( | AB*C |
– | ( + ( – | AB*C |
D | ( + ( – | AB*CD |
/ | ( + ( – / | AB*CD |
F | ( + ( – / | AB*CDF |
) | ( + | AB*CDF/- |
) | AB*CDF/- + |
Output: AB*CDF/-+
You may also like
Python Queue
17 June, 2021
Solve any character pattern program in python
3 June, 2021
Solve any number patterns programs in python
3 June, 2021