Interleave the first half of the queue with second half

from queue import Queue

def interLeaveQueue(q):

if (q.qsize() % 2 != 0):

print("Input even number of integers.")

s = []

size = int(q.qsize() / 2)

for i in range(size):

s.append(q.queue[0])

q.get()

while len(s) != 0:

q.put(s[-1])

s.pop()

for i in range(size):

q.put(q.queue[0])

q.get()

for i in range(size):

s.append(q.queue[0])

q.get()

while len(s) != 0:

q.put(s[-1])

s.pop()

q.put(q.queue[0])

q.get()

if __name__ == '__main__':

q = Queue()

q.put(11)

q.put(12)

q.put(13)

q.put(14)

q.put(15)

q.put(16)

q.put(17)

q.put(18)

q.put(19)

q.put(20)

interLeaveQueue(q)

length = q.qsize()

for i in range(length):

print(q.queue[0], end=" ")

q.get()


No comments: