Complete beginner here. For the "Feed Pigeons" task, I have come up with the following:
import numpy
def checkio(feedportions: int):
birds = []
newbirds = 0
increment_index = 0
while sum(birds) < feedportions:
newbirds += 1
birds.extend([0]*newbirds)
while sum(birds) < feedportions and increment_index < len(birds):
birds[increment_index] += 1
increment_index += 1
return numpy.count_nonzero(birds), birds
For the input checkio(5), I expected it to return (3, [3, 1, 1, 0, 0, 0]). But instead it returns (5, [1, 1, 1, 1, 1, 0]). It appears that the variable increment_index is keeping its value after the nested loop finishes running instead of resetting back to 0 (so the pigeons that got fed already aren't getting fed again). How can I make it reset back to 0 whenever the nested while loop completes?
Created at: 2022/10/17 10:06; Updated at: 2022/10/17 11:09
The question is resolved.