Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
udipta opened this issue May 30, 2022 · 0 comments

Comments

@udipta
Copy link

udipta commented May 30, 2022

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant