Data Structures
Data Structures
Julie has created a dictionary containing names and marks as key value pairs of 6
students. Write a program, with separate user defined functions to perform the
following operations:
● Push the keys (name of the student) of the dictionary into a stack, where the
corresponding value (marks) is greater than 75.
● Pop and display the content of the stack. For example: If the sample content of
the dictionary is as follows:
R={"OM":76, "JAI":45, "B/OB":89, "ALI":65, "ANU":90, "TOM":82}
The output from the program should be: TOM ANU BOB OM
def Pop(St):
return St.pop()
St=[]
for i in R:
if R[i]>75:
Push(St,i)
while True:
if St!=[]:
print(Pop(St),end=' ')
else:
break
2. Alam has a list containing 10 integers. You need to help him create a program with
separate user defined functions to perform the following operations based on this list.
● Traverse the content of the list and push the even numbers into a stack.
● Pop and display the content of the stack. For
Example: If the sample Content of the list is as follows:
N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
Sample Output of the code should be:
38 22 98 56 34 12
N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
def Push(St,N):
St.append(N)
def Pop(St):
return St.pop()
St=[]
for i in N:
if i%2==0:
Push(St,i)
print(St)
while True:
if St!=[]:
print(Pop(St),end=' ')
else:
break
3. BCCI has created a dictionary containing top players and their runs as key value
pairs of cricket team. Write a program, with separate user defined functions to
perform the following operations:
● Push the keys (name of the players) of the dictionary into a stack, where the
corresponding value (runs) is greater than 49.
● Pop and display the content of the stack.
5. Write separate user defined functions for the following : (i) PUSH(N) - This function
accepts a list of names, N as parameter. It then pushes only those names in the stack
named OnlyA which contain the letter 'A’. (ii) POPA(OnlyA) - This function pops each
name from the stack OnlyA and displays it.
When the stack is empty, the message "EMPTY" is displayed.
For example : If the names in the list N are ['ANKITA', 'NITISH', 'ANWAR',
'DIMPLE', 'HARKIRAT’] Then the stack OnlyA should store ['ANKITA',
'ANWAR', 'HARKIRAT’] And the output should be displayed as HARKIRAT
ANWAR ANKITA EMPTY
def Push(N):
for i in N:
if 'A' in i:
OnlyA.append(i)
def Pop(OnlyA):
while True:
if OnlyA!=[]:
print((OnlyA.pop()),end=' ')
else:
print("EMPTY")
break
def pushEven(N):
for i in N:
if i%2==0:
EVEN.append(i)
def popEven(EVEN):
while True:
if EVEN!=[]:
print((EVEN.pop()),end=' ')
else:
print("Stack Empty ")
break
N=[10,5,3,8,15,4]
EVEN=[]
pushEven(N)
popEven(EVEN)
7. ● Write the definition of a user defined function Push3_5(N) which accepts a list of
integers in a parameter N and pushes all those integers which are divisible by 3 or
divisible by 5 from the list N into a list named Only3_5.
● Write a program in Python to input 5 integers into a list named NUM. The program
should then use the function Push3_5() to create the stack of the list Only3_5.
Thereafter pop each integer from the list Only3_5 and display the popped value. When
the list is empty, display the message "StackEmpty". For example: If the integers input
into the list NUM are :
[10,6,14,18,30] Then the stack Only3_5 should store
[10,6,18,30] And the output should be displayed as 30 18 6 10
def Push3_5(N):
for i in N:
if i%3==0 or i%5==0:
Only3_5.append(i)
NUM=[]
Only3_5=[]
for i in range(5):
a=int(input("Enter Number : "))
NUM.append(a)
print(NUM)
Push3_5(NUM)
while True:
if Only3_5!=[]:
print((Only3_5.pop()),end=' ')
else:
break
print("Stack Empty")