# Repeat List Until Longer

## The Challenge

The challenge is simple: given an input list a and another list b, repeat a until it is longer than b. 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!

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

# Python 3, 29 bytes

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


Try it online!

# 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


# Vyxalr, 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


# 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'

• This is only 22 bytes using SBCS.
Mar 10 at 22:52
• -2? Mar 11 at 1:22
• @AZTECCO Thanks Mar 11 at 16:34
• Um, your TIO link says that this code is 42 bytes... did I miss something? Mar 22 at 19:44
• @SylvesterKruin this is SBCS see Adám comment above. Mar 22 at 20:49

# 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.

]$~#@]*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.) • Managed to eke off one more byte: ];@;<.@%&##<@]. Try it online! Mar 11 at 6:03 • @Jonah Clever approach boxing the input! Thank you! Mar 12 at 3:34 # 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 • 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. Apr 7 at 19:30 • ah good ones @WheatWizard! clearly theres a lot still to learn about this language Apr 7 at 19:43 # 05AB1E, 6 bytes gIg÷>и  Takes the input-lists in the order $$\b,a\$$. 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)  # 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. # Perl 5, 29 bytes sub{(@{$a=shift})x(1+@_/@\$a)}


Try it online!

# 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)


# 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).

# 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.

• It still seems to work when I delete the [1...]... am I missing something?
– att
Mar 11 at 3:25
• @att bruh I swear it wasn't working when I tested it, that's wierd... Mar 11 at 5:38
• 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. Mar 11 at 5:41

# R, 33 31 bytes

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

Attempt This Online!

-2 bytes thanks to pajonk.

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

# 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!

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

# 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)

• 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). Mar 12 at 4:41

# 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.


# Factor + sequences.repeating, 45 42 bytes

[ length over length tuck /i 1 + * cycle ] ## 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 }


# Charcoal, 9 8 bytes

Ｗ¬›ⅉＬηＩθ


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

Ｗ¬›ⅉＬη


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

Ｉθ


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

# 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.

# 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!

• #1 -> # saves 3 bytes. Apr 27 at 3:00
• How did I not catch that? Apr 27 at 13:05
• 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.
– att
Apr 28 at 1:04
• 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. Apr 28 at 13:30
• Floor[1,2]+1 is 1. Ceiling[1+1,2] is also 1.
– att
Apr 28 at 21:00

# 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
/


# 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.

• ~~(...) => ...|0, .fill(a).flat() Apr 20 at 22:21

# Perl 6, 34 bytes

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


Try it online!

# Ruby, 26 bytes

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

Attempt This Online!

# 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.

# Wolfram Language (Mathematica), 33 bytes

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


Try it online!

# 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

# 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.

# BQN, 17 bytes

⊑⥊˜≠∘⊑×1+·⌊∘÷˜´≠¨


Anonymous tacit function that takes a single argument, a list containing the two input lists. Try it at BQN online

### Explanation

⊑⥊˜≠∘⊑×1+·⌊∘÷˜´≠¨
≠¨  Length of each list
´    Fold that two-integer list on
˜     Reversed-order
⌊∘÷      Division-and-floor
·         Then