J, 932 bytes
[:;(,/:,&(#\)
Attempt This Online!
Since we can't zip uneven lists in J using ,.
, we just cat the lists ,
and then sort them /:
by 1..n
catted with 1..m
(where n
and m
are the two list lengths). This works because sort is stable.
J, 12 bytes
a:-)&(<;.~,@|1~2~:@/\'_'&,:)
Attempt This Online!Attempt This Online!
We can take the input as lists (boxed lists in J, by necessity), and the problem reduces to zipping listsThe 2nd part of unequal length. Thisthis is not allowed by "stitch" ,.
(J's zip), but you can combined to lists with ,:
and J will pad the extra slots with blanks.only interesting insight:
After that we can imitate zip by doing a transpose, flatten, and removing empties.
[:;...&(<;.1~2~:/\'_'&,)
This is the preprocessing step that breaks the lists down into the grouped sections. A standard J idiom which is unfortunately a bit verbose in J. I could save two bytes by using integers instead of strings as input.
,/:,&(#\)
The main logic. Since we can't zip uneven lists in J using ,.
, we just cat the lists ,
and then sort them /:
by 1..n
catted with 1..m
(where n
and m
are the two list lengths). This works because sort is stable.