A steady beat
- O(n) · Medium
- Full plan
- Python
- JavaScript
- lists
- loops
- booleans
Problem
Nora recorded a drum rehearsal without a metronome and wrote down the millisecond when each hit landed. Now she wants to know if she kept perfect time: every hit the same distance from the next.
Write a function that takes hits, a list of integers from smallest to largest with no repeats (it may be empty), and returns true if the gap between each hit and the next is always the same, and false if not. No one gives you that gap: the reference is the gap between the first two hits, and every other gap is compared with it.
With [0, 250, 500, 750] it returns true. With [100, 400, 700, 950] it returns false: the gaps are 300, 300 and 250. With two hits or fewer there is nothing to compare, so it returns true.
Examples
Steady
[0, 250, 500, 750] → true
Rushes at the end
[100, 400, 700, 950] → false
One out of place in the middle
[0, 50, 110, 150, 200] → false
Two hits
[1000, 1500] → true
Five steady hits
[7, 19, 31, 43, 55] → true
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def steady_beat(hits):
passJavaScript
function steadyBeat(hits) {
}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.