Skip to content

Possible inaccuracy in 'Defensive Programming' #329

@dndln

Description

@dndln

https://swcarpentry.github.io/python-novice-inflammation/08-defensive/

Section: Testing Assertions

def running(values):
    assert len(values) > 0
    result = [values[0]]
    for v in values[1:]:
        assert result[-1] >= 0
        result.append(result[-1] + v)
        assert result[-1] >= result[0]
    return result

The second assertion checks that the first value in the list is positive. Input such as [-1,0,2,3] will make it fail.

The second assertion is actually checking if the last value of the 'result' list is positive on every iteration. On the first iteration it does check that the first value is positive, since result[0] == result[-1], but on subsequent iterations it does not.

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