Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is the loop running infinitely?

In the following code snippet, I intended to exit the while loop when the value of m becomes 0 or less than 0. But my while loop is still running even after the value of m finally decrements to 0. What is the reason for this infinite loop? Does it have anything to do with my else statement? If it does, why so?

NOTE: The reason I need the while loop here is because I need the for loop to run afresh when it terminates but the value of m is greater than 0.

Here is my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

n, m = input().split()
n = int(n)
m = int(m)

while m>=0:
    for i in range(n+1):
        if m >= i:
            m -= i
        else:
            break
   
        
print(m)

>Solution :

This is solved more clearly by recursion. Imagine a function f that determines how many chips are left to the presenter:

def f(chips: int, walruses: int, position: int = 1) -> int:
    # Base case, there are fewer chips than the current position number
    if chips < position:
        return chips
    else:  # Iterative case; decrement chips and recalculate position
        chips = chips - position
        position = position + 1 if position < walruses else 1
        return f(chips, walruses, position)

walrus_count, starting_chips = input().split()
print(f(int(starting_chips), int(walrus_count)))

That said, if you really want to use a while loop:

def f(chips: int, walruses:int) -> int:
    position = 1
    # Quit if we don't have enough chips
    while chips > position:
        # Decrement chips
        chips = chips - position
        # Reset position if we've gone around the circle
        position = 1 if position >= walruses else position + 1
    return chips

The problem you are having is that you’re nesting loops.

while m >= 0: # Loop until you run out of chips -> but that is not what the problem calls for
    for i in range(0, n+1):  # i is position, and you're looking to loop once you run out of walruses. Alas, you're zero-indexing here!
        if m >= i:  # Check to see if there are enough chips, which is a half-overlap of the outer loop
            m -= i  # Decrement chips, correct
        else:
            break  # Breaks out of for-loop, but not while-loop
     # No extra check here to see if you need to break out of while loop. Need to add one, or fix the while loop check.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading