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

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 moderator attention?
You might want to add some details to your flag.

1 comment thread

General comments (4 comments)