The shape of the melody
- O(n) · Medium
- Full plan
- Python
- JavaScript
- lists
- strings
- comparisons
Problem
You do not need the notes to recognize a song: it is enough to know whether each one goes up, goes down or repeats. That outline is called the contour, and it is how apps find a song when you hum it.
Write a function that takes a list of note pitches, as whole numbers, and returns a string with one character for each note from the second one on, compared with the note right before it: "+" if it goes up, "-" if it goes down and "=" if it repeats.
With [60, 62, 62, 59, 64]: 62 goes up, 62 repeats, 59 goes down and 64 goes up, so you return "+=-+". The string is one shorter than the list. With a single note, or an empty list, you return the empty string "".
Examples
The example
[60, 62, 62, 59, 64] → "+=-+"
Always going down
[72, 71, 69, 67] → "---"
The same note
[64, 64, 64] → "=="
Two notes
[55, 57] → "+"
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def contour(notes):
passJavaScript
function contour(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.