Uploaded by meenakshi nagpal

assignment Stack

advertisement
Assignment - Stack
1.
A list contains following record of course details for a University :
[Course_name, Fees, Duration]
Write the following user defined functions to perform given operations on the stack named
'Univ' :
(i) Push_element() To push an object containing the
Course_name, Fees and Duration of
a course, which has fees greater than 100000 to the stack.
(ii) Pop_element() To pop the object from the stack and display it.
For example :
If the lists of courses details are :
[“MCA”,200000,3]
[“MBA”,500000,2]
[“BA”,1000000,3]
The stack should contain :
[“MBA”,500000,2]
[“MCA”,200000,3]
2.
ICC 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 59.
● Pop and display the content of the stack.
For example:
If the sample content of the dictionary is as follows:
SCORE={"Mukesh":43, "Virendra":64, "Rajesh":88, "Amit":75, "Wajid":97 }
The output from the program should be:
Virendra
Rajesh
Amit
Wajid
3.
Two list Lname and Lage contains name of person and age of person respectively. A list
named Lnameage is empty. Write functions as details given below
(i) Push_na() :- it will push the tuple containing pair of name and age from Lname and Lage
whose age is above 50
(ii) Pop_na() :- it will remove the last pair of name and age and also print name and age of
removed person. It should also print “underflow” if there is nothing to remove
For example the two lists has following data
Lname=[‘narender’, ‘jaya’, ‘raju’, ‘ramesh’, ‘amit’, ‘Piyush’]
Lage=[45,23,59,34,51,43]
After Push_na() the contains of Lnameage stack is
[(‘raju’,59),(‘amit’,51)]
The output of first execution of pop_na() is
The name removed is amit
The age of person is 51
4.
A dictionary stu contains rollno and marks of students. Two empty list stack_roll and
stack_mark will be used as stack. Two function push_stu() and pop_stu() is defined and
perfom following operation
(a) Push_stu() :- It reads dictionary stu and add keys into stack_roll and values into
stack_marks for all students who secured more than 60 marks.
(b) Pop_stu() :- it removes last rollno and marks from both list and print “underflow” if there
is nothing to remove
For example
stu={1:56,2:45,3:78,4:65,5:35,6:90}
values of stack_roll and stack_mark after push_stu()
[3,4,6] and {78,65,90}
5.
Given a Dictionary Stu_dict containing marks of students for three test-series in the form
Stu_ID:(TS1, TS2, TS3) as key-value pairs.
Write a Python program with the following user-defined functions to perform the specified
operations on a stack named Stu_Stk
(i) Push_elements(Stu_Stk, Stu_dict) : It allows pushing IDs of those students, from the
dictionary Stu_dict into the stack Stu_Stk, who have scored more than or equal to 80
marks in the TS3 Test.
(ii) Pop_elements(Stu_Stk): It removes all elements present inside the stack in LIFO order and
prints them. Also, the function displays 'Stack Empty' when there are no elements in the
stack.
Call both functions to execute queries.
For example:
If the dictionary Stu_dict contains the following data:
Stu_dict ={5:(87,68,89), 10:(57,54,61), 12:(71,67,90), 14:(66,81,80), 18:(80,48,91)}
After executing Push_elements(), Stk_ID should contain
[5,12,14,18]
After executing Pop_elements(), The output should be:
18
14
12
5
Stack Empty
Answers
1.
Course=[["MCA",200000,3],["MBA",500000,2],["BA",100000,3]]
Univ=[]
def Push_element():
for Rec in Course:
if Rec[1]>100000:
Univ.append(Rec)
def Pop_element():
while len(Univ)>0:
print(Univ.pop())
else:
print("Underflow")
Push_element()
Pop_element()
2.
SCORE={"Mukesh":43, "Virendra":64, "Rajesh":88, "Amit":75, "Wajid":97 }
def PUSH(S,R):
S.append(R)
def POP(S):
if S!=[]:
return S.pop()
else:
return None #Message
ST=[ ]
for k in SCORE:
if SCORE[k]>59:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
else:
break
3.
Lname=['narender', 'jaya', 'raju', 'ramesh', 'anita', 'Piyush']
Lage=[45,23,59,34,51,43]
Lnameage=[]
def push_na():
for x in range(len(Lname)):
if Lage[x]>50:
Lnameage.append((Lname[x],Lage[x]))
print(Lnameage)
def pop_na():
if len(Lnameage)==0:
print("Underflow")
else:
t=Lnameage.pop()
print("The name removed is ",t[0])
print("The age of person is ",t[1])
push_na()
pop_na()
4.
stu={1:56,2:45,3:78,4:65,5:35,6:90}
stack_roll=[]
stack_marks=[]
def push_stu():
for x,y in stu.items():
if y>60:
stack_roll.append(x)
stack_marks.append(y)
def pop_stu():
if len(stack_roll)==0:
print("underflow")
else:
stack_roll.pop()
stack_marks.pop()
push_stu()
pop_stu()
5.
Stu_dict={5:(87,68,89), 10:(57,54,61), 12:(71,67,90), 14:(66,81,80), 18:(80,48,91)}
Stu_Stk=[]
def Push_elements(Stu_Stk, Stu_dict):
for Stu_ID, marks in Stu_dict.items():
if marks[2]>=80:
Stu_Stk.append(Stu_ID)
def Pop_elements(Stu_Stk):
while len(Stu_Stk)>0:
print(Stu_Stk.pop())
if not Stu_Stk:
print('Stack Empty')
Push_elements(Stu_Stk, Stu_dict)
Pop_elements(Stu_Stk)
Download