In musical notation, groups of notes shorter than one beat are joined together by a line at the bottom called a beam. Here are a few bars of music with the beams highlighted:
(Taken from Second Suite in F by Gustav Holst; you can hear how these notes sound at 0:13 in this video.)
You may notice that beam #6 is above the ovals that define the notes, and its vertical lines attached to their right sides, while the others are below the notes and attached to their left. What determines this? There are a few simple rules, which are the subject of this challenge.
- By default, the placement of the beam is determined by its highest or lowest note. If the note farthest from the centermost line of the staff is above it, then the beam goes below the notes. If the note with the highest absolute value is above the centermost line of the staff, then the beam goes below the notes.
- If the highest and the lowest note are the same distance from the center line, the direction of the beam is determined by which side of the center line the majority of notes in the beam lie on. If a majority of notes lie above the centre line, the beam goes down. If a majority of notes lie below the centre line, the beam goes up. Notes on the center line do not count either way.
- If the highest and the lowest note are the same distance from the center line, and equally many notes lie above and below the center line, the beam may be placed either above or below the notes.
In the sample notation above, the beams of clusters #1, 2, 3, and 5 are determined to be below the notes because their highest notes (their second, second, first, and third respectively) are above the center line. #6's lowest note is its first/third note, which is below the center line. #4's highest and lowest note are equally distant from the center line and it contains no majority either way. Its beam could be placed either above or below, but my composition program renders it as below.
Write a program that takes in a list of integers, which may be zero or negative, and outputs a truthy/falsey value. These integers represent notes and their distance from the center line; positive values represent notes above the line, zero represents notes on the line, and negative values represent notes below the line. Determine, given a cluster of notes that sit those distances from the center line, which way the cluster's beam should go; output
TRUE if the beam should go above the notes and
FALSE if the beam should go below the notes. To restate the rules above and give the algorithm:
- Find the number in the list with the largest absolute value. If it is greater than zero, output
FALSE. If it is less than 0, output
TRUE. If it is 0, output may be
FALSEas convenient. If there are positive and negative numbers with the same, maximal absolute value, continue.
- Determine whether the majority of numbers are positive and negative, ignoring zeroes. If a majority of numbers are positive, output
FALSE. If a majority of numbers are negative, output
- Otherwise, output may be
You might be given only one number. In that case, it has the highest absolute value, so if it is greater than 0 output false; if it is less than zero output true; and if it is 0 you may output true or false.
You may not assume the numbers are sorted.
5, 4, 3 -> FALSE # #3 1, 0, -1 -> TRUE or FALSE # #4 0, 1, 2, 0 -> FALSE # #5 -2, -1, -2 -> TRUE # #6 3, 1, 4, 1, 5, -9 -> TRUE # Largest absolute value below 0, rule 1 3, 1, 4, 1, 5, -5 -> FALSE # Largest absolute value tied, determined by majority, rule 2 3, 1, -3, -1, 5, -5 -> TRUE or FALSE # Largest absolute value tied and no majority, unspecified