Skip to the content

The vault combination

Problem

Irene, the castle locksmith, has worked out the secret of the vault: it only opens for combinations shaped like a mountain. They climb, reach a peak and come down, without ever standing still.

Write a function that takes the combination, a list of whole numbers (possibly negative), and returns true if it is a mountain and false if not. It is a mountain if each number is greater than the one before until the peak, and from there on each one is smaller than the one before until the end. It has to go up at least once and down at least once: only going up or only going down is not a mountain. Two equal numbers in a row break it, and so does going up again after coming down. With fewer than three numbers, or an empty list, you return false. [1, 4, 7, 5, 2] is a mountain; [1, 3, 2, 4, 1] is not.

Examples

  • The example

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

  • It climbs again

    [1, 3, 2, 4, 1] → false

  • It stands still at the peak

    [2, 5, 5, 3] → false

  • It only goes up

    [1, 2, 3, 4] → false

  • It only goes down

    [9, 6, 3] → false

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

You start with this

Python

def is_mountain(numbers):
    pass

JavaScript

function isMountain(numbers) {
}
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 →