| Bytes | Lang | Time | Link |
|---|---|---|---|
| 080 | Go | 240612T212142Z | bigyihsu |
| 010 | Pip xp | 240311T213827Z | DLosc |
| 009 | Pip x | 240311T212411Z | DLosc |
| 065 | Perl 5 MListUtil=min | 240311T153851Z | Xcali |
| 005 | Vyxal 3 | 240106T202213Z | pacman25 |
| 009 | Uiua | 240106T200949Z | chunes |
| 007 | Japt g | 230117T080520Z | Shaggy |
| 042 | Arturo | 230117T072150Z | chunes |
| 028 | Prolog SWI | 220903T054447Z | Aiden Ch |
| 021 | Curry PAKCS | 220405T124205Z | Wheat Wi |
| 139 | Desmos | 220327T110155Z | Aiden Ch |
| 006 | Vyxal | 220321T154659Z | Seggan |
| 068 | C gcc | 220317T031538Z | ErikF |
| 025 | Julia 1.0 | 220315T093217Z | MarcMush |
| 045 | PowerShell Core | 220313T205621Z | Julian |
| 032 | Ruby | 220314T091307Z | G B |
| 005 | 05AB1E | 220313T125112Z | Kevin Cr |
| 027 | Bash | 220314T020636Z | Jonah |
| 038 | PARI/GP | 220314T010009Z | alephalp |
| 065 | HBL | 220313T213301Z | DLosc |
| 036 | R | 220313T123129Z | pajonk |
| nan | J | 220313T170105Z | Jonah |
| 039 | x86 machine code 32bit | 220313T163625Z | xiver77 |
| 005 | Husk | 220313T145956Z | Dominic |
| 033 | JavaScript ES6 | 220313T150734Z | Arnauld |
| 039 | Factor | 220313T143433Z | chunes |
| 005 | Jelly | 220313T133532Z | Jonathan |
| 051 | Perl 6 | 220313T131551Z | Razetime |
| 016 | APL Dyalog Unicode | 220313T131405Z | Adá |
| 032 | Python 2 | 220313T104543Z | pxeger |
| 019 | Haskell | 220313T105459Z | pxeger |
| 013 | Charcoal | 220313T113209Z | Neil |
| 027 | APL+WIN | 220313T111124Z | Graham |
| 040 | Retina 0.8.2 | 220313T105223Z | Neil |
Go, 80 bytes
func f(a,b[]int)(o[]int){if len(a)<1{return}
return append(a[:1],f(b,a[1:])...)}
Straight-forward solution directly implementing the algorithm in the OP.
Pip -xp, 10 bytes
FI\$&FLZDg
Takes two Pip lists as command-line arguments; outputs a Pip list to stdout. Try It Online!
Explanation
Port of Jonah's J answer, among others:
FI\$&FLZDg
g ; List of command-line args (i.e. list of lists of numbers)
ZD ; Zip (transpose), filling with a default value of "" (which is falsey)
FL ; Flatten
\$& ; Scan on logical AND, making everything after the first falsey value
; falsey
FI ; Filter, keeping only truthy values
Pip -x, 9 bytes
Wb::aPPOb
Takes two Pip lists as command-line arguments; outputs to stdout, one number per line. Attempt This Online!
Explanation
Wb::aPPOb
; a and b are command-line args
W ; Loop while
b::a ; b, after being swapped with a, is truthy (nonempty):
POb ; Pop the first element from b
P ; and print it
Perl 5 -MList::Util=min, 65 bytes
sub{($a,$b)=@_;grep/./,map{$_,shift@$b}@$a[0..min(@$b+0,$#{$a})]}
Japt -g, 7 bytes
íV c óÏ
íV c óÏ :Implicit input of arrays U & V
íV :Interleave U with V, padding V with null
c :Flatten
ó :Partition before elements
Ï :That are falsey
:Implicit input of first element
5 bytes
By taking input as a 2D array, with the order reversed.
ÕcÔóÏ
ÕcÔóÏ :Implicit input of 2D array
Õ :Transpose, padding the first array with null
c :Flat map
Ô :Reverse
óÏ :As above
:Implicit input of first element
Arturo, 42 bytes
f:$[a,b][([]=a)?->a->@[a\0]++f b drop a 1]
f:$[a,b][ ; a function f taking two lists a and b
([]=a)? ; is a empty?
->a ; if so, return a
-> ; otherwise,
@[a\0] ; the first element of a as a list
++ ; concatenated with
f b drop a 1 ; the result of calling f with b and a without its first element
] ; end function
Prolog (SWI), 37 28 bytes
-9 bytes thanks to @DLosc
[H|T]+B+[H|Y]:-B+T+Y.
A+_+A.
Port of the Haskell solution.
Curry (PAKCS), 21 bytes
(a:b)#c=a:c#b
[]#_=[]
The Haskell answer almost works in Curry, but because of non-determinism it matches any prefix of the result. We can restrain it by adding two more bytes.
Desmos, 139 bytes
l=[1...a.length+b.length]
g(A)=join(A,0l)[ceil(l/2)]
L=\{\mod(l,2)=1:g(a),g(b)\}
f(a,b)=\{a.\length=0:a,L[1...\min(\{L=0:l,l.\max+1\})-1]\}
Holy crap, it took me forever for me to get an actually working solution for this challenge in Desmos. I don't know if I'm severely overcomplicating this or what, but I got it to work so...
¯\_(ツ)_/¯
I'll see if I can golf this some more later, too tired to think about this right now :P
Vyxal, 6 bytes
Zf0€÷^
Looks like 05AB1E beats Vyxal this time. Takes input in reversed order.
Zf0€÷^ # Leaving out this comment makes the explanation kinda feel empty
Z # Zip the lists
f # Flatten
0€ # Split on 0
÷ # Push every item
^ # Reverse the stack
Vyxal, 6 bytes
Zf:0ḟẎ
A slightly different approach. Still 6 bytes.
Zf:0ḟẎ # Leaving out this comment makes the explanation kinda feel empty
Z # Zip the lists
f # Flatten
: # Duplicate the top of the stack
0ḟ # Index of the first 0
Ẏ # Take everything up until that index
Vyxal, 5 bytes
Zf0€h
Takes input in reversed order. Returns 0 for the case of 2 empty lists, so does not meet the spec.
Zf0€h # Leaving out this comment makes the explanation kinda feel empty
Z # Zip the 2 lists
f # Flatten them
0€ # Split on 0
h # Get the first item
C (gcc), 68 bytes
Takes 0-terminated arrays as input.
f(a,b)int*a,*b;{for(;*a;!*b?a=b:b++)printf("%d %d "+3*!*b,*a++,*b);}
Julia 1.0, 25 bytes
a^b=@show(a[1])b^a[2:end]
prints the values and ends with an error
Julia 1.0, 30 bytes
>(b)=[]
>(b,a,A...)=[a;A>b...]
expects b>a..., returns an actual list
PowerShell Core, 45 bytes
param($a,$b)for(;$a){$h,$t=$a
$a,$b=$b,$t
$h}
One test case returns 1 instead of [1] as PowerShell tends to flatten arrays
-1 byte thanks to mazzy
05AB1E, 6 5 bytes
ζ˜#R`
Input-lists in reversed order.
1 byte is added to deal with the edge case of two empty input-lists, which otherwise would result in an empty string. Otherwise the two trailing bytes could have been н (pop and push first inner list) instead.
Try it online or verify all test cases.
Explanation:
ζ # Zip/transpose the two (implicit) input-lists, with space " " as filler
˜ # Flatten this list of pairs
# # Split it on spaces
R # Reverse the list of lists
` # Pop and push the lists to the stack
# (after which the top list is output implicitly,
# or the (implicit) input-list if both lists were empty)
Bash, 27 bytes
read x<$1&&echo $x&&f $2 $1
Port of pxeger's python solution to Bash.
Input lists are taken as files (one entry per line). Output is one entry per line. Output contains extra blanks, which may be illegal and could be fixed at the price of some bytes, but I liked the simplicity of the translation.
HBL, 6.5 bytes
?.(211.'?,(2.
The branch macro isn't implemented in the online interpreter at the moment, so here's a 7.5-byte equivalent without it:
?.(1(1.)('?,(2.
Explanation
The longer version implements the spec directly:
?.(1(1.)('?,(2.
? If
. The first argument is truthy:
(1 Cons
(1.) The head of the first argument to
('? A recursive call with
, The second argument and
(2. The tail of the first argument
Otherwise, return the first argument (empty list)
The branch macro (2, in golfed HBL) takes several expressions and restructures them into a function call with two arguments. With six expressions, it works like this:
(2 a b c d e f) -> (a (b c) (d e f))
In this case, we have:
2 1 1 . '? , (2.) -> (1 (1 .) ('? , (2.)))
R, 36 bytes
f=\(a,b)if(sum(a))c(a[1],f(b,a[-1]))
Slightly longer, but without recursion:
R, 43 bytes
\(a,b,x=rbind(c(a,0),c(b,0)))x[!cumsum(!x)]
J, 19 18 17 16 bytes
0(i.~{.]),@|:@,:
-1 thanks to ovs!
Consider 1 2 3 4 f 8 9:
,:Laminate with 0-fill:1 2 3 4 8 9 0 0|:@Transpose:1 8 2 9 3 0 4 0,@Flatten:1 8 2 9 3 0 4 00(i.~{.[)Take everything up to the first 0:1 8 2 9 3
x86 machine code (32-bit), 39 bytes
\x31\xC0\x89\xC5\x55\x87\x2C\x24\x39\xCD\x74\x19\xFF\x34\xAE\x8F\x04\x87
\x45\x40\x87\x2C\x24\x39\xD5\x74\x0A\xFF\x34\xAB\x8F\x04\x87\x45\x40\xEB
\xE0\x5D\xC3
The algorithm is quite straightforward, but 32-bit x86 really needs more registers. Some ugly stack hacks are used to cope with the lack of registers.
The test cases in TIO relies on a GCC extension supporting C arrays of length 0. ISO C and C++ forbid 0-length arrays.
Since OP didn't specify the upper bound of integers, I tried to find some clever way using an array of bytes, and interleaving them with pdep or some SSE shuffle instruction, but that ended up using too many bytes.
# edi: dst, esi: arr0, ecx: len0, ebx: arr1, edx: len1
# eax: return, ebp: clobber
0: 31 c0 xor eax,eax
2: 89 c5 mov ebp,eax
4: 55 push ebp
5: 87 2c 24 xchg DWORD PTR [esp],ebp
8: 39 cd cmp ebp,ecx
a: 74 19 je 0x25
c: ff 34 ae push DWORD PTR [esi+ebp*4]
f: 8f 04 87 pop DWORD PTR [edi+eax*4]
12: 45 inc ebp
13: 40 inc eax
14: 87 2c 24 xchg DWORD PTR [esp],ebp
17: 39 d5 cmp ebp,edx
19: 74 0a je 0x25
1b: ff 34 ab push DWORD PTR [ebx+ebp*4]
1e: 8f 04 87 pop DWORD PTR [edi+eax*4]
21: 45 inc ebp
22: 40 inc eax
23: eb e0 jmp 0x5
25: 5d pop ebp
26: c3 ret
Husk, 5 bytes
↑_ΣT0
Input is list of the two lists.
Transposes using 0 to fill shorter rows, flattens (Σ) the result, and ↑ takes the longest prefix with all elements that are truthy using the function _ (=negate; it would have been more-intuitive to use the Identity function here, but for reasons that I don't understand this doesn't seem to work).
Pretty-much the same approach as Kevin Cruijssen's answer but independently found.
Note that the Husk interpreter will not accept input consisting of only empty lists, presumably since it's unable to infer their type. The TIO link above successfully runs all the test cases (including the first, only-empty-lists one), since these are grouped together and run side-by-side, allowing the interpreter to infer that all lists are of numbers; but it fails when given the first test case on its own. I can't currently see a way to avoid this.
Factor, 43 39 bytes
[ f pad-longest vmerge { f } split1 . ]
Explanation
! { 1 2 3 4 5 } { 11 12 13 }
f ! { 1 2 3 4 5 } { 11 12 13 } f
pad-longest ! { 1 2 3 4 5 } { 11 12 13 f f }
vmerge ! { 1 11 2 12 3 13 4 f 5 f }
{ f } ! { 1 11 2 12 3 13 4 f 5 f } { f }
split1 ! { 1 11 2 12 3 13 4 } { 5 f }
. ! { 1 11 2 12 3 13 4 }
Jelly, 5 bytes
Uses Kevin Cruijssen's method from their 05AB1E answer.
z⁶FḲḢ
A monadic Link that accepts a pair of lists, [a, b], and yields a zipped list.
Try it online! Or see the test-suite.
How?
z⁶FḲḢ - Link: pair of lists [a,b]
⁶ - space character
z - transpose [a,b] with space-filler
F - flatten
Ḳ - split at spaces
Ḣ - head
APL (Dyalog Unicode), 16 bytes
Anonymous tacit prefix function taking a list of two lists.
((>+2×⌊)/≢¨)⍴⍉∘↑
Python 2, 32 bytes
def f(X,Y):print X.pop(0);f(Y,X)
Ends with an error.
Python, 35 bytes
f=lambda X,Y:X and X[:1]+f(Y,X[1:])
No error.
Whython, 33 bytes
f=lambda X,Y:[X.pop(0)]+f(Y,X)?[]
Charcoal, 13 bytes
UMθ⮌ιW§θⅉ⟦I⊟ι
Try it online! Takes input as a pair of lists. Explanation:
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.
APL+WIN, 27 bytes
Prompts for two lists as a nested vector
(^\0≠v)/v←,⍉⊃(⌈/⍴¨v)↑¨v←,¨⎕
Retina 0.8.2, 40 bytes
^
;
+`;(\d+),?(.*);(.*)
$1,;$3;$2
,?;.*
Try it online! Link includes test cases. Explanation:
^
;
Start with an empty output list.
+`
Until the first input list is empty, ...
;(\d+),?(.*);(.*)
... match the first element of the first input list, the rest of the first input list and the second input list, and...
$1,;$3;$2
... move the first element of the input list to the output list and swap the rest of the first input list with the second list.
,?;.*
Delete the input lists.