The Winters one
- O(2ⁿ) · Legendary
- Full plan
- SQL
- recursion
- strings
- window functions
Problem
At the front desk nobody says a whole title. "Is the Winters one back yet?" someone asks, and everybody knows which book that is. The librarian wants that word written on each book's card: the longest word in the title, which is almost never an article. If two words tie, it's the one that comes first: for "The Train South," Train.
Show each book's title and its word, in that order, sorted alphabetically by title. The word goes exactly the way it's written in the title. A one-word title gets the whole thing, and if two books have the same title, each one gets its own row.
In books, the words of a title are separated by a single space, and no title starts or ends with one. SQL has no function that splits text into words.
What it has to return
These rows, in this order
| title | word |
|---|---|
| Long Night | Night |
| Loose Leaves | Leaves |
| Maps of Salt | Maps |
| Short Stories | Stories |
| The Blue House | House |
| The Lighthouse | Lighthouse |
| The Train South | Train |
| Three Winters | Winters |
This is what comes out with the data in the practice database this challenge uses. The column names don’t count, so name yours however you like: what counts is how many there are and the order they come in. Besides this one, the challenge has hidden tests with other data that are revealed when you submit your solution.
You start with this
SQL
SELECT title,
LENGTH(title) AS letters
FROM books
ORDER BY titleIt 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.