PRIP 10.2 Sumita Arora Solutions | Class 12 Computer Science

Here are PRIP 10.2 Sumita Arora Solutions for class 12 Computer Science. To view chapter 10 conceptual videos, visit here.

To view Sumita Arora solutions for all chapters, visit here.

Q.1: After the following operations have all been completed: draw the picture of the resulting queue.

(a) Q.Enqueue(20)
Answer: [20]

(b) Q.Enqueue(51)
Answer: [20 | 51 ]

(c) Q.Enqueue(31)
Answer: [ 20 | 51 | 31 ]

(d) Q.Enqueue(71)
Answer: [ 20 | 51 | 31 | 71 ]

(e) Q.Dequeue()
Answer: [ 51 | 31 | 71 ]

(f) Q.Enqueue(43)
Answer: [ 51 | 31 | 71 | 43]

(g) Q.Dequeue()
Answer: [ 31 | 71 | 43]

Q.2: Determine whether each of the following characteristics apply to a stack ( S), a queue(Q) both (B), or neither (N):

(a) An element is inserted at a special place called the top.
Answer:      

(b) An element is inserted at a special place called the rear.
Answer:      Q

(c) An element is deleted at the front.
Answer:      Q

(d) The ith position may be deleted.
Answer:      N

(e) An element is deleted at the top.
Answer:     

(f)The structure is a LIFO structure.
Answer:     

(g) The structure is a FIFO structure.
Answer:     Q

(h) The structure is a restricted access structure.
Answer:      B

Q.3: Write a program to implement a queue of order numbers in a restaurant. Display a menn as shown below:
                  YUMMY PROGRAM
                     1. Order a meal
                     2. Waiting Queue
                     3. Order is ready
                     4. Exit

• When option 1 is chosen, generate an order number of three digits, randomly and add it to your queue 
• For option 2, display the orders that are in queue.
• For option 3, dequeue an order number from the queue and flash it like:
           Order number <orderno> is ready e.g., 
           Order number 345 is ready.

Answer:

Code:
import random

def generate_order(order_queue):
    order_no = random.randint(100,999)
    order_queue.append(order_no)    # enqueue operation using listas queue
    
def waiting_orders(order_queue):
    print("orders in waiting list:")
    for i in order_queue:
        print(i,end=", ")

def dequeue_order(order_queue):
    front = order_queue.pop(0) # deque operation using list as queue
    print("Order number {} is ready.".format(front))

#__main__
order_queue = []  #create empty stack
choice = -1   # innitially set choice to -1
while(choice!=0):
    print()
    print("---------------")
    print("YUMMY PROGRAM")
    print("1. Order a meal")
    print("2. Waiting Queue")
    print("3. Order is ready")
    print("4. Exit")
    print("---------------")
    
    choice = input("Enter your choice: ")
    if(choice == "1"):
        generate_order(order_queue)
    if(choice == "2"):
        waiting_orders(order_queue)
    if(choice == "3"):
        dequeue_order(order_queue)
    if(choice == "4"):
        print("program exiting...")
        choice = 0
print("program ended")


Output:
---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
3. Order is ready
4. Exit
---------------
Enter your choice: 1

---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
3. Order is ready
4. Exit
---------------
Enter your choice: 1

---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
3. Order is ready
4. Exit
---------------
Enter your choice: 1

---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
3. Order is ready
4. Exit
---------------
Enter your choice: 2
orders in waiting list:
956, 111, 530, 
---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
3. Order is ready
4. Exit
---------------
Enter your choice: 3
Order number 956 is ready.

---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
3. Order is ready
4. Exit
---------------
Enter your choice: 1

---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
3. Order is ready
4. Exit
---------------
Enter your choice: 2
orders in waiting list:
111, 530, 541, 
---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
3. Order is ready
4. Exit
---------------
Enter your choice: 3
Order number 111 is ready.

---------------
YUMMY PROGRAM
1. Order a meal
2. Waiting Queue
3. Order is ready
4. Exit
---------------
Enter your choice: 4
program exiting...
program ended
4. A stack S stores some integer values. Write a program that displays the integers from the stack S in sorted order with the help of a dequeue.
Use output restricted deque D to accomplish this task as per following algorithm:
pop from stack, call it anum 
if deque empty add to deque 
if deque not empty 
    if anum < front-most of deque 
        add enum in beginning of deque 
    otherwise 
        add anum in the end of deque 
repeat above steps for all elements of stack 
finally display the deque

Answer:

  • Algorithm in question is not perfect 
  • Some modifications (additional steps) are made in algorithm
  • Instead of one, two dequeues are used
Code:
from collections import deque  # for this program we are using deque from collections module 

def isEmpty(temp):
    if len(temp)==0:
        return True
    return False

def sortUsingQueue(stack):
    deq = deque()
    while(not isEmpty(stack)):
        anum = stack.pop()
        if(isEmpty(deq)):
            deq.append(anum)  # append element at rear
        else:
            if anum < deq[0]:  # compare with front element
                deq.appendleft(anum)  # enque at the front
            else:
                if(deq[-1] < anum):
                    deq.append(anum) # enque at the rear
                else:
                    temp = deque()  # create a temporary deque
                    while(deq[-1] > anum):
                        temp.appendleft(deq.pop()) # add every greater element from left of deque
                    deq.append(anum)               # when while loop ends append anum
                    deq.extend(temp)               # extend temp deque to original deque 
        
        print(deq)       # print deq status with every iterations                          
    return deq

#__main__                
                
stack = [10, 20, 54, 5, 63, 40, 37, 88]       
deq = sortUsingQueue(stack)
print("Sorted list : ")

for i in deq:
    print(i,end = " ")


Output:   
deque([88])
deque([37, 88])
deque([37, 40, 88])
deque([37, 40, 63, 88])
deque([5, 37, 40, 63, 88])
deque([5, 37, 40, 54, 63, 88])
deque([5, 20, 37, 40, 54, 63, 88])
deque([5, 10, 20, 37, 40, 54, 63, 88])
Sorted list : 
5 10 20 37 40 54 63 88

Clear Doubts with Computer Tutor
In case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to join CTs learner-teacher community: students.computertutor.in

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You cannot copy content of this page