# Interlaced array [duplicate]

You are given an array A of length N and an array B of length N-1.

You need to output array where items alternates:

A[0], B[0], A[1], B[1], ..., B[N-2], A[N-1]


Restrictions:

N>0


Type of items is not specified, but code should work at least for unsigned integers.

Sample input:

A = [1,2,3,4]
B = [7,8,9]


Sample output:

[1,7,2,8,3,9,4]


Edge case input:

A = ['Hello, world']
B = []


Edge case output:

['Hello, world']


This question is a simplified version of Zip uneven lists.

• Will the arrays always contain positive integers as in the example? Will they always be sorted? Will N always be at least 2? Please add test cases to cover those possibilities, if applicable Mar 11 at 13:52
• Related Mar 11 at 14:08
• The edge case does not satisfy the criteria in the question. According to the specification, B should be one element shorter than A. In your edge case, it is 2 elements shorter. Mar 11 at 14:17
• @Xcali no, array A has length 1. Mar 11 at 14:19
• No need to apologize for asking a duplicate. It happens all the time that two people had the same idea or very similar ideas. Usually that's an indication it is a good idea. Mar 11 at 16:02

# Python 3, 36 34 bytes

-2 bytes thanks to @Jonathan Allan

lambda a,b:sum(zip(a,b+a),())[:-1]


Try it online!

# Vyxal, 1 byte

Y


Try it Online!

It's a built in. Takes B then A

• Can you change the order of arrays, so that the bigger one comes first? Mar 11 at 14:10

# JavaScript (ES6), 35 bytes

Expects (A,B) and returns a new array.

-2 bytes if the integers are guaranteed to be positive, in which case this is identical to my answer to this closely related challenge.

f=([v,...a],b)=>1/v?[v,...f(b,a)]:a


Try it online!

# JavaScript (ES6), 38 bytes

Expects (A)(B). Outputs by modifying A.

a=>b=>b.map((v,i)=>a.splice(i-~i,0,v))


Try it online!

# Perl 5, 40 bytes

sub{grep/./,map{$_,$_[1][$i++]}@{$_[0]}}


Try it online!

# Jelly, 2 bytes

żẎ


Try It Online!

I don't think there's a built-in directly for this...? Interleaving returns a 2D list and then we flatten it by one level (so even if either list is more than depth 1, this should be correct).

# Retina 0.8.2, 28 bytes

O#$\d+(?<=((,)|.)+)$#2
¶
,


Try it online! Takes two comma separated lists of unsigned integers as input. Explanation:

O#$\d+(?<=((,)|.)+)$#2


Sort each value by the number of preceding commas in its line.

¶
,


Join the two lists together.

# Charcoal, 13 bytes

ＵＭθ⮌ιＷ§θⅉ⟦Ｉ⊟ι


Try it online! Takes input as a pair of lists. Explanation: Same as my answer to the duplicate challenge.

ＵＭθ⮌ι


Reverse each of the two lists.

Ｗ§θⅉ


Cyclically index the lists according to the number of values already output and repeat until that list is empty.

⟦Ｉ⊟ι


Output the "next" value from that list on its own line.

(a:x)!b=a:b!x
a!_=a


Try it online!

-4 bytes thanks to Wheat Wizard

Defines an infix function ! (thanks to Wheat Wizard for this optimization) that returns [a[0], ...] (swapping arguments, so next we get [b[0], ...] which is followed by [a[1], ...], etc.) or if b is empty, just a.

### Standard ML (MLton), 28 bytes

fun f[]a=a|f(a::x)b=a::f b x


Try it online!

### Proton, 31 bytes

f=(a,b)=>b?a[to1]+f(b,a[1to]):a


Try it online!

• Some slight optimizations: You can make f an infix function, and you can save a byte by swapping your cases: 25 bytes Mar 11 at 14:34
• @WheatWizard Oh that's neat, I did not realize you can just define infix functions like that. Thanks! Mar 11 at 14:45