Post History
BQN, 8 bytesSBCS ⍒↕+2|⊢+↕ Run online! The solution itself is an 8-train, so its evaluation order is given by the sequence 7 5 6 3 4 1 2 0: first the rightmost ↕, then the ⊢, then the + between...
Answer
#1: Initial revision
# [BQN](https://mlochbaum.github.io/BQN/), 8 bytes<sup>[SBCS](https://github.com/mlochbaum/BQN/blob/master/commentary/sbcs.bqn)</sup> ``` ⍒↕+2|⊢+↕ ``` [Run online!](https://mlochbaum.github.io/BQN/try.html#code=VHIg4oaQIOKNkuKGlSsyfOKKoivihpUKCiMgQXBwbHkgdG8gMeKApjEwIGFuZCBhbGlnbiB2ZXJ0aWNhbGx5CuKJjcuYIFRywqggMSvihpUxMA==) The solution itself is an 8-[train](https://mlochbaum.github.io/BQN/doc/train.html), so its evaluation order is given by the sequence `7 5 6 3 4 1 2 0`: first the rightmost `↕`, then the `⊢`, then the `+` between them, and so on—it's fully expanded below. Although it's organized as a train, the computation itself doesn't take advantage of BQN's support for trains. ``` ⍒↕+2|⊢+↕ # Tacit function (suppose 𝕩=3) ↕ # Range of 𝕩 0‿1‿2 ⊢ # 𝕩, unchanged 3 + # Add 3‿4‿5 2 # 2 | # Remainder mod ^ 1‿0‿1 ↕ # Range again 0‿1‿2 + # Add 1‿1‿3 ⍒ # Descending grade 2‿0‿1 ``` The idea is to start with ascending indices, then group them into mostly-twos by adding one to the first part of each pair. We do this by taking indices modulo 2. However, this means the first pair starts on element index 1—in english, the second element—which is correct for even lengths but wrong for odd lengths. So we flip the odd lengths by adding `𝕩` to the range. Subtracting would also work, in either order: perhaps a more intuitive version is `⊢-↕`, which is the distance from each position to one after the end. After doing this we have a number for each function, and the ordering goes from high to low numbers but left to right for functions with the same number. Descending [Grade](https://mlochbaum.github.io/BQN/doc/order.html#grade) does exactly this.