Priyanka has created a dictionary 'emeployee_data' containing EmpCode and Salary as key value pairs for 5 Employees of Cyber Intratech. Write a program, With separate user defined function, as mentioned below, to perform the following operations:
(a) push_emp(): Push all those EmpCode, where the Salary is less than 25000, from the dictionary into a stack 'stk_emp'
(b) pop_emp(): Remove all the elements from the stack, one at a time, in a Last-In-First-Out(LIFO) manner and displays them. It also displays 'Stack is empty' once all the element have been removed.
For Example:
If the sample content of the dictionary is as follows:
{'E001':15000,'E002':27000,'E003':30000,'E004':15000,'E005':19000}, then the stack 'stk_emp' will contain EmpCode E001, E004, E005 after push_emp(). pop_emp() will pop and display employee record in LIFO fashion and display 'Stack is empty' at last.
Solution:
stk_emp=[]
employee_data={'E001':15000,'E002':27000,'E003':30000,'E004':15000,'E005':19000}
def push_emp():
for k in employee_data.keys():
if employee_data[k]<25000:
stk_emp.append(k)
def pop_emp():
while stk_emp!=[]:
print(stk_emp.pop())
else:
print("Stack is Empty")
push_emp()
pop_emp()
No comments:
Post a Comment