Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible inaccuracy in 'Defensive Programming' #329

Closed
dndln opened this issue Nov 13, 2016 · 2 comments
Closed

Possible inaccuracy in 'Defensive Programming' #329

dndln opened this issue Nov 13, 2016 · 2 comments

Comments

@dndln
Copy link
Contributor

dndln commented Nov 13, 2016

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.

@tbekolay
Copy link
Contributor

Yes, you're quite right, a PR clarifying that would be great!

I don't recall when we added this exercise, but it strikes me as an odd one... why would we want to constrain the function to only accept monotonically increasing positive numbers? It's also not at all clear what would happen if we ran running('abc') with the 'defensive' code, as 'a' >= 0 is not obvious.

Perhaps the whole exercise should be replaced with something more straightforward?

dndln added a commit to dndln/python-novice-inflammation that referenced this issue Dec 16, 2016
@dndln dndln mentioned this issue Dec 16, 2016
valentina-s added a commit that referenced this issue Dec 22, 2016
@ldko
Copy link
Contributor

ldko commented Oct 3, 2020

This has been resolved.

@ldko ldko closed this as completed Oct 3, 2020
zkamvar pushed a commit that referenced this issue Apr 21, 2023
zkamvar pushed a commit that referenced this issue Apr 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants