1.
lpha Company has created a dictionary Emp which stores eid and his salary
as key value pairs. The dictionary Emp contains the following key value
pairs
Emp={1001:45000,1002:56000,1003:65000,1004:90000,1005:46000,1006:
55000}
Define the following user defined functions with the help of menu driven program tto
push and pop values.
Push()-To push the keys(eid) of the dictionary into stack, where the corresponding
values(salary) is greater than 50000. After pushing, the keys in the stack area as
follow.
1002,1003,1004,1006
Pop() should pop out the peek element i.e 1006
OUTPUT
emp={1001:45000,1002:56000,1 MENU
003:65000,1004:90000,1005:460 1. PUSH
00,1006:55000} 2. POP
3. DISPLAY
4 peek
c=len(emp) 5. EXIT
print(c) accept which operation?1
sorry could not push as not matching criteria
ch=True sorry could not push as not matching criteria
p=0 [1002, 1003, 1004, 1006]
st=[] MENU
1. PUSH
def push(): 2. POP
for k in emp: 3. DISPLAY
if emp[k]>50000: 4 peek
[Link](k) 5. EXIT
else: accept which operation?4
print("sorry could not the peek element is 1006
push as not matching criteria") MENU
print(st) 1. PUSH
2. POP
def poping(): 3. DISPLAY
if (len(st)==0): 4 peek
print("empty stack so 5. EXIT
nothing can be poped out") accept which operation?5
return False
else:
v=[Link]()
return v
def display():
if len(st)==0:
print("nothing is for display
because the stack is empty")
else:
for i in range((len(st)-1),-1,-
1):
print(st[i])
def peek():
if len(st)==0:
print("no peek element as
nothing is in stack")
else:
print("the peek element
is",st[len(st)-1])
while(ch):
print("MENU")
print("1. PUSH")
print("2. POP")
print("3. DISPLAY")
print("4 peek")
print("5. EXIT")
op=int(input("accept which
operation?"))
if op==1:
push()
elif op==2:
val=poping()
if val==False:
print("nothing in stack
for poping")
else:
print("the value
whichever is poped",val)
elif op==3:
display()
elif op==4:
peek()
elif op==5:
break;
else:
print("wrong choice")
2. The owner of the cinema hall ordered the gate man to collect all the tickets of the audience and
then asked him to provide all tickets which are divisible by 2 for the purpose of lucky draw. He
entered all the ticket numbers in the form of a list. Now You need to help him create a menu
program with separate user defined functions to perform the following operations based on this list.
● Traverse the content of the list and push all those numbers which are divisible by 2 into a stack.
● Pop and display the content of the stack.
For Example:
If the sample Content of the list is as follows:
N=[1211, 1300, 3224, 5660, 2111, 7987, 9800, 2222, 3500, 3800]
Sample Output of the code should displaly 3800 after 1st pop
and after that the status of the stack should be as follows:
[1300,3224,5660,9800,2222,3500]
OUTPUT:-
n=int(input("how many"))
sl=[0 for i in range(n)] how many10
for i in range(n): accept ticket no1211
tno=int(input("accept ticket no")) accept ticket no1300
sl[i]=tno accept ticket no3224
print(sl) accept ticket no5660
ch=True accept ticket no2111
p=0 accept ticket no7987
st=[] accept ticket no9800
accept ticket no2222
def push(): accept ticket no3500
global p accept ticket no3800
if sl[p]%2==0: [1211, 1300, 3224, 5660, 2111, 7987, 9800,
[Link](sl[p]) 2222, 3500, 3800]
print("the pushed element MENU
is",st[::-1]) 1. PUSH
print(st) 2. POP
else: 3. DISPLAY
print("sorry could not push as 4 peek
not matching criteria") 5. EXIT
p=p+1 accept which operation?1
sorry could not push as not matching criteria
def poping(): MENU
if (len(st)==0): 1. PUSH
print("empty stack so nothing 2. POP
can be poped out") 3. DISPLAY
return False 4 peek
else: 5. EXIT
v=[Link]() accept which operation?1
return v the pushed element is 1300
[1300]
def display(): MENU
if len(st)==0: 1. PUSH
print("nothing is for display 2. POP
because the stack is empty") 3. DISPLAY
else: 4 peek
for i in range((len(st)-1),-1,-1): 5. EXIT
print(st[i]) accept which operation?1
the pushed element is 3224
def peek(): [1300, 3224]
if len(st)==0: MENU
print("no peek element as 1. PUSH
nothing is in stack") 2. POP
else: 3. DISPLAY
print("the peek element 4 peek
is",st[len(st)-1]) 5. EXIT
accept which operation?1
the pushed element is 5660
[1300, 3224, 5660]
while(ch): MENU
print("MENU") 1. PUSH
print("1. PUSH") 2. POP
print("2. POP") 3. DISPLAY
print("3. DISPLAY") 4 peek
print("4 peek") 5. EXIT
print("5. EXIT") accept which operation?1
sorry could not push as not matching criteria
op=int(input("accept which
MENU
operation?")) 1. PUSH
if op==1: 2. POP
if p==n: 3. DISPLAY
print("no more elements") 4 peek
5. EXIT
else: accept which operation?1
push() sorry could not push as not matching criteria
elif op==2: MENU
val=poping() 1. PUSH
if val==False: 2. POP
print("nothing in stack for 3. DISPLAY
poping") 4 peek
else: 5. EXIT
print("the value whichever is accept which operation?1
poped",val) the pushed element is 9800
elif op==3: [1300, 3224, 5660, 9800]
display() MENU
elif op==4: 1. PUSH
peek() 2. POP
elif op==5: 3. DISPLAY
break; 4 peek
else: 5. EXIT
print("wrong choice") accept which operation?1
the pushed element is 2222
[1300, 3224, 5660, 9800, 2222]
MENU
1. PUSH
2. POP
3. DISPLAY
4 peek
5. EXIT
accept which operation?1
the pushed element is 3500
[1300, 3224, 5660, 9800, 2222, 3500]
MENU
1. PUSH
2. POP
3. DISPLAY
4 peek
5. EXIT
accept which operation?1
no more elements
MENU
1. PUSH
2. POP
3. DISPLAY
4 peek
5. EXIT
accept which operation?3
3500
2222
9800
5660
3224
1300
MENU
1. PUSH
2. POP
3. DISPLAY
4 peek
5. EXIT
accept which operation?
3. Vivek has created a dictionary Book which stores Book name and its
price as key value pairs for 5 products. Write a menu driven program
with user defined functions to perform the following operations:
● Push the keys (name of the Book ) of the dictionary into a stack,
where the corresponding value (price) is greater than Rs.125/-
● Pop and display the content of the stack. For
example:
If the sample content of the dictionary is as follows:
BOOK={“Wings of fire“: 400, ”The leader”:135, “Broken heart”:125,”name
sake”:135,”Beyond blue mountains”:165,”chicken soup”:75}
The output from the program after 1st pop should be
Beyond blue mountains
and the status of the stack should be as follows
Wings of fire, the leader, name sake,
OUTPUT
bd={"wings of fire":150, "name STRUCTURES\stack_books.PY
sake":100, "beyond the blue ========
moutains":200,"the leader":145} 4
c=len(bd) MENU
print(c) 1. PUSH
2. POP
ch=True 3. DISPLAY
p=0 4 peek
st=[] 5. EXIT
accept which operation?1
def push(): sorry could not push as not matching
for k in bd: criteria
if bd[k]>125: ['wings of fire', 'beyond the blue
[Link](k) moutains', 'the leader']
else: MENU
print("sorry could not push as not 1. PUSH
matching criteria") 2. POP
print(st) 3. DISPLAY
4 peek
def poping(): 5. EXIT
if (len(st)==0): accept which operation?3
print("empty stack so nothing can be the leader
poped out") beyond the blue moutains
return False wings of fire
else: MENU
v=[Link]() 1. PUSH
return v 2. POP
3. DISPLAY
def display(): 4 peek
if len(st)==0: 5. EXIT
print("nothing is for display because accept which operation?4
the stack is empty") the peek element is the leader
else: MENU
for i in range((len(st)-1),-1,-1): 1. PUSH
print(st[i]) 2. POP
3. DISPLAY
def peek(): 4 peek
if len(st)==0: 5. EXIT
print("no peek element as nothing is accept which operation?2
in stack") the value whichever is poped the leader
else: MENU
print("the peek element is",st[len(st)- 1. PUSH
1]) 2. POP
3. DISPLAY
4 peek
5. EXIT
while(ch): accept which operation?3
print("MENU") beyond the blue moutains
print("1. PUSH") wings of fire
print("2. POP") MENU
print("3. DISPLAY") 1. PUSH
print("4 peek") 2. POP
print("5. EXIT") 3. DISPLAY
4 peek
op=int(input("accept which 5. EXIT
operation?")) accept which operation?
if op==1:
push()
elif op==2:
val=poping()
if val==False:
print("nothing in stack for poping")
else:
print("the value whichever is
poped",val)
elif op==3:
display()
elif op==4:
peek()
elif op==5:
break;
else:
print("wrong choice")