Giter Club home page Giter Club logo

lis's Introduction

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]

lis's People

Contributors

mailund avatar christinagrand avatar jojovin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.