f=lambda s:s and s[0][:1]+f(s[1:]+[s[0][1:]][:s[0]>""])or""
A recursive function that takes a list of strings and returns a string. Try it online!
Explanation
We're going to build the interleaved string one character at a time. There are several cases to consider:
- Is the first string in the list nonempty? Take its first character, move the rest of it to the end of the list, and recurse.
- Is the first string in the list empty? Remove it from the list and recurse.
- Is the list of strings empty? Then we're done. Return empty string.
The implementation goes as follows:
f = lambda s: s and ... or ""
Define f
to be a function of one argument, s
, which is a list of strings. If s
is nonempty, do the ...
part (see below); otherwise, return ""
.
s[0][:1] + f(...)
Since we now know s
is nonempty, we can refer to its first element, s[0]
. We don't know yet whether s[0]
is empty, though. If it isn't, we want to concatenate its first character to a recursive call; if it is, we can concatenate the empty string to a recursive call. Slicing everything left of index 1 works in both cases.
s[1:] + ...
The argument to the recursive call is going to be all but the first element of s
, concatenated to either a 1-element list (the rest of s[0]
if it was nonempty) or an empty list (if s[0]
was empty).
[s[0][1:]][:s[0] > ""]
We put s[0][1:]
(the part of s[0]
after the first character, which is the empty string if s[0]
is empty) into a singleton list and then take a slice of that list. If s[0]
is empty, s[0] > ""
is False
, which is treated as 0
in a slice; thus, we slice from index 0 to index 0--an empty slice. If s[0]
is not empty, s[0] > ""
is True
, which is treated as 1
; thus, we slice from index 0 to index 1--the whole 1-element list.
If lists of single-character strings can be used in place of strings, this solution can be 54 bytes:
f=lambda s:s and s[0][:1]+f(s[1:]+(s[0]and[s[0][1:]]))
Try it online!