Skip to the content

In a row, in order

Problem

Sophia writes down, in order, the number of every bus that goes past her stop: 2, 4, 3, 5 and 7.

Write a function that takes that long list and a shorter one, and returns true if the short one shows up inside the long one with its numbers in a row and in the same order.

With 4 and 3 the answer is true: they went past one right after the other. With 3 and 7 it is false, even though both numbers are in the list: the 5 went past between them, so they are not in a row. That is the whole point of this kata.

The rules: order counts, so 3 and 4 are not there either. The short list can start anywhere in the long one, even right at the end. If the short one has more numbers than the long one, it does not fit and you return false. And the empty short list is contained in any list, because it asks for no numbers at all: in that case you return true.

Examples

  • One right after the other

    [2, 4, 3, 5, 7], [4, 3] → true

  • Both there, but apart

    [2, 4, 3, 5, 7], [3, 7] → false

  • Neither one is there

    [2, 4, 3, 5, 7], [1, 6] → false

  • Right at the end

    [8, 1, 9, 4, 6], [4, 6] → true

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def contains(whole, part):
    pass

JavaScript

function contains(whole, part) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n²) challenges

See all challenges →