Skip to content
/ lis Public template
forked from birc-ctib/lis

Longest increasing substring

Notifications You must be signed in to change notification settings

lauramhfur/lis

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Longest increasing substring

Given a sequence x over whatever type you can think of that has an order to it (i.e. we can compare if one element is smaller than another), we can define increasing substrings as a slice x[i:j] where each element is smaller than the following. Despite the name substring, it doesn't have to be an actual string. It is just the anme for this kind of sub-sequence, where the elements are consecutive.

The function below will check if an entire list is increasing. You could call it as is_increasing(x). If you wanted to know if a sub-string x[i:j] is increasing, you could just as well as is_increasing(x[i:j]).

def is_increasing(x: Sequence[Any]) -> bool:
    """
    Determine if x is an increasing sequence.
    """
    for i in range(len(x) - 1):
        if not x[i] < x[i+1]:
            return False
    return True

We want a function that gives us the longest increasing substring of any given sequence x. This could be the entire sequence, of course, if the full sequence is increasing. It is possible that two inreasing substrings have the same length, but in that case we want the left-most.

In applications with very long strings, copying out a substring can be expensive. However, we have the same information if we have x[i:j] or (x,i,j), so if we already know x, then we can represent a substring simply using the indices i and j. So our function should return that.

With this representation, you can get the length of a substring like this:

def substring_length(substring: tuple[int, int]) -> int:
    """Give us the length of a substring, represented as a pair."""
    return substring[1] - substring[0]

About

Longest increasing substring

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 82.4%
  • Shell 17.6%