Full bars
- O(n) · Medium
- Full plan
- Python
- JavaScript
- lists
- loops
- booleans
Problem
Clara is checking her student's sheet music before rehearsal. The piece is in bars of 4 beats, and the notes have to fill each bar exactly: no note can be split between one bar and the next.
Write a function that takes the list of note lengths, in order and in beats (whole numbers from 1 to 4), and returns true if they fill bars of exactly 4, and false if they don't. The notes are placed from the start: when a bar adds up to 4, the next one begins. With [3, 1, 2, 2] the first bar is 3 + 1 and the second is 2 + 2: true.
A total that's a multiple of 4 isn't enough: in [3, 2, 3] the 2 would make the first bar add up to 5, so it crosses the bar line and you return false. It's also false if the last bar is left incomplete. No notes means nothing is wrong: an empty list returns true.
Examples
Two exact bars
[4, 2, 2] → true
The last bar is short
[2, 2, 3] → false
Lots of short notes
[1, 1, 1, 1, 2, 1, 1] → true
It crosses the line in the second bar
[4, 1, 2, 2, 3] → false
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def full_bars(notes):
passJavaScript
function fullBars(notes) {
}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.