CheckIO: Feed Pigeons

def iter_pigeons():
    i = 1
    while True:
        yield sum(range(1, i + 1))
        i += 1


def checkio(portions):
    pigeons = iter_pigeons()
    feeded = 0
    while True:
        hungry = next(pigeons)
        if portions <= hungry:
            return portions if portions > feeded else feeded
        else:
            portions = portions - hungry
            feeded = hungry


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"

 http://www.checkio.org/mission/feed-pigeons/

Leave a Reply

Your email address will not be published. Required fields are marked *