17
\$\begingroup\$

Given a multidimensional, rectangular array and a list of dimensions, such as:

[ [1, 2],
  [3, 4] ]

[3, 4]

Your challenge is to extend the matrix to the dimensions given in the list.

To extend an array to a length in a single direction, simply repeat its elements; for example, if we want to extend [1, 2, 3] to length 7, the result would be [1, 2, 3, 1, 2, 3, 1].

For example, with the above, we can extend it to length 3:

[ [1, 2],
  [3, 4],
  [1, 2] ]

Then extend each row to length 4:

[ [1, 2, 1, 2],
  [3, 4, 3, 4],
  [1, 2, 1, 2] ]

However, your code should work for arrays with arbitrary dimensions. For example, if we take the 3D array

[ [ [1, 2],
    [3, 4] ],
  [ [5, 6],
    [7, 8] ] ]

And dimensions [3, 3, 5], the result is:

[ [ [1, 2, 1, 2, 1],
    [3, 4, 3, 4, 3],
    [1, 2, 1, 2, 1] ],
  [ [5, 6, 5, 6, 5],
    [7, 8, 7, 8, 7],
    [5, 6, 5, 6, 5] ],
  [ [1, 2, 1, 2, 1],
    [3, 4, 3, 4, 3],
    [1, 2, 1, 2, 1] ] ]

You can assume that the dimensions will be strictly greater than the corresponding dimensions of the array, and that there will be the same amount as the list has dimensions. You may take dimensions in reverse.

This is , shortest wins!

Testcases

[[1, 2], [3, 4]], [3, 4] -> [[1,2,1,2],[3,4,3,4],[1,2,1,2]]
[[[1,2],[3,4]],[[5,6],[7,8]]], [3, 3, 5] -> see above
[1, 2, 3], [5] -> [1, 2, 3, 1, 2]
[[4], [3], [2]], [4,3] -> [[4,4,4],[3,3,3],[2,2,2],[4,4,4]]
\$\endgroup\$
4
  • 7
    \$\begingroup\$ Can't wait for the APL/BQN answer to be like 1-4 bytes long lmao \$\endgroup\$
    – lyxal
    Commented Jun 19, 2022 at 7:25
  • \$\begingroup\$ ... and for Wolfram to have the obligatory builtin (remember upgoat?). \$\endgroup\$
    – ojdo
    Commented Jun 19, 2022 at 22:32
  • \$\begingroup\$ can i take the input as a single list, with the matrix as the last element? \$\endgroup\$
    – Jonah
    Commented Jun 20, 2022 at 3:11
  • \$\begingroup\$ @Jonah That seems reasonable \$\endgroup\$
    – emanresu A
    Commented Jun 20, 2022 at 3:36

13 Answers 13

11
\$\begingroup\$

BQN, 13 8 bytesSBCS

A bit closer to 4 bytes. Takes the array as left argument and the shape vector on the right.

(⍉⁼⥊⎉1)´

Run online! <- This uses the new array notation using [].

The reduction ´ iterates over the shape vector from right to left, using the large array as a starting value. ⥊⎉1 resizes the trailing axis by cycling elements, then ⍉⁼ rotates this axis to the front.

\$\endgroup\$
3
  • \$\begingroup\$ I think you can remove \$\endgroup\$
    – Adám
    Commented Jun 19, 2022 at 9:39
  • \$\begingroup\$ @Adám that would rotate the axes in the wrong direction (see [[[1]],[[2]]] F [2,3,4] for an example where that matters) \$\endgroup\$
    – ovs
    Commented Jun 19, 2022 at 9:58
  • \$\begingroup\$ Probably not worth a separate answer, but here's the J translation, 4 more bytes assuming i can take everything as a single list of boxes: (0|:$"1)&.>/ Try it online! \$\endgroup\$
    – Jonah
    Commented Jun 20, 2022 at 3:15
7
\$\begingroup\$

Wolfram Language (Mathematica), 14 bytes

##~PadRight~#&

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Factor + sequences.repeating, 65 63 bytes

[ [ swap '[ _ cycle ] [ '[ _ map ] ] repeat call ] each-index ]

Try it online!

This is very unlike how my Factor golfs usually turn out (what with constructing multiple functions and calling them in turn). I noticed that every solution follows this pattern:

  • [[1, 2], [3, 4]], [3, 4] ->

    3 cycle [ 4 cycle ] map

  • [[[1,2],[3,4]],[[5,6],[7,8]]], [3, 3, 5] ->

    3 cycle [ 3 cycle ] map [ [ 5 cycle ] map ] map

So curry each dimension into the innermost quotation with cycle, then build up the nested map quotations around it based on its index in the list of dimensions and call it. Since recursion is extremely verbose in Factor, this iterative approach is all but guaranteed to be shorter.

\$\endgroup\$
4
\$\begingroup\$

Python 3, 51 bytes

f=lambda m,d:d and[*map(f,m*d[0],d[0]*[d[1:]])]or m

Try it online!

\$\endgroup\$
4
\$\begingroup\$

05AB1E, 7 bytes

„∍ε«J.V

First input is the list of lengths; second the matrix.

Try it online or verify all test cases.

Explanation:

„∍ε        # Push string "∍ε"
   «       # Append it to each integer of the first (implicit) input-list
    J      # Join this list together to a single string
     .V    # Evaluate and execute it as 05AB1E code
           # (after which the result is output implicitly)

is the extend buildin, which does exactly what this challenges asks: given a list and integer arguments, extend the list to the given integer's length. ε is the map builtin, which stays open until we close it with } or ] (which we don't want to do in this challenge).

For example: „∍ε«J will convert the list of lengths [3,3,5] from the example of the challenge to string "3∍ε3∍ε5∍ε". Evaluated and executed as 05AB1E code, this will:

3∍         # Extend the (implicit) 3D input-matrix to 3 inner matrices
  ε        # Map over each inner matrix:
   3∍      #  Extend the matrix to 3 inner rows
     ε     #  Map over each inner row:
      5∍   #   Extend the row to 5 values
        ε  #   No-op map

Luckily '∍ε isn't a dictionary word, otherwise the „∍ε would had to be "∍ε" instead.

\$\endgroup\$
3
\$\begingroup\$

Charcoal, 36 bytes

⊞υθFη«≔⟦⟧ζFυ«≔⁺ζκζF…κ⁻ιLκ⊞κλ»≔ζυ»⭆¹θ

Try it online! Link is to verbose version of code. Explanation:

⊞υθ

Start by considering the first dimension.

Fη«

Loop over the new dimensions.

≔⟦⟧ζ

Start collecting the elements of that dimension.

Fυ«

Loop over all of the lists of that dimension.

≔⁺ζκζ

Collect the elements of the list.

F…κ⁻ιLκ⊞κλ

Cyclically append sufficient elements to reach the desired length.

»≔ζυ

Save the elements as the list of lists of the next dimension.

»⭆¹θ

Pretty-print the expanded array.

\$\endgroup\$
3
\$\begingroup\$

Python 3 with numpy, 59 57 56 bytes

lambda m,d:tile(m,d)[[*map(slice,d)]]
from numpy import*

Try it online!

−2 thanks to loopy walt, and another −1 by changing back to the deprecated use of a list for indexing.

\$\endgroup\$
1
  • \$\begingroup\$ -2 using map instead of comprehension. \$\endgroup\$
    – loopy walt
    Commented Jun 19, 2022 at 18:14
3
\$\begingroup\$

R, 62 55 51 bytes

Edit: -7 bytes thanks to Giuseppe, and -4 bytes thanks to pajonk

\(a,d,`*`=array)a[t(t(which(T*d,T)-1)%%dim(a)+1)]*d

Attempt This Online!


R, 49 bytes

\(a,d)array(a[t(t(which(!a*0,T)-1)%%dim(a)+1)],d)

Attempt This Online!

Works with arrays of finite numbers. In this case, we don't need to create a new array of TRUE elements to get the indices, as we can just multiply the elements by zero and take the logical NOT.

\$\endgroup\$
3
  • \$\begingroup\$ @Giuseppe - Oh, much better, what was I thinking...? Thanks! \$\endgroup\$ Commented Jun 23, 2022 at 15:23
  • \$\begingroup\$ -4 bytes by reassigning array. \$\endgroup\$
    – pajonk
    Commented Jun 24, 2022 at 4:48
  • \$\begingroup\$ @pajonk - Nice, thanks! \$\endgroup\$ Commented Jun 24, 2022 at 6:47
2
\$\begingroup\$

BQN, 22 bytesSBCS

{⟨⟩𝕊𝕩:𝕩;(1↓𝕨)⊸𝕊¨𝕩⥊˜⊑𝕨}

Run online!

recursive is shorter ¯\_(⌾‿⌾)_/¯

BQN, 26 bytesSBCS

{𝕩{a‿b𝕊𝕩:a⥊⚇b𝕩}´𝕨⋈¨⌽1+↕≠𝕨}

Run online!

\$\endgroup\$
2
  • 3
    \$\begingroup\$ Dang it razetime I said 1-4 bytes not 26 of them. Smh \$\endgroup\$
    – lyxal
    Commented Jun 19, 2022 at 7:44
  • 1
    \$\begingroup\$ maybe there is a smarter way lmao \$\endgroup\$
    – Razetime
    Commented Jun 19, 2022 at 7:48
2
\$\begingroup\$

Brev, 99 bytes

(define(j l . m)(j((over(apply j x(cdr m)))l)(car m)))
(define(j l k)(take(apply circular-list l)k))

(Displayed with one unnessecary newline for clarity.)

Example usage:

(pp (j '(((1 2) (3 4)) ((5 6) (7 8))) 3 3 5))
\$\endgroup\$
2
\$\begingroup\$

jq, 58 bytes

def r($r):[.[range($r[0])%length]|r($r[1:])? //.];r(input)

Try it online! ~ Attempt This Online! ~ jqplay.org

\$\endgroup\$
1
\$\begingroup\$

Pip -xp, 20 bytes

aH:PObb?fMZa[b]RL#aa

Attempt This Online!

Explanation

Kinda ugly, too bad Pip doesn't have any way to do currying or partial application.

aH:PObb?fMZa[b]RL#aa
     b                ; Second argument, the list of dimensions
   PO                 ; Pop the first element (call it N)
a                     ; First argument, the matrix
 H:                   ; Take the first N elements (cyclically) and assign back to a
      b?              ; Is b still nonempty?
                      ; If so:
           a          ;   Take the array
            [b]       ;   and a list containing b
               RL#a   ;   repeated len(a) times
         MZ           ;   Zip together and map to each pair
        f             ;   a recursive call to the main function
                   a  ; If not, just return a
\$\endgroup\$
1
\$\begingroup\$

Uiua SBCS, 8 bytes

∧(⍉↙⊃∘▽)

Try it!

Pretty much the same thing as ovs' BQN answer.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.