| Bytes | Lang | Time | Link |
|---|---|---|---|
| 013 | Wolfram Language Mathematica | 231006T085042Z | att |
| 006 | Nekomata | 231011T035405Z | alephalp |
| 037 | Julia | 231013T163522Z | MarcMush |
| 001 | Uiua | 231005T142022Z | chunes |
| 042 | Charcoal | 231005T095506Z | Neil |
| 051 | Python 3 | 231005T055434Z | Command |
| 033 | Python 2 | 231005T180959Z | loopy wa |
| 023 | R | 231005T074035Z | Nick Ken |
| 021 | 05AB1E | 231005T091609Z | Kevin Cr |
Wolfram Language (Mathematica), 13 bytes
#0/@Thread@#&
Input [array]. Recursively Threads the first dimension through all other dimensions. Thread behaves more nicely than \[Transpose] on arrays of dimension <2.
Wolfram Language (Mathematica), 23 bytes
D'[[0]][,{##},Listable]
Taking a rare chance to showcase Function's 3-argument form, which lets us set attributes on a pure function, even if this ends up being almost twice the length of the shortest solution.
Input [array...]. Append @@#& to take [array] input.
A Listable function that enlists its arguments. D'[[0]] is slightly shorter than writing out Function.
Transpose received additional functionality in version 14.1 that trivializes this task: 15 bytes
#~Transpose~-1&
...but 13<15 anyways.
Nekomata, 6 bytes
ʷ∑→ᵉbD
ʷ∑→ᵉbD Take [[[1,2],[3,4]],[[5,6],[7,8]]] as an example
ʷ∑ Sum of the nested array
[[[1,2],[3,4]],[[5,6],[7,8]]] -> 36
→ Increment
36 -> 37
ᵉb Convert the input from a list of digits in that base
[[[1,2],[3,4]],[[5,6],[7,8]]], 37 -> [[42,80],[118,156]]
D Convert each number back to a list of digits in that base
[[42,80],[118,156]], 37 -> [[[1,5],[2,6]],[[3,7],[4,8]]]
This works because b and D are vectorized in different ways.
b (\fromBase) is not vectorized by itself. It simply views the input as a list of digits in that base. However, when converting the list of digits to a number, the addition and multiplication are vectorized. For example [[[1,2],[3,4]],[[5,6],[7,8]]] 37 b is [[1,2],[3,4]]*37+[[5,6],[7,8]]=[[42,80],[118,156]].
D (\toBase) is vectorized in the usual way. It converts each number in the input to a list of digits in that base. For example [[42,80],[118,156]] 37 D converts each number (42,80,118,156) to a list of digits in base 37, which is [[[1,5],[2,6]],[[3,7],[4,8]]].
Uiua, 1 byte
⍉
(When reading the output, dashes on the left indicate the rank of the array and whitespace separates axes.)
Charcoal, 42 bytes
⊞υθFυ«≔E⌊ιEι§μληWι≔⊟ικFη⊞ικFη¿⁺⟦⟧⌊κ⊞υκ»⭆¹θ
Try it online! Link is to verbose version of code. Explanation: Port of @CommandMaster's Python 3 answer.
⊞υθ
Start by processing the input array.
Fυ«
Loop over the arrays that need to be transposed.
≔E⌊ιEι§μλη
Transpose the top two dimensions of the current array.
Wι≔⊟ικFη⊞ικ
Replace the contents of the original array with that of the transposed array, thus transposing the array in-place.
Fη¿⁺⟦⟧⌊κ⊞υκ
If the elements of the transposed arrays are themselves arrays rather than lists of numbers, then push those arrays so that they get transposed in-place.
»⭆¹θ
Pretty-print the final state of the original array.
41 bytes using the newer version of Charcoal on ATO:
W⁺⟦⟧ΣΣθ«⊞υL⌊⌊θUMθΣκ»≔E⌊θEθ§λκθF⮌υ≔⪪θιθ⭆¹θ
Attempt This Online! Link is to verbose version of code. Explanation:
W⁺⟦⟧ΣΣθ«
Until the input array has two dimensions, ...
⊞υL⌊⌊θ
... record the number of elements in the third dimension, and...
UMθΣκ
... flatten the third dimension into the second dimension.
»≔E⌊θEθ§λκθ
Transpose the array.
F⮌υ
Loop over the sizes of the dimensions.
≔⪪θιθ
Unflatten the array.
⭆¹θ
Pretty-print the final array.
32 bytes by taking the dimensions of the array as a second argument:
F✂η²UMθΣκ≔E⌊θEθ§λκθF⮌✂η²≔⪪θιθ⭆¹θ
Attempt This Online! Link is to verbose version of code. Explanation: As above, but using the input dimensions, ignoring the first two which aren't needed.
26 bytes by taking a list of dimensions and a flattened array as I/O:
F…θ¹I⟦⊞OΦθλιEη§§⪪η÷Lηιλ÷λι
Try it online! Link is to verbose version of code. Explanation:
θ Input dimensions
… Truncated to length
¹ Literal integer `1`
F Loop over dimension
θ Input dimensions
Φ Filtered where
λ Not first dimension
⊞O Concatenated with
ι First dimension
η Input array
E Map over elements
η Input array
⪪ Split into arrays of length
η Input array
L Length
÷ Integer divided by
ι First dimension
§ Cyclically indexed by
λ Current index
§ Indexed by
λ Current index
÷ Integer divided by
ι First dimension
⟦ Separate dimensions from elements
I Implicitly print
F…θ¹ saves a byte over ≔§θ⁰ι and two bytes over repeating §θ⁰ instead of ι.
(Length of array could also be product of dimensions of course.)
Python 3, 51 bytes
f=lambda a,b:b<2and[*a]or[f(x,b-1)for x in zip(*a)]
Takes as input the list and then the dimension.
If we look at the list of dimensions, a 2D transpose is equivalent to swapping the first two, so if we recursively do that we swap dimensions 0,1, then 1,2, and so on, so we move the first dimension to the last position.
If the output can contain have tuples in the topmost level it can be -1 by replacing and[*a]or with and a or.
Python 3 + numpy, 37 36 bytes
-1 thanks to @loopwalt
lambda a,b:a.transpose(range(1-b,1))
Takes a numpy array and the dimension as input.
Python 2, 33 bytes
f=lambda*L:L*(L<(f,))or map(f,*L)
Ports @Command Masters Python 3 (non numpy). Takes splatted input. Innermost lists will be tuples.
R, 23 bytes
\(x,d)aperm(x,1:d%%d+1)
A situation where R has a built-in that meets the requirements! A function that takes a multidimensional array and the number of dimensions as its two parameters and returns a multidimensional array with the coordinates transposed left one.
Note that in the printout R loops through dimensions that are 3 or above, printing a 2d matrix for each 2d plane. Within that matrix, rows are dimension 1 and columns are dimension 2.
Thanks to @Giuseppe for suggesting a fix that works for 1-dimensional arrays.
05AB1E, 21 bytes
JΔ€`}\N<Ý®K"€"×'ø«J.V
Try it online or verify all test cases.
Explanation:
J # Join each inner-most list together to a single item
Δ€`}\N< # Then determine the depth-2 of this multidimensional list
Δ } # Loop until it no longer changes:
€` # Flatten it one level down
\ # After the changes-loop, discard the resulting flattened list
N # Push the last 0-based index
< # Decrease it by 1
Ý # Push a list in the range [0,depth-2]
®K # Remove -1 so [0,-1] becomes [0]
"€"× # Convert each value in this list to that many "€" as string
'ø« '# Append an "ø" to each string
J # Join this list of strings together
.V # Evaluate and execute it as 05AB1E code on the (implicit) input
# (after which the result is output implicitly)
Try all test cases without the .V to see which maps/transpose builtins are actually being generated by JΔ€`}\N<Ý®K"€"×'ø«J, where € is a map (and therefore €€ a nested map) and ø transposes a matrix/2D-list, swapping its rows/columns.
Minor notes:
- Using
Δ€`}\NÍ(whereÍis -2) only seems to work for the last test case: try it online; and with<but without the leadingJit works for some of the test cases, but not the last two: try it online; so using a leadingJand>works for all test cases. - The range builtin
Ýalso accepts negative values for its \$[0,n]\$-ranged list. So for singular lists, it'll result in[0,-1]after both theJandΔ€`}\N<, for which the-1are ignored by the×and it basically evaluatesø-1ø: try it online. - Why use
"€"for string€but'øfor stringø? Because'is also used for pushing a dictionary word, and'€×is apparently the dictionary wordview: try it online; whereas'ø«is coincidentally none: try it online.