ASSIGNMENT: STACK
1. 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 perform 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}
2. A list contains following record of a student in the form of a list containing student_name, marks, subject
Write the following user defined functions to perform given operations on the stack named ‘status’:
a) Push_element(status,student) –this function takes the stack status and new students record as argument and
push an new student record containing name and marks of a student who scored more than 75 marks in ‘CS’
to the stack
b) Pop_element(status) – function pops the topmost record from the stack and returns [Link] the stack is empty the
function should display “underflow”.
For example:
If the lists of customer details are: [“Danish”,80,”Maths”] [“Hazik”,79,”CS”]
[“Sidhi”,99,”CS”]
The stack should contain [“Hazik”,”79”] [“Sidhi”,”99”]
The output should be: [“Hazik”,”79”] [“Sidhi”,”99”]
Stack Empty
3. Write a function in Python, Push(Item) where Item is a list containing the details
of Bakery Items – [[Name, Price], [Name, Price], [Name, Price]]
The function should push the names of those items in the Stack who have price less than Rs. 50. Also display the
count of elements pushed into the Stack.
Example: If the list contains the following items :[['ravi',26],['raman',36], ['chaman',56]]
The Stack should contain : Ravi Raman
The output should be : The number of elements in stack is : 2
4. Consider a list named NUMS which contains random numbers :
Write the following user defined functions in python and perform the specified operations on a stack named
BigNums.
a) PushBig()- it checks every number from the list Nums and pushes all such numbers which have 5 or more digits
into the stack BigNums
b) PopBig() – It pops the numbers from the stack ,BigNums and disply them . the function should also display
“Stack Empty ” when there are no numbers left
For example if the list Nums=[213,10025,1267,24357,14,1234567]
Then on execution PushBig() should store [10025,24357,1234567]
And on execution of PopBig(), the following output should be displayed
10025 24357 1234567
5. Write the python program to accept 10 integers from the user .If the entered number is a three digit even number
push it onto stack. After all inputs are taken , pop all the three digits even integers from the stack and display them
for example if the user enter 12,31,320,457,6,92,924,220,1,218
Then the stack should contain 320,924,220.218.
The output of the program should be like : 218.,220,924,320
6. Write a program in Python to input 5 integers into a list named NUM. The program should then use the function
Push 3_5() to create the stack of the list Only3_5 which contains only those number which are divided by either 3 or
5 or both .There after 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 StackEmpty