Skip to content

Instantly share code, notes, and snippets.

@pat-lau
Created March 12, 2016 07:44
Show Gist options
  • Save pat-lau/09cc5d59aac7204a4852 to your computer and use it in GitHub Desktop.
Save pat-lau/09cc5d59aac7204a4852 to your computer and use it in GitHub Desktop.
A function called longestRun, which takes as a parameter a list of integers named L (assume L is not empty). This function returns the length of the longest run of monotonically increasing numbers occurring in L. A run of monotonically increasing numbers means that a number at position k+1 in the sequence is either greater than or equal to the n…
def getSublists(L, n):
return [L[i:i+n] for i in range(len(L)-n+1)]
def longestRun(L):
master = []
longest = 0
for i in range(len(L)+1):
for k in getSublists(L, i):
master.append(k)
for h in master:
if sorted(h) == h and len(h) >= longest:
longest = len(h)
return longest
# L = [1, 2, 3, -1, -2, -3, -4, -5, -6]
# longestRun(L)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment