You are on page 1of 5

1.

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

R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}


def Push(S,N):
S.append(N)

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.

For example: If the sample content of the dictionary is as follows:


SCORE={"KAPIL":40, "SACHIN":55, "SAURAV":80, "RAHUL":35,
"YUVRAJ":110, }
The output from the program should be: YUVRAJ SAURAV SACHIN
Program:
def Push(St,N):
St.append(N)
def Pop(St):
return St.pop()

SCORE={"KAPIL":40, "SACHIN":55, "SAURAV":80, "RAHUL":35,"YUVRAJ":110, }


St=[]
for i in SCORE:
if SCORE[i]>49:
Push(St,i)
while True:
if St!=[]:
print(Pop(St),end=' ')
else:
break
4.Raghav has created a vocabulary list. 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 entries having less than 7 characters into
a stack.
● Pop and display the content of the stack. For Example: If the sample Content of the
list is as follows: W=['Elucidate', 'Haughty', 'Pacify', 'Quip', 'Rapport', 'Urbane',
'Young','Zenith']
Sample Output of the code should be: Zenith Young Urbane Quip Pacify
def Push(St,N):
St.append(N)
def Pop(St):
return St.pop()
W=['Elucidate', 'Haughty', 'Pacify', 'Quip', 'Rapport',
'Urbane', 'Young','Zenith']
St=[]
for i in W:
if len(i)<7:
Push(St,i)
while True:
if St!=[]:
print(Pop(St),end=' ')
else:
break

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

Names=['ANKITA', 'NITISH', 'ANWAR', 'DIMPLE', 'HARKIRAT']


OnlyA=[]
Push(Names)
Pop(OnlyA)

6.Write the following user defined functions :


(i) pushEven(N) - This function accepts a list of integers named N as parameter. It then
pushes only even numbers into the stack named EVEN.
(ii) popEven(EVEN) - This function pops each integer from the stack EVEN and
displays the popped value. When the stack is empty, the message "Stack Empty" is
displayed.
For example: If the list N contains :
[10,5,3,8,15,4] Then the stack, EVEN should store [10,8,4]
And the output should be 4 8 10 Stack Empty

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")

8. Write the definition of a function POP_PUSH(LPop, LPush,


N) in Python. The function should Pop out the last N elements of the list LPop and Push
them into the list LPush. For example: If the contents of the list LPop are [10, 15, 20,
30] And value of N passed is 2, then the function should create the list LPush as [30, 20]
And the list LPop should now contain [10, 15] NOTE: If the value of N is more than
the number of elements present in LPop, then display the message
“Pop not possible”.
def POP_PUSH(LPop, LPush, N):
if N>len(LPop):
for i in range(N):
a=LPop.pop()
LPush.append(a)
else:
print("Pop Not Possible...")

LPop=[10, 15, 20, 30]


N=2
LPush=[]
POP_PUSH(LPop,LPush,N) print(LPop)
print(LPush)

You might also like