Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Using Itertools - Explained solution in Clear category for Feed Pigeons by Selindian
import itertools
def checkio(food: int) -> int:
pigeons = 0 # Start with zero pigeons
for new_pigeons in itertools.count(1): # Endless loop for new pigeons (1, 2, 3, 4, ....)
if pigeons + new_pigeons <= food: # ... If we have enough food for old + new pigeons
pigeons += new_pigeons # ... ... add new pigeons to flock of pigeons that we will feed
food -= pigeons # ... ... and reduce the food by the ammount of all pigeons.
else: # ... else (not enough food to feed all)
return max(pigeons, food) # ... ... return whatever is higher (old pigeons or remaining food (for newly arrived pigeons)) as this is how many we can feed in this last round.
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio(1) == 1, "1st example"
assert checkio(2) == 1, "2nd example"
assert checkio(5) == 3, "3rd example"
assert checkio(10) == 6, "4th example"
Sept. 9, 2022
Comments: