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

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

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy