Page 22 - PYTHON-12
P. 22

(I)  push_complaint(ComplaintStack, complaint): This function takes the Stack ‘ComplaintStack’ and a
                           new complaint record ‘complaint’ as arguments and pushes the new complaint record onto the
                           Stack.
                        (II)  pop_complaint(ComplaintStack): This function pops the topmost complaint record from the Stack
                           and returns it. If the stack is empty, the function should display ‘Underflow’.
                        (III)  peep(ComplaintStack): This function displays the topmost element of the Stack without deleting it.
                           If the Stack is empty, the function should display ‘None’.
                                                             OR
                    (B)  Write the definition of the user-defined function ‘Push_upper(strings)’ that takes a list of strings and
                       pushes only the uppercase versions of each string onto a Stack named UpperStack.
                        Write ‘Pop_upper()’ function to pop and return the topmost uppercase string from UpperStack. If the
                       Stack is empty, it should display ‘Stack is empty.’                                  (3)
                        Write ‘Show_all_upper()’ function to display all uppercase strings in UpperStack without removing them.
                       If the Stack is empty, display ‘No uppercase strings available.’
              Ans.  (A)   (I)  def push_complaint(ComplaintStack, complaint):

                                ComplaintStack.append(complaint)
                                print("Complaint added successfully.")
                        (II)  def pop_complaint(ComplaintStack):

                                if len(ComplaintStack) == 0:
                                      print("Underflow")
                                      return None

                                else:
                                      return ComplaintStack.pop()
                        (III)  def peep(ComplaintStack):

                                if len(ComplaintStack) == 0:
                                      print("None")
                                else:
                                      print("Topmost complaint:", ComplaintStack[-1])
                                                             OR
                   (B)  UpperStack = []

                       def Push_upper(strings):
                               for string in strings:
                                    UpperStack.append(string.upper())
                               print("Uppercase strings pushed to Stack.")
                       def Pop_upper():
                               if len(UpperStack) == 0:
                                    print("Stack is empty.")
                                    return None
                               else:
                                    return UpperStack.pop()
                       def Show_all_upper():
                               if len(UpperStack) == 0:



              A.8    Computer Science–XII
   17   18   19   20   21   22   23   24   25   26   27