My issue
i am facing problem of hidden test case. suggest me a better solution
My code
from queue import Queue
class NumberPattern:
def printPattern(self, N):
# Initialize the queue
queue = Queue()
# Start by enqueuing 1
queue.put(1)
# To track printed numbers
printed_count = 0
next_number = 2 # Start with 2 for enqueueing
# Process until we've printed the first N numbers
while printed_count < N:
# Dequeue and print the front number
current = queue.get()
print(current, end=" ")
printed_count += 1
# Enqueue the next number
queue.put(next_number)
next_number += 1
# Print the remaining numbers in the queue
while not queue.empty():
print(queue.get(), end=" ")
# Example usage
if __name__ == "__main__":
N = 10 # Input for testing
obj = NumberPattern()
obj.printPattern(N)
Learning course: Data structures & Algorithms lab
Problem Link: Queue - Print Numbers with a Given Pattern in Data structures & Algorithms lab