Skip to content

remove unwanted extra interation of loop and saving data in queue in 6_Queue/Exercise/binary_numbers.py #57

Open
@udipta

Description

@udipta

https://github.com/codebasics/data-structures-algorithms-python/blob/master/data_structures/6_Queue/Exercise/binary_numbers.py

Issue:

def produce_binary_numbers(n):
    numbers_queue = Queue()
    numbers_queue.enqueue("1")

    for i in range(n):
        front = numbers_queue.front()
        print("   ", front)
        numbers_queue.enqueue(front + "0")
        numbers_queue.enqueue(front + "1")

        numbers_queue.dequeue()

Proposed below:

def produce_binary_numbers(n):
    numbers_queue = Queue()
    numbers_queue.enqueue("1")
    i = 0
    while i < n//2 and not numbers_queue.is_empty():
        i+=1
        front = numbers_queue.dequeue()
        print(front)
        numbers_queue.enqueue(front + "0")
        numbers_queue.enqueue(front + "1")
        
    while not numbers_queue.is_empty() and i<n:
        print(numbers_queue.dequeue())
        i+=1

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @udipta

        Issue actions

          remove unwanted extra interation of loop and saving data in queue in 6_Queue/Exercise/binary_numbers.py · Issue #57 · codebasics/data-structures-algorithms-python