Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Sandbox

Comments on Interleave multiple arrays at once

Post

Interleave multiple arrays at once

+2
−0

Given $ n $ lists of numbers:

$$ [A_1, A_2, ..., A_x ] $$ $$ [B_1, B_2, ..., B_y ] $$ $$ [C_1, C_2, ..., C_z ] $$

Join them in a way that the index of each element is preserved. The order between elements with the same index does not matter.

Sorted: $$ [A_1, B_1, C_1, ..., A_x, B_y, C_z] $$ Arbitrary, but still correct as the rough order is preserved: $$ [A_1, C_1, B_1, C_2, A_2, B_2 ..., B_y, A_x, C_z] $$

If the arrays are of different lengths, extra elements are not inserted, they are merged as-is: $$ [A_1, A_2, A_3, A_4, A_5] $$ $$ [B_1, B_2, B_3] $$ becomes: $$ [A_1, B_1, A_2, B_2, A_3, B_3, A_4, A_5] $$

Examples

These examples assume ordering remains correct.

[1, 2, 3], [4, 5, 6] -> [1, 4, 2, 5, 3, 6]
[1, 2], [3, 4], [5, 6], [7, 8] -> [1, 3, 5, 7, 2, 4, 6, 8]
[1, 2, 3], [4, 5], [6] -> [1, 4, 6, 2, 5, 3]
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (4 comments)
General comments
Hakerh400‭ wrote almost 4 years ago

This seems very similar to https://codegolf.codidact.com/posts/279434, and some of the answers would literally work for this challenge too.

moony‭ wrote almost 4 years ago

Seems so, it's only different for languages where strings aren't implicitly character arrays

Monica Cellio‭ wrote almost 4 years ago

I don't understand the first example, where B(y) is between A(z) and C(z). Shouldn't that one end A(y), B(y), C(y), A(z), C(z)? (Sorry, I don't know how to do proper Mathjax.)

user‭ wrote over 3 years ago

"the index of each element is preserved" That suggests A1, B1, and C1 are all at index 1 in the output. I feel like you could remove that sentence, since the sentence after it is clear enough.