5 ms·
Here's my BQN solution. It uses shift functions « and Group ⊔ (links below) where APL would use windowed reduction (e.g. 2≠/d) and partitioned enclose (r←b⊂𝕩)
by mlochbaum 5y ago
Here's my BQN solution. It uses shift functions « and Group ⊔ (links below) where APL would use windowed reduction (e.g. 2≠/d) and partitioned enclose (r←b⊂𝕩) so it's slightly different.
MonoRuns ← {
d ← <⟜« 𝕩 # Direction
b ← <`1‿0»≠⟜«d # Boundaries between runs
r ← (¯1+`b) ⊔ 𝕩 # Runs
(¬b/d) ⌽∘⊢⍟⊣¨ r # Reverse the descending ones
}
Run online: https://mlochbaum.github.io/BQN/try.html#code=TW9ub1J1bnMg4oaQIHsKICBkIOKGkCA84p+cwqsg8J2VqSAgICAgICAgICAjIERpcmVjdGlvbgogIGIg4oaQIDxgMeKAvzDCu+KJoOKfnMKrZCAgICAgIyBCb3VuZGFyaWVzIGJldHdlZW4gcnVucwogIHIg4oaQICjCrzErYGIpIOKKlCDwnZWpICAgICMgUnVucwogICjCrGIvZCkg4oy94oiY4oqi4o2f4oqjwqggciAgICAjIFJldmVyc2UgdGhlIGRlc2NlbmRpbmcgb25lcwp9CgriiY3LmCBNb25vUnVuc8KoIOKfqAogIOKfqOKfqQogIOKfqDLin6kKICDin6g0LDHin6kKICAx4oC/MuKAvzPigL8y4oC/MeKAvzjigL804oC/NeKAvzbigL83CiAgwq8x4oC/wq8y4oC/MeKAvzLigL8z4oC/MuKAvzEK4p+p https://mlochbaum.github.io/BQN/try.html#code=TW9ub1J1bnMg4o...
https://mlochbaum.github.io/BQN/doc/shift.html https://mlochbaum.github.io/BQN/doc/shift.html
https://mlochbaum.github.io/BQN/doc/group.html https://mlochbaum.github.io/BQN/doc/group.html
- eesmith 5y agoNeat link to working code! "⟨3, 2, 1⟩" generates "⟨ 2 1 3 ⟩". I expected "⟨1, 2, 3⟩".
- mlochbaum 5y agoGood spot, edited in a fix. I had ⌽ instead of ⌽∘⊢ before, which would end up rotating runs by 1 instead of reversing them (downsides of ambivalent functions).
- eesmith 5y ago⟨1, 1, 2⟩ goes to ⟨ ⟨ 1 1 ⟩ ⟨ 2 ⟩ ⟩. I expected ⟨ ⟨1, 1, 2⟩ ⟩ This task is surprisingly tricky!
- mlochbaum 5y agoThat's by design (or rather, I knew it would do this and took the vague "ascending" and "descending" in the description to mean adjacent elements wouldn't be equal). Using < in d means ascending runs are strict but descending ones aren't. Probably better to use ≤ to swap these. That would match the run-splitting scheme used by Timsort.