Weave Strings Together
Given a list of strings(and optionally, their length) as input, weave the strings together.
Intro
Your goal is to mimic the WV
operator in Pip. Take a list of strings and alternate between their characters like so:
hello,
world, → hwc,eoo,lrd,lle,od → hwceoolrdlleod
code
Effectively, you need to group the characters at each index, and join them into a single string.
The strings may have spaces.
No truncation should be done, and all the characters of each string must be used.
Test Cases
["kino","cinema","movie"] → kcmiionnvoeimea
["code","golf"] → cgoodlef
["Hi","","There"] → HTihere
["Explanation"] → Explanation
["Dyalog APL","Cultivation","Orchard"] → DCOyuralclthoiagvr adAtPiLon
You check any other test cases with this program.
[Jelly], 2 1 byte Z …
3y ago
[APL (Dyalog Unicode)], 10 byt …
4y ago
Risky, 1 byte 0? Try …
3y ago
[Haskell], 40 bytes ```hask …
4y ago
Japt `-P`, 7 2 bytes Õc …
4y ago
Scala, 60 bytes ```scala .ma …
3y ago
[Ruby], 51 bytes ```ruby - …
3y ago
[Python 2], 58 54 bytes ``` …
3y ago
[Python 3], 72 69 bytes ``` …
3y ago
BQN, 8 bytesSBCS ``` ∾⊒˜¨⊔ …
3y ago
[JavaScript (Node.js)], 53 byt …
4y ago
Ruby, 48 45 bytes ```ruby …
3y ago
[Python 3], 91 90 bytes Sav …
3y ago
13 answers
Jelly, 2 1 byte
Z
-1 byte thanks to Razetime!
Full program
How it works
Z - Main link. Takes a list L on the left
Z - Transpose
Implicitly print, smashing lists together to form one string
APL (Dyalog Unicode), 10 bytes
Full program. Prompts for list of strings from stdin.
0~⍨,⍉↑0,¨⎕
⎕
prompt for list of strings
0,¨
prepend a zero to each
↑
convert to orthogonal matrix (increase rank at cost of depth), padding with 0s
⍉
transpose
,
ravel (flatten)
0~⍨
remove 0s
0 comment threads
Python 3, 72 69 bytes
lambda l:''.join(sum(zip(*[[*i]+['']*max(map(len,l))for i in l]),()))
Uses this method to flat zip, with modifications to pad the shorter lists.
0 comment threads
Ruby, 51 bytes
->a{(0..a.map(&:size).max).map{|n|a.map{_1[n]}}*""}
->a{ } # lambda taking array `a`
(0..a.map(&:size).max) # range of 0..length of longest string
.map{|n| } # map over indices range
a.map{ } # map over strings
_1[n] # nth character in string (or nil)
*"" # recursive join to string
This exploits the following behaviors:
- Indexing past the bound of a string is
nil
"abc"[5]
=> nil
-
nil.to_s
is the empty string
nil.to_s
=> ""
-
Array#join
joins nested arrays recursively
[1,2,[3,4],5,[6,[7]]]*""
=> "1234567"
0 comment threads
BQN, 8 bytesSBCS
∾⊒˜¨⊔○∾⊢
Run online! and parsing diagram
The idea here is to build a list of groups, with group i
containing the i
th character of each string that has one, then join the groups. The structure ∾ ⊒˜¨ ⊔○∾ ⊢
is a train of four functions: the last three form a 3-train and then Join (∾
) is applied to the result.
∾⊒˜¨⊔○∾⊢
⊢ # Argument, unchanged
⊒˜¨ # Indices for each string
⊔○∾ # Join both and group
∾ # Join group result
The function ⊒˜¨
converts a list of strings such as ⟨"ab","cde"⟩
into indices ⟨0‿1,0‿1‿2⟩
. Occurrence Count searches for cells of the right argument in the left argument, taking the first match and never matching a cell more than once. When applied with the same list as left and right argument using ˜
, it always matches each cell to itself as any previous cells that match it are used up. The result is the index of each match, giving sequential indices. Each (¨
) simply applies this function to each string.
Group moves each character to the group with the specified index. For example, 0‿1‿0‿1‿2⊔"abcde"
is ⟨"ac","bd","e"⟩
. Since its arguments must be flat lists, not nested ones, it's applied as Group Over (○
) Join, joining both arguments before applying. Instead of grouping and joining, it's also possible to sort by the indices with ⊒˜¨⍋⊸⊏○∾⊢
. But longer.
0 comment threads
JavaScript (Node.js), 53 bytes
f=(a,b=[],c=a.map(([a,...c])=>(b+=[a],c)))=>b&&b+f(c)
0 comment threads
Scala, 60 bytes
_.map(_ map "".+).reduce(_.zipAll(_,"","")map(_+_)).mkString
First, _.map(_ map "".+)
turns every string into a list of strings containing a single character. After, these lists of strings are reduced
by zipping with each other, padding with an empty string if needed, and then concatenating each pair together. At the end, all the strings are joined together with mkString
.
0 comment threads
Python 2, 58 54 bytes
lambda l:''.join(filter(None,sum(map(None,'',*l),())))
Amazingly, shorter than my Python 3 answer! Uses this method to zip unequal-length lists and this method to flatten the result.
0 comment threads
Python 3, 91 90 bytes
Saved one byte thanks to Mark Giraffe in the comments
lambda l:"".join(["".join(x)for x in zip_longest(*l,fillvalue='')])
from itertools import*
Ruby, 48 45 bytes
->w{([p].*w*''=~/$/).zip(*w.map(&:chars))*''}
It could be improved to 32 bytes, if every string is represented as an array of characters:
->w{([p].*w*''=~/$/).zip(*w)*''}
1 comment thread