28
\$\begingroup\$

The Challenge

The challenge is simple: given an input list a and another list b, repeat a until it is longer than b.

As an example, call the repeated list ra. Then the following condition must hold true: len(b) < len(ra) <= len(b) + len(a). That is, a must not be repeated more than is required.

Sample Python Implementation

def repeat(a, b):
    ra = a.copy()
    while len(b) >= len(ra):
        ra += a
    return ra

Try it online!

Examples

[1,2,3], [2,4] -> [1,2,3]
[1,2,3], [2,3,4] -> [1,2,3,1,2,3]
[1,2,3], [18,26,43,86] -> [1,2,3,1,2,3]
[2,3,5], [1,2,3,4,5,6,7] -> [2,3,5,2,3,5,2,3,5]
[1,123], [1,12,123,1234] -> [1,123,1,123,1,123]

Scoring

This is , shortest answer in bytes wins. Have fun!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ May we assume that the lists are filled with positive integers, as your examples suggest? \$\endgroup\$
    – Arnauld
    Mar 11 at 7:32
  • 1
    \$\begingroup\$ @Arnauld sure. Go for it \$\endgroup\$
    – Seggan
    Mar 11 at 16:20

31 Answers 31

18
\$\begingroup\$

Python 3, 29 bytes

lambda x,y:len(y)//len(x)*x+x

Try it online!

\$\endgroup\$
10
\$\begingroup\$

Husk, 6 bytes

*¹→¤÷L

Try it online!

   ¤     # combin: ¤ f g x y = f (g x) (g y)
     L   #    where g = length
    ÷    #    and f = integer divide
         # so ¤÷L calculates the (integer) ratio of input lengths;
   →     # then increment the result,
 *¹      # and repeat input 1 that many times
\$\endgroup\$
8
\$\begingroup\$

Vyxal r, 7 bytes

₅?Lḭ›ẋf

Try it Online!

How it works:

₅?Lḭ›ẋf    
₅        # Push first list and its length
 ?Lḭ     # Integer-divide by length of second list
    ›    # Increment ^
     ẋ   # Repeat first list that many times
      f  # Flatten into a single list
\$\endgroup\$
8
\$\begingroup\$

APL (Dyalog Unicode), 20 18 bytes

{(A×⌈(⍴⍵,1)÷A←⍴⍺)⍴⍺}

Try it online!

Thanks to Adám for golfing some bytes and helping me to fix errors in chat. ⎕←,⍣2⍨'thanks'

-2 thanks to AZTECCO ⎕←'thanks'

\$\endgroup\$
7
  • \$\begingroup\$ This is only 22 bytes using SBCS. \$\endgroup\$
    – Adám
    Mar 10 at 22:52
  • \$\begingroup\$ -2? \$\endgroup\$
    – AZTECCO
    Mar 11 at 1:22
  • \$\begingroup\$ Um, your TIO link says that this code is 42 bytes... did I miss something? \$\endgroup\$ Mar 22 at 19:44
  • 1
    \$\begingroup\$ @SylvesterKruin this is SBCS see Adám comment above. \$\endgroup\$
    – Fmbalbuena
    Mar 22 at 20:49
  • \$\begingroup\$ did you subtract the bytes for b← twice? \$\endgroup\$
    – att
    Apr 28 at 1:56
7
\$\begingroup\$

J, 15 14 bytes

-1 byte thanks to Jonah's very clever idea!

];@;<.@%&##<@]

Try it online!

Takes input as b f a, if f is the name you assign the verb to.

Explanation

];@;<.@%&##<@]
       %          divide
        &#        the lengths of the lists by each other
    <.@           and floor it;
           <@]    box b
          #       and repeat it that many times
]  ;              prepend b to this boxed array
 ;@               and raze the result, collapsing it into a single list

Instead of incrementing the division result, as I did in the first version below, we simply prepend the input list to the resulting tiled box list, which is equivalent.

Old Answer

]$~#@]*1<.@+%&#

Try it online!

Explanation

]$~#@]*1<.@+%&#
            %      divide
             &#    the lengths of the lists by each other
       1   +       increment
        <.@        and floor
      *            multiplied by
   #@]             length of a
]$~                and reshape a to be that length

Unfortunately, # doesn't work here, as it doesn't preserve the order. (1 1 1 2 2 2 3 3 3 -: 3 # 1 2 3.)

\$\endgroup\$
2
  • \$\begingroup\$ Managed to eke off one more byte: ];@;<.@%&##<@]. Try it online! \$\endgroup\$
    – Jonah
    Mar 11 at 6:03
  • 1
    \$\begingroup\$ @Jonah Clever approach boxing the input! Thank you! \$\endgroup\$ Mar 12 at 3:34
7
\$\begingroup\$

Curry, 57 42 bytes

This being my first Curry answer, I'm pretty certain its not quite optimal, but as it is our current lang of the month, I figured I'd give at least a half-hearted try at it.

l=length
a!b|l a>l b=a|1>0=a++a!drop(l a)b

Edit: Try it online!

A bit longer than I'd like, my first idea involved the recursive return being r(a++a)b, but I realized that didn't work unless the correct number of repetitions was a power of two.

Edit -15 bytes from some good tips by WheatWizard

\$\endgroup\$
2
  • \$\begingroup\$ There are a couple of things you could apply here from the Haskell tips page. Replacing r with an infix, and replacing if with a guard would shorten things a bit. These two together can get you down to 42 bytes. \$\endgroup\$
    – Wheat Wizard
    Apr 7 at 19:30
  • \$\begingroup\$ ah good ones @WheatWizard! clearly theres a lot still to learn about this language \$\endgroup\$
    – des54321
    Apr 7 at 19:43
6
\$\begingroup\$

05AB1E, 6 bytes

gIg÷>и

Takes the input-lists in the order \$b,a\$.

Try it online or verify all test cases.

Explanation:

g       # Push the length of the first (implicit) input-list `b`
 Ig     # Push the length of the second input-list `a`
   ÷    # Integer-divide the length by `b` by that of `a`
    >   # Increase it by 1
     и  # Repeat the second (implicit) input-list `a` that many times
        # (after which the result is output implicitly)
\$\endgroup\$
6
\$\begingroup\$

Haskell + hgl, 18 bytes

m**frt$P1<<fdv.*l

Divide the lengths, add 1 and then repeat that many times.

Reflection

This is kind of long, but also pretty compact. Part of the issue is that variable reuse is always going to be a bit long. We also have to use both frt and fdv which are unfortunately 3 bytes each. It's totally possible to do this without flipping via rt and dv

m jn$rt<<P1<<dv.*l

But that ends up being a byte longer.

It seems mostly like a quirk of the specific problem that flipping is required.

The only real improvement I can see here is to combine some of the glue used to make things more compact.

  • m jn used in the 19 byte solution could be 1 function. It has a useful type. If it were 3 bytes it would save 1 byte overall

    mjn$rt<<P1<<dv.*l
    
  • l2 m is a little more niche but could be added. As a 3 byte prefix it would actually have costed a byte here

    l2m frt$P1<<fdv.*l
    

    It could also be an infix, but that also costs a byte.

    frt**<(P1<<fdv.*l)
    

    However in better circumstances it could potentially save a byte.

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

Perl 5, 29 bytes

sub{(@{$a=shift})x(1+@_/@$a)}

Try it online!

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

Pyth, 10 9 bytes

*h/l.).Ql

Try it online!

*h/l.).Ql

   l.).Q    # get the length of the second list
  /     l   # integer divide it by the length of the first list
 h          # increment the result of the division
*           # multiply by the first list (like int * list in Python)
\$\endgroup\$
4
\$\begingroup\$

K (ngn/k), 18 bytes

{(~(#y)<#:)(x,)/x}

Try it online!

Appends copies of x until the length is greater than the length of y.

  • (cond)(code)/x set up a while-reduce, beginning with x, the input. the "code" part is repeated until the "cond" part returns a non-truthy value
    • (~(#y)<#:) check whether or not the current list is longer than y
    • (x,) prepend a copy to the current list

An alternative version, with the same byte count, instead uses the do-reduce overload, swapping (~(#y)<#:) for ((-#x)!#y).

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

Jelly, 7 bytes

ẋɓL:L}‘

Try it online!

Nothing all too original.

ẋ          Repeat a by
 ɓL        the length of b
   :L}     floor divided by the length of a
      ‘    plus 1.
\$\endgroup\$
4
\$\begingroup\$

Desmos, 55 49 bytes

f(a,b)=[lforl=a,i=[0...floor(b.length/a.length)]]

Try It On Desmos!

Try It On Desmos! - Prettified

I had to write l=a[1...] instead of just l=a because apparently Desmos defaults to recognizing function arguments as numbers, which the list comprehension doesn't like. The workaround is to force Desmos to recognize it as a list by slicing it in such a way that it just returns the entire list, resulting in +6 bytes.

Apparently the workaround wasn't even necessary because I think a.length forces Desmos to recognize a as a list, rather than a number. Thanks @att for helping me realize this.

\$\endgroup\$
3
  • \$\begingroup\$ It still seems to work when I delete the [1...]... am I missing something? \$\endgroup\$
    – att
    Mar 11 at 3:25
  • \$\begingroup\$ @att bruh I swear it wasn't working when I tested it, that's wierd... \$\endgroup\$
    – Aiden Chow
    Mar 11 at 5:38
  • \$\begingroup\$ wait it might be because I put a.length, which forced desmos to recognize it as a list. dang, let me remove that right now. \$\endgroup\$
    – Aiden Chow
    Mar 11 at 5:41
4
\$\begingroup\$

R, 33 31 bytes

\(a,b,`-`=length)rep(a,1+-b/-a)

Attempt This Online!

-2 bytes thanks to pajonk.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Why the integer division? Standard / seems fine: Try it online! (I don't know why ATO doesn't work for me...). \$\endgroup\$
    – pajonk
    Mar 11 at 5:25
  • \$\begingroup\$ @pajonk because I didn't think to try it! Thanks! Have you tried asking in the ATO chatroom? Maybe they could offer you help. \$\endgroup\$
    – Giuseppe
    Mar 11 at 12:19
  • \$\begingroup\$ I did, it was a known temporary problem. \$\endgroup\$
    – pajonk
    Mar 11 at 13:14
4
\$\begingroup\$

JavaScript (ES6), 43 bytes

Expects (a)(b). This code assumes that both lists are filled with positive integers (as allowed by the OP).

a=>g=(b,...c)=>c[b.length]?c:g(b,...c,...a)

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Really clever, but it looks like it fails eg on f([0,0,0])([18,26,43,86])). \$\endgroup\$
    – Jonah
    Mar 11 at 6:54
  • \$\begingroup\$ @Jonah I assumed the lists were filled with positive integers, but this is indeed not specified. I've asked the OP to clarify. \$\endgroup\$
    – Arnauld
    Mar 11 at 7:34
4
\$\begingroup\$

Brachylog, 11 10 bytes

-1 byte thanks to Unrelated String in chat

lᵐ÷<;?tᵗj₍

Takes input as a single list containing the two input lists in reverse order. Try it online!

Explanation

Ports the common strategy of using int-division to calculate the rep-count:

lᵐ÷<;?tᵗj₍
lᵐ          Length of each list in the input
  ÷         Int-divide
   <        Next greater integer
    ;?      Pair with input
      tᵗ    Replace the last element with its tail (i.e. the second input list)
        j₍  Repeat the last element a number of times equal to the first element

For example, with input [[1, 2, 3, 4, 5], [8, 9]]:

lᵐ          [5, 2]
  ÷         2
   <        3
    ;?      [3, [[1, 2, 3, 4, 5], [8, 9]]]
      tᵗ    [3, [8, 9]]
        j₍  [8, 9, 8, 9, 8, 9]

Old solution, 11 bytes

Implements the spec pretty directly, using Brachylog's backtracking:

hj↙İ.&tl<~l
h            The first of the inputs
 j           Concatenated to itself
  ↙İ         an unspecified number of times
    .        is the output
     &       And
      t      The second of the inputs
       l     Length of ^
        <    is less than a number
         ~l  which is the length of
             the output (implicit)
\$\endgroup\$
1
  • \$\begingroup\$ Nice call on using İ! I had 14 bytes before forgetting about it because (alongside not having thought of your trailing ~l) arrow subscripting with a completely unconstrained variable does some very strange things with explicit writes (although it's fine with "normal" I/O, oddly enough). \$\endgroup\$ Mar 12 at 4:41
4
+50
\$\begingroup\$

Mathematica/Wolfram Language, (44) 41 Bytes

PadLeft[#,Ceiling[Tr[1^#2]+1,Tr[1^#]],#]&

-3 bytes from alephalpha on something I really should have caught

I feel like this could still be shorter, but it works as-is. Uses Tr[1^x] as a way of getting Length for cheap, and takes advantage of Ceiling having a two-argument mode. Other than that, it would probably take a full rewrite to improve, since I can't use [LeftCeiling] to shave bytes with the two-argument mode.

Try It Online!

\$\endgroup\$
8
  • 1
    \$\begingroup\$ #1 -> # saves 3 bytes. \$\endgroup\$
    – alephalpha
    Apr 27 at 3:00
  • \$\begingroup\$ How did I not catch that? \$\endgroup\$
    – Romanp
    Apr 27 at 13:05
  • 2
    \$\begingroup\$ Ceiling[a+1,b] -> Floor[a,b]+1. Interestingly, two-argument ⌈a,b⌉ (and ⌊a,b⌋) does work in wolframscript, but not in notebooks. Also, your tio link seems to be wrong. \$\endgroup\$
    – att
    Apr 28 at 1:04
  • \$\begingroup\$ The two actually seem to perform differently. Floor[1,2]+1 is equal to 1, while Ceiling is equal to 2. Fixing the TIO link now. \$\endgroup\$
    – Romanp
    Apr 28 at 13:30
  • \$\begingroup\$ Floor[1,2]+1 is 1. Ceiling[1+1,2] is also 1. \$\endgroup\$
    – att
    Apr 28 at 21:00
3
\$\begingroup\$

Factor + sequences.repeating, 45 42 bytes

[ length over length tuck /i 1 + * cycle ]

enter image description here

Explanation

          ! { 2 3 5 } { 1 2 3 4 5 6 7 }
length    ! { 2 3 5 } 7
over      ! { 2 3 5 } 7 { 2 3 5 }
length    ! { 2 3 5 } 7 3
tuck      ! { 2 3 5 } 3 7 3
/i        ! { 2 3 5 } 3 2
1         ! { 2 3 5 } 3 2 1
+         ! { 2 3 5 } 3 3
*         ! { 2 3 5 } 9
cycle     ! { 2 3 5 2 3 5 2 3 5 }
\$\endgroup\$
3
\$\begingroup\$

Charcoal, 9 8 bytes

W¬›ⅉLηIθ

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

W¬›ⅉLη

Until the number of output lines exceeds the length of the second input...

Iθ

... output each element of the first input on its own line.

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

Ruby, 26 bytes

->x,y{x*(y.size/x.size+1)}

Attempt This Online!

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

TI-Basic, 26 bytes

Prompt A,B
ʟA
While dim(Ans)≤dim(ʟB
augment(Ans,ʟA
End
Ans

Output is stored in Ans and displayed.

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

Wolfram Language (Mathematica), 33 bytes

Table[##&@@#,Tr[1^#2]/Tr[1^#]+1]&

Try it online!

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

Exceptionally, 25 18 bytes

GV}lL}nGVL/nIU*lP/

Attempt This Online!

Explanation

' Get an input
G
' Eval it
V
' Store that in ls
} ls
' Get its length
L
' Store that in num
} num
' Get an input
G
' Eval it
V
' Get the length
L
' Divide by num
/ num
' Truncate to integer
I
' Increment
U
' Repeat ls that many times
* ls
' Print
P
' Attempt to divide the list by itself, ending the program
/
\$\endgroup\$
3
\$\begingroup\$

Julia 1.0, 31 bytes

~=length
A%B=repeat(A,~B÷~A+1)

Try it online!

\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 61 bytes

(a,b)=>Array(~~(b.length/a.length)+1).fill``.map(_=>a).flat()

Try it online!

Way longer than the other answers but who cares. Should work in the browser too but TIO only supports Array.flat() in Node.

\$\endgroup\$
1
  • \$\begingroup\$ ~~(...) => ...|0, .fill(a).flat() \$\endgroup\$
    – allxy
    Apr 20 at 22:21
2
\$\begingroup\$

Perl 6, 34 bytes

{|@^a xx 1+@^b.elems div@^a.elems}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Pip, 12 bytes

Ty#>b{Yy.a}y

Try It Online!

How?

Ty#>b{Yy.a}y
T    {    }  - Till
 y#>b        - Length of y greater than length of b(2nd input)
      Y        - Yank (equivalent to (y:a))
       y.a     - y concatenated with a
           y - Return y 
\$\endgroup\$
1
\$\begingroup\$

APL+WIN, 18 bytes

Prompts for a nested vector comprising of list b followed by list a

∊(∊1+⌊÷/⍴¨v)⍴1↓v←⎕

Try it online! Thanks to Dyalog APL Classic

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

JavaScript (Node.js), 43 48 bytes

+5 bytes, realised it doesn't work

a=>b=>{for(A=[...a];!a[b.length];a.push(...A));}

Try it online!

An alternative JS answer. Takes curried input a and b and extends a until it's longer than b.

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

C (clang), 62 61 bytes

-1 thanks to @ceilingcat

f(*a,b,c,d){for(;b=++d%c;);for(;d--;)printf("%d ",a[b++%c]);}

Try it online! Inputs are list a, list b, and their respective lengths in that order (as C doesn't store length information in arrays); outputs are sent to stdout.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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