Skip to the content

The alternating choir

Problem

For the winter concert, the director lines the choir up in a single row and alternates the voices: first the lowest, then the highest, then the second lowest, the second highest, and so on toward the middle.

Write a function that takes notes, a list of positive integers (each singer's note; it may be empty and may repeat notes), and returns a new list with the notes in that order. A smaller number is a lower voice. Each singer appears exactly once: with an odd number of singers, the one in the middle ends up last, alone. Two singers with the same note take two places in the row.

With [5, 1, 4, 2, 3] return [1, 5, 2, 4, 3]. With an empty list, [].

Examples

  • The example

    [5, 1, 4, 2, 3] → [1, 5, 2, 4, 3]

  • Four voices

    [67, 60, 72, 64] → [60, 72, 64, 67]

  • Repeated notes

    [4, 4, 1, 1] → [1, 4, 1, 4]

  • Seven voices

    [12, 3, 7, 20, 15, 9, 1] → [1, 20, 3, 15, 7, 12, 9]

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def alternate(notes):
    pass

JavaScript

function alternate(notes) {
}
Solve this challenge

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.

More O(n log n) challenges

See all challenges →