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:
D = {'Ram' : 78, "Pankaj" : 67, 'Ravi' : 90, 'Manu' : 86}
The output from the program should be:
Manu Ravi Ram
Coding:
D = {'Ram' : 78, "Pankaj" : 67, 'Ravi' : 90, 'Manu' : 86}
def Push(S, N):
S.append(N)
def Pop(S):
if S != []:
return S.pop()
else:
return None
ST = []
for item in D:
if D[item] >= 75:
Push(ST,item)
while True:
if ST != []:
print(Pop(ST), end = " ")
else :
break
--------------------
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
Coding:
N = [12,13,34,56,21,79,98,22,35,38]
def Push(S, value):
S.append(value)
def Pop(S):
if S != []:
return S.pop()
else:
return None
ST = []
for item in N:
if item % 2 == 0:
Push(ST,item)
while True:
if ST != []:
print(Pop(ST), end = " ")
else :
break
Q) A line of text is read from the input terminal into a stack. Write a program to
output the string in the reverse order, each character appearing twice.
(e.g., the string a b c d e should be changed to ee dd cc bb aa)
Coding:
Max_Size = 5
stack = [0 for i in range(Max_Size)]
top = 0
def isEmpty():
return top == 0
def Push(x):
global stack,top
if top >= Max_Size:
return
stack[top] = x
top += 1
def Pop():
global stack,top
if isEmpty():
return
else:
top -= 1
return stack[top]
string = input("Enter String:").split()
for s in string:
Push(s)
while not isEmpty():
x = Pop()
print(x+x, end = " ")
-----------------
Q) Write a program to implement a stack for these book - details (Book Number, Book
Name). That is, now each item node of the stack contains two types of information -
Bookno and ites name. Just implement Push and display operationd
Coding(Push, Pop, peek and display)
def isEmpty(stk):
if stk == []:
return True
else:
return False
def Push(stk, item):
stk.append(item)
top = len(stk) - 1
def Pop(stk):
if isEmpty(stk):
return "Underflow"
else:
item = stk.pop()
if len(stk) == 0:
top = None
else:
top = len(stk) - 1
return item
def Peek(stk):
if isEmpty(stk):
return "Underflow"
else:
top = len(stk) - 1
return stk[top]
def disp(stk):
if isEmpty(stk):
print("Stack is Empty")
else:
top = len(stk) - 1
print(stk[top],",<--top")
for a in range(top-1,-1,-1):
print(stk[a])
stack = []
top = None
while True:
print("\n\nStack Operations : ")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Display Stack")
print("5. Exit")
ch = int(input("Enter your choice : "))
if ch == 1:
bno= int(input("Enter Item : "))
bname = input("Enter Book Name : ")
item = [bno,bname]
Push(stack, item)
elif ch == 2:
item = Pop(stack)
if item == "Underflow":
print("Stack is Empty")
else:
print("Popped item is : ", item)
elif ch == 3:
item = Peek(stack)
if item == "Underflow":
print("Stack is Empty")
else:
print("Topmost item is : ", item)
elif ch == 4 :
disp(stack)
elif ch == 5:
break
else:
print("Invalid Choice")
-----------------
Q) Write a program to make a stack of every third item of
the list, pop items and show.
L = [1,2,3,4,5,6,7,8,9]
def Push(S, N):
S.append(N)
def Pop(S):
if S != []:
return S.pop()
else:
return None
ST = []
for item in L[0:len(L):3]:
Push(ST,item)
while True:
if ST != []:
print(Pop(ST), end = " ")
else :
break
--------------
Q) Write a program to make a list of alphabets. Write only vowels from the list
into a stack. Pop all the items of the stack and show.
L = list(input("Enter a string: "))
def Push(S, N):
S.append(N)
def Pop(S):
if S != []:
return S.pop()
else:
return None
ST = []
for item in L:
if item in ['a', 'e','i', 'o', 'u','A', 'E', 'I', 'O', 'U']:
Push(ST,item)
while True:
if ST != []:
print(Pop(ST), end = " ")
else :
break
--------------------------
Q) Write a program to read a string through the keyboard.
write its those words into a stack which are starting with "T"
Pop all the words from the stack and show.
L = input("Enter a string: ")
A = list(L.split())
def Push(S, N):
if N[0] == 'T':
S.append(N)
def Pop(S):
if S != []:
return S.pop()
else:
return None
ST = []
for item in A:
Push(ST,item)
while True:
if ST != []:
print(Pop(ST), end = " ")
else :
break
-------------------
Q) WAP to make a tuple of numbers.
Make a stack and insert only those numbers into
the stack from tuple which are divisible by 10.
Pop all the values from stack and show.
T = (11,12,20,23,40,43,50)
def Push(S, N):
if N % 10 == 0:
S.append(N)
def Pop(S):
if S != []:
return S.pop()
else:
return None
ST = []
for item in T:
Push(ST,item)
while True:
if ST != []:
print(Pop(ST), end = " ")
else :
break