#STACK Assignment :1 With Solutions ''' Q1. A linear stack called "List" contain the following information: a. Roll Number of student b. Name of student Write add(List) and pop(List) methods in python to add and remove from the stack. Ans. ''' List=[] def add(List): rno=int(input("Enter roll number")) name=input("Enter name") item=[rno,name] List.append(item) def pop(List): if len(List)>0: List.pop() else: print("Stack is empty") #Call add and pop function to verify the code add(List) add(List) pop(List) print(List) ''' Q2. Write push(edetail) and pop(edetail) in python to add and remove the employee detail in a stack called "edetail". "edetail" stack store the following details: a. Name of employee b. Salary of employee Ans. ''' edetail = [] def push(edetail): name = input("Enter name") sal = int(input("Enter Salary")) item = [name, sal] edetail.append(item) def pop(edetail): if len(edetail) > 0: edetail.pop() else: print("Stack is empty") #Call push and pop function to verify the code push(edetail) push(edetail) pop(edetail) print(edetail) ''' Q3. Write addscore(game) and delscore(game) methods in python to add and remove score from a list "game", considering these methods to act as push and pop operations of data structure stack. Ans. ''' game = [] def addscore(game): sc=int(input("Enter Score")) game.append(sc) def pop(ed):#in place of 'ed' we can use 'game'also if len(ed) > 0: ed.pop() else: print("Stack is empty") #Call functions to verify the code addscore(game) addscore(game) pop(game) print(game) #OR (When we don't want to accept value from the user) game = [] def addscore(sc): game.append(sc) def pop(): if len(game) > 0: game.pop() else: print("Stack is empty") #Call functions to verify the code addscore(23) addscore(54) pop() print(game) ''' Q4. Write addsal(sal) and removesal(sal) functions in python to add and remove salary from a list of salary in a list "sal", considering these methods to act as push and pop operations of data structure stack. Ans. ''' sal = [] def addsal(sc): sal.append(sc) def pop(): if len(sal) > 0: sal.pop() else: print("Stack is empty") #Call functions to verify the code addsal(23) addsal(54) pop() print(sal) '''Q5. Write addfee() function in python to add the following detail in list named "fee". feeamount feemonth Considering this as push function of stack ''' Ans. fee=[] def addfee(fee): fa=int(input("Enter the fee amount")) fm = input("Enter the fee month") item = [fa,fm] fee.append(item) addfee(fee)