Can the Tune be Played?
A broken musical keyboard has keys labelled with positive integers. It is broken in two ways:
- It takes a long time to process key presses: after pressing the key labelled with the number \$n\$, there is a gap of \$n\$ seconds before the \$n\$th note is heard.
So, for example, the \$5\$th key must be pressed \$5\$ seconds early for its note to sound in the right place.
- Only one key can be pressed at a time.
Because of these problems, some tunes cannot be played on the keyboard. To understand why, let us first define a tune:
A tune will be defined as a list of positive integers representing the order in which notes should be heard (not the order in which keys should be pressed). A number \$n\$ represents the note heard when the \$n\$th note on the keyboard is pressed. This definition does not allow for rests, chords or notes of differing lengths, so you can imagine that all notes are played at a speed of exactly one note per second.
Invalid Tune Example
An example of a tune would be
[3, 1, 2]. This means that the note \$3\$ should be heard, then, one second later, the note \$1\$, and a second after that, the note \$2\$.
However, when trying to play this tune on the keyboard, there is a problem. To understand why, shift each of the numbers \$n\$ in the tune back by \$n\$ spaces. The result represents the order in which keys must be pressed for the notes to sound in the correct place:
Tune [ 3 , 1 , 2] Index -3 -2 -1 0 1 2 How keys would be pressed [3 , 1&2 ]
The problem here is that keys \$1\$ and \$2\$ must be pressed at the same time for their notes to sound in the right place, but it is impossible to press two keys at once on the keyboard. Therefore, the tune
[3, 1, 2] cannot be played.
Valid Tune Example
An example of a valid tune would be
[2, 1, 3]. To see why, shift the numbers back to find out when the keys must be pressed:
Tune [ 2 , 1 , 3] Index -2 -1 0 1 2 How keys would be pressed [2 , 3 , 1 ]
Having shifted each of the numbers back (\$2\$ moved back \$2\$ spaces, \$1\$ moved back \$1\$ space and \$3\$ moved back \$3\$ spaces), none of them have landed in the same position. Therefore, this tune can be played on the broken keyboard: the keys would be pressed in the order
[2, 3, 1].
Your task is to write a program which takes as input a list representing a tune, and outputs a truthy/falsy value depending on whether or not the tune can be played on the broken keyboard.
- You can assume that input lists will always contain only positive integers.
- You can assume that input lists will always have at least one element.
- You can assume that inputs will always be lists.
- Standard loopholes are forbidden.
[1, 2, 3] -> False [3, 1, 2] -> False [3, 2, 1] -> True [6, 4, 7, 3, 5, 2, 1] -> True [4, 7, 6, 5, 2, 1, 3] -> False // 6 and 4 land in same position [4, 6, 4, 2, 1, 4] -> False [2, 1, 6, 4, 4, 4] -> False // 4 and 1 [2, 1, 6, 4, 2, 4] -> True
This is code-golf so the shortest answer (as measured in bytes) wins!