23
\$\begingroup\$

Given two arrays of non-negative integers \$A = [A_1,A_2,\ldots,A_n]\$ and \$R = [R_1,R_2,\ldots,R_n]\$ which are equal in length, return an array which has the element \$A_1\$ repeated \$R_1\$ times, then element \$A_2\$ repeated \$R_2\$ times, all the way up to \$A_n\$.

Standard loopholes are forbidden. As this is , the shortest program wins.

Test Cases

\$A\$ \$R\$ Output
[1,2,3] [1,2,3] [1,2,2,3,3,3]
[6,0,0,6] [5,1,1,0] [6,6,6,6,6,0,0]
[100,100] [0,0] []
\$\endgroup\$
2
  • \$\begingroup\$ Related \$\endgroup\$ Oct 2, 2022 at 19:24
  • 6
    \$\begingroup\$ Pretty sure this is run-length decoding, just with the runs and the lengths separated. \$\endgroup\$
    – hakr14
    Oct 2, 2022 at 23:04

45 Answers 45

1
2
1
\$\begingroup\$

Red, 51 bytes

func[a b][collect[forall a[loop take b[keep a/1]]]]

Try it online!

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

BQN, 1 byte

/

Try it at BQN REPL

Built-in solution.


BQN, 3 bytes

∾⥊¨

Try it at BQN REPL

Roll-your-own solution avoiding / (Replicate) built-in.

   ¨    # Each: for each element of R arg
  ⥊     # Reshape: reshape to a list of L arg copies
 ∾      # Join: and join all the lists together
\$\endgroup\$
1
\$\begingroup\$

K (ngn/k), 6 bytes

{y#'x}

Try it online!

I don't know if this is accepted or not. Take input as (A);(R).

Render output with ,x for array with only one element x, and !0 for array with no element (aka null duplication).

\$\endgroup\$
2
  • \$\begingroup\$ If we're allowed to switch argument order then can be 4 in oK - and I assume something similar is possible with ngn/k? \$\endgroup\$
    – mkst
    Oct 5, 2022 at 10:34
  • 1
    \$\begingroup\$ You can just swap arguments and do #' for two bytes \$\endgroup\$
    – naffetS
    Nov 10, 2022 at 0:09
1
\$\begingroup\$

Clojure, 21 bytes

#(mapcat repeat %2 %)

Yay, short built-in names and neat composition!

Try it online!

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

Nibbles, 4.5 bytes (9 nibbles)

+!$_~.,$_

Input is arg1=R, arg2=A

Nibbles has a 'replicate' function, ^, but it unfortunately does not work on numeric values (the ^ op exponentiates these instead). So we need to use a slightly more convoluted map, returning the value of arg2 for each element of 1..arg1, instead.

 !$_        # zip together arg1 and arg2
    ~       # using the following function:
     .      # map over each element of
      ,$    # 1..arg1
        _   # returning arg2;
+           # finally, flatten the list of lists.

enter image description here

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

MATLAB, 56 bytes

L=[];for k=1:1:numel(a) L=[L repmat(a(k),1,r(k))]; end;L
\$\endgroup\$
1
\$\begingroup\$

tinylisp, 55 bytes

(load library
(q((A R)(foldl concat(map* repeat-val A R

The second line is an anonymous function that takes two lists. Try it online!

Explanation

Too bad the tinylisp library doesn't have a flatten function.

(q                       ; Two-element list acting as a function:
 ((A R)                  ;  Take two arguments, A and R
  (foldl concat          ;  Fold on list concatenation:
   (map* repeat-val A R  ;  Map repeat-val over corresponding pairs of elements
                         ;  from A and R

A non-library version is 67 bytes for a named recursive function that takes the two lists in the opposite order:

(d F(q((R A)(i A(i(h R)(c(h A)(F(c(s(h R)1)(t R))A))(F(t R)(t A)))(

Try it online!

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

Wolfram Language (Mathematica), 20 bytes

Join@@Table@@@In@##&

Try it online!

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

x86-64 machine code, 15 bytes

8B 0A 48 83 C2 04 F3 AB AD 85 C0 79 F3 AB C3

Try it online!

Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes in RSI the address of an array A of 32-bit integers with a −1 terminator; in RDX, the address of an array R in the same form; and in RDI, an address at which to place the result, in the same form. The starting point is after the first 8 bytes.

In assembly, re-arranged for easier understanding:

f:  lodsd           # Load a number from A into EAX, advancing the pointer.
    test eax, eax   # Set flags based on that value.
    jns r           # Proceed to r if it's nonnegative.
    stosd       # When -1 is reached, add it to the result array
    ret         #  and return.
#----------------------------------------------------------------------------------
r:  mov ecx, [rdx]  # Load a number from R into ECX.
    add rdx, 4      # Advance the pointer into R.
    rep stosd       # Add EAX to the result array ECX times, advancing the pointer.
# (This section is actually placed at the start; continue at f.)
\$\endgroup\$
1
\$\begingroup\$

Nim, 68 bytes

import sugar,sequtils
(x,y)=>concat zip(x,y).map x=>x[0].repeat x[1]

Attempt This Online!

-3 bytes thanks to Michael Chatiskatzi

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

jq, 34 bytes

[transpose[]|range(last)as$_|.[0]]

Try it online!

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

Arturo, 48 bytes

$[a r][flatten map combine a r'x->repeat x\0x\1]

Try it

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

q, 5 bytes

where

k, 2 bytes

&:

Built-in where and indexing does exactly as specified

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

Uiua, 1 byte

Try it online!

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

GolfScript, 9 bytes

~zip{)*}%

Try it online!

~ Eval STDIN, input is a list containing the list of items followed by the list of repetitions.

zip Zip the two lists together

{...}% Map each to:

  • ) Separate the tail from the rest.
  • * Repeat the rest {tail} times.

Each item of the resulting list is implicitly printed. Although the list isn't flat, it appears flat when printed.

\$\endgroup\$
1
2

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.