19
2
Challenge
Given a n-dimensional array of integers and a permutation of the first n natural numbers, permute the array dimensions accordingly.
Details
This challenge is inspired by MATLABs permute. demonstration
The permutation is given as a list of integers, e.g. [1,3,2] means 1 gets mapped to 1, 2 gets mapped to 3 and 3 gets mapped to 2 (here the ith entry is the value i gets mapped to). But you can use other formats that are convenient, for instance as cycles or as a function. If it is more convenient you can also use 0-based indexing.
The array can be assumed to be a full "rectangular" m1 x m2 x ... x mn-array (i.e. you can assume it is not ragged/jagged).
You can assume that n is not too large, as many languages have a limit of the number of dimensions in a nested array.
If your language does not support multidimensional arrays, you can also take a string that represents the array as input.
Examples
- Any
n-dimensional array with the identity permutation[1,2,3,...,n]will be unchanged. - The array
[[10,20,30],[40,50,60]]with the permutation[2,1]gets mapped to[[10,40],[20,50],[30,60]]. - The array
[[[1,2],[3,4]],[[5,6],[7,8]]]with the permutation[2,3,1]gets mapped to[[[1,3],[5,7]],[[2,4],[6,8]]].
You do not need parentheses for calling
– Jonathan Frech – 2018-01-06T05:42:13.977exec(saving two bytes), as it is a statement in Python 2.There also is a superfluous space in
z(p) for. – Jonathan Frech – 2018-01-06T05:55:10.3731
Used
– Mr. Xcoder – 2018-01-06T09:31:32.790map(z,l),s%jandprintfor 301 bytes –– Try it online!