Skip to the content

The scrambled message

Problem

Inspector Ruiz found the note under the door, its words all jumbled. Whoever wrote it hid a digit inside each word that tells its place in the message.

Write a function that takes the message, a string of words separated by one space, and returns the same string with the words in order: the one with the 1 first, then the one with the 2, and so on. Each word has exactly one digit, anywhere in it; the digits go from 1 up to the number of words, with no repeats, so there are 9 words at most. The words come out just as they arrived, with their digit and their capitals, separated by one space. If the message is empty, you return "".

With "arri3ves Th1e mid5night ship2 a4t" you return "Th1e ship2 arri3ves a4t mid5night".

Examples

  • The example

    "arri3ves Th1e mid5night ship2 a4t" → "Th1e ship2 arri3ves a4t mid5night"

  • The digit at the start, middle or end

    "do3or back2 1The" → "1The back2 do3or"

  • Already in order

    "Nobody1 kn2ows it3" → "Nobody1 kn2ows it3"

  • A single word

    "cl1ue" → "cl1ue"

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

You start with this

Python

def order_message(message):
    pass

JavaScript

function orderMessage(message) {
}
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) challenges

See all challenges →