1
\$\begingroup\$

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.

\$\endgroup\$
7
  • 4
    \$\begingroup\$ 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 \$\endgroup\$
    – Luis Mendo
    Mar 11 at 13:52
  • 3
    \$\begingroup\$ Related \$\endgroup\$
    – Dingus
    Mar 11 at 14:08
  • \$\begingroup\$ 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. \$\endgroup\$
    – Xcali
    Mar 11 at 14:17
  • 1
    \$\begingroup\$ @Xcali no, array A has length 1. \$\endgroup\$ Mar 11 at 14:19
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – Wheat Wizard
    Mar 11 at 16:02

7 Answers 7

2
\$\begingroup\$

Python 3, 36 34 bytes

-2 bytes thanks to @Jonathan Allan

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

Try it online!

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

Vyxal, 1 byte

Y

Try it Online!

It's a built in. Takes B then A

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

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!

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

Perl 5, 40 bytes

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

Try it online!

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

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

\$\endgroup\$
0
\$\begingroup\$

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

UMθ⮌ιW§θⅉ⟦I⊟ι

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

UMθ⮌ι

Reverse each of the two lists.

W§θⅉ

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

⟦I⊟ι

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

\$\endgroup\$
0
\$\begingroup\$

Haskell, 19 bytes

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

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

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