Page 9 - PYTHON-12
P. 9

OR

                (b) def Unique_Words(filename):
                       try:
                           with open(filename, 'r') as file:
                              text = file.read()
                              words = text.split()
                              return set(words)
                       except FileNotFoundError:
                              return "File not found."
                   Note: Users can take any filename.
           30. (a)  Consider a dictionary containing names and fees as key-value pairs of 5 doctors. Write a program with
                   separate user-defined functions to perform the following operations:                    (3)
                    (i) Push the keys (name of the doctors) of the dictionary into a Stack where the corresponding value
                       (fees) is greater than 1000.
                   (ii) Pop and display the contents of the Stack.
                       The dictionary should be as follows:
                       doc={"Reena":500, "Amrita": 1000, "Shama":900, "Vishal":1500, "Vyom":2000}
                       Then the output will be:

                       Vishal, Vyom
                                                          OR
                   (b) Write functions in Python, do_push(Num) and do_pop(Num), to add a new number and delete a
                       number from a list of numbers, considering them to act as push and pop operations of the
                       Stack data structure.
           Ans.  (a)  def push(stk,item):
                      stk.append(item)
                   def pop(stk):
                      if stk==[]:
                        return None
                      else:
                        return stk.pop()
                   stk=[]
                   doc={"Reena":500, "Amrita": 1000, "Shama":900, "Vishal":1500, "Vyom":2000}
                   for i in doc:
                       if doc[i] > 1000:
                        push(stk,i)
                   while True:
                      if stk!=[]:
                        print(pop(stk),end=" ")
                      else:
                                break
                                                          OR

                (b) def do_push(Num):
                       a=int(input("Enter a number:"))
                      Num.append(a)
                   def do_pop(Num):
                      if(Num==[]):
                        print("stack empty")
                      else:
                                print("Deleted element",Num.pop())






          A.40                                                           Computer Science with Python–XII
   4   5   6   7   8   9   10   11   12   13   14