Python Practice Queue1

In this class, We do Python Practice Queue1.

For Complete YouTube Video: Click Here

The reader should have prior knowledge of data structures and python. Check our data structures course for python Click here.

The examples we discuss will help the reader to improve his coding skills.

We can easily crack the placement exams. Take our practice tests in the placement training course.

Analyze the programs for better practice.

Q1) Consider the following queue
num_queue(front to rear)5 4 9 2 1 12
what will be the content of prime_queue returned by arrange_queue
if num_queue is passed as parameter

def arrange_queue(num_queue):
    prime_queue=Queue(6)
    count=0
    while not num_queue.isempty():#isempty return true if queue empty
        for index in range(1,num_queue.dequeue()): #enque and deque are used to inser and delete element from queue
            if num_que.dequeue()%index-1==0:
                count+=1
        if count>=1:
            prime_queue.enqueue(num_queue.dequeue()+2)
    return prime_queue

In the above program, they have given a queue object num_queue.

The queue class is already provided in python, and the description of the methods is given in the program.

The object num_queue is given as input to the function arrange_queue.

Analyze the program and identify the elements present in prime_queue after executing the function arrange_queue.

The elements present in prime_queue after execution are 14.

A detailed explanation of the code is provided in the video.

Q2) Consider a queue in_queue and a list in_list given below

in_queue(front to rear) 4,2,3,1
in_list=[1,4,5,2,6,8,12,3,10]

what will be the content of out_queue
if in_queue and in_list are passed as parameters to below function

def calculatesum(in_queue,in_list):
    out_queue=Queue(len(in_list))
    index=0
    while (not in_queue.isempty()):
        summ=0
        iteration=0
        number=in_queue.dequeue()
        while(index<len(in_list) and iteration<number):
            summ+=in_list[index]
            index+=1
            iteration+=1
        if iteration!=0:
            out_queue.enqueue(sum+in_queue.dequeue())
        else:
            out_queue.dequeue()
    out_queue.enqueue(index)
    return out_queue

A list and a queue object are provided as input to the function calculate sum in the above program.

We have to analyze the elements present in out_queue after executing the function calculate sum.

A clear explanation is provided in the video.

The elements in out_queue are 14, 27,7 after executing the function.