| Bytes | Lang | Time | Link |
|---|---|---|---|
| 010 | APLDyalog Unicode | 240925T075554Z | akamayu |
| 014 | K ngn/k | 240925T072158Z | akamayu |
| 007 | Jelly | 240923T011922Z | Unrelate |
| 006 | Nekomata | 240924T070755Z | alephalp |
| 045 | Python + NumPy | 240923T070826Z | Albert.L |
| 010 | Uiua | 240923T214005Z | nyxbird |
| 110 | Python | 240923T042739Z | Unrelate |
| 113 | Python3 | 240923T015712Z | Ajax1234 |
| 016 | Charcoal | 240923T045516Z | Neil |
K (ngn/k), 14 bytes
Naive implementation. It takes \$\sigma\$ as a 0-indexed premutation vector.
{y@2/(2\!#y)x}
Jelly, 7 bytes
UŒPṬỤỤị
Port of alephalpha's Nekomata answer.
U Reverse p,
ŒP shortest-first powerset,
Ṭ untruth (surprised this vectorizes!),
ỤỤ double grade up (convert to permutation),
ị index into s.
Jelly, 9 8 bytes
ịⱮL2ṗɗỤị
Pretty naive implementation. Takes a 1-indexed permutation list \$[\sigma(1), ..., \sigma(n)]\$ on the left, and the string \$s\$ on the right.
2ṗ Raise [1, 2] to the Cartesian power of
L the length of the permutation (= n),
ịⱮ ɗ and index the permutation into each result.
Ụ Grade that up (argsort),
ị and index into s.
Nekomata, 6 bytes
↔SĦaõ@
Takes the permutation as a 0-indexed list, e.g., [1,2,0] for \$\begin{pmatrix}0&1&2\\1&2&0\end{pmatrix}\$. Takes the binary string as a list of integers, e.g., [1,1,0,1,0,0,1,0] for 11010010.
↔SĦaõ@ Take [1,2,0] [1,1,0,1,0,0,1,0] as an example
↔ Reverse
-> [0,2,1]
S Find a subset
-> [] [0] [2] [0,2] [1] [0,1] [2,1] [0,2,1]
Ħ Histogram
-> [] [1] [0,0,1] [1,0,1] [0,1] [1,1] [0,1,1] [1,1,1]
a All possible values
-> [[],[1],[0,0,1],[1,0,1],[0,1],[1,1],[0,1,1],[1,1,1]]
õ Ordering
-> [0,2,4,6,1,3,5,7]
@ Index into the second input
-> [1,0,0,1,1,1,0,0]
Python + NumPy, 45 bytes
-5 thanks @att
lambda a,p:a.reshape(p*0+2).transpose(p).flat
Expects the binary string and the permutation as NumPy arrays and returns a NumPy flatiter object.
How?
Quite literal approach: Arranges the input sequence into a 2x2x...x2 hypercube, shuffles dimensions according to permutation, and flattens the result.
Uiua, 10 bytes
⊏⍜⊙⋯≡⊏¤⊙°⊏
Takes input with the permutation as a 0-indexed array of indices, followed by the binary string.
⊏⍜⊙⋯≡⊏¤⊙°⊏
⊙°⊏ # create an array of indices
⍜⊙⋯ # convert to binary
≡⊏¤ # permute the bits
⍜⊙⋯ # convert back to numbers
⊏ # index into s
Python, 113 110 bytes
lambda s,p,n:[s[i]for i in g(range(n),*p[::-1])]
g=lambda s,h,*t:sorted(t and g(s,*t)or s,key=lambda i:i>>h&1)
-3 thanks to Karl
Probably golfable and loses to a more direct approach, but this one is just too much fun. (2*&ÞƒJ’$}‘ị for 11 in Jelly--thanks 1-indexing...) Takes an inverse 0-indexed permutation list \$[\sigma^{-1}(0 ),...,\sigma^{-1}(n-1)]\$, where the inverse is equivalently the grade-up, alongside \$2^n\$.
Since sorted is stable, this sorts by each bit in permutation order, essentially "bubbling" that bit to most significant (w.r.t. the destination indices) before being "pushed down" by the next.
A little more digestible:
lambda s,p,n:[s[i]for i in reduce(lambda x,h:sorted(x,key=lambda i:i>>h&1),p,range(n))]
from functools import*
Python3, 113 bytes
lambda n,s,o:''.join(s[int(''.join(x for _,x in sorted([*zip(o,bin(j)[2:].zfill(n))])),2)]for j in range(len(s)))
Charcoal, 16 bytes
⭆θ§θΣEη∧&κX²μX²λ
Try it online! Link is to verbose version of code. Takes s as the first input as a list or bitstring and σ as the second input as a list (or string if n<11) of 0-indexed destination indices and outputs a bitstring. Explanation:
θ First input `s`
⭆ Map over characters and join
θ First input `s`
§ Indexed by
η Second input `σ`
E Map over indices
κ Outer index
& Bitwise and
² Literal integer `2`
X Raised to power
μ Source index
∧ Logical And
² Literal integer `2`
X Raised to power
λ Destination index
Σ Take the sum
Implicitly print
(If you run this code on ATO instead of TIO then you can also pass σ as a dictionary mapping 0-indexed source indices to destination indices.)