Skip to content

Instantly share code, notes, and snippets.

@gsinclair
Created February 14, 2020 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsinclair/2eef84de3743747644c09b3d3caf5c06 to your computer and use it in GitHub Desktop.
Save gsinclair/2eef84de3743747644c09b3d3caf5c06 to your computer and use it in GitHub Desktop.
Is a list increasing?
# Determine whether a list L is increasing. Return True or False.
def incr(L):
if len(L) < 2:
# An empty list or a single-value list is counted as increasing.
return True
answer = True
i = 1
while True:
if i >= len(L):
# We got to the end without returning False, so the answer must be True.
return True
if L[i] > L[i-1]:
# This part of the list is increasing; do nothing.
pass
else:
# We found a part that is not increasing; return False.
return False
# Increase the index so we consider the next part of the list.
i = i + 1
# Test the incr(L) function with a few different values.
# If a test fails, an error will occur.
# If nothing happens, all tests passed.
def test_incr():
assert incr([]) == True
assert incr([4]) == True
assert incr([3,4,7,9,11,20]) == True
assert incr([3,4,7,2,11,20]) == False
assert incr([3,4,7,7,11,20]) == False
# To run the test, type test_incr() in the right-hand side of repl.it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment