Challenges

# Weave Strings Together

+9
−0

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

Without the outputs

["kino","cinema","movie"] → kcmiionnvoeimea
["code","golf"] → cgoodlef
["Hi","","There"] → HTihere
["Explanation"] → Explanation


You check any other test cases with this program.

Why does this post require moderator attention?
Why should this post be closed?

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+6
−0

# Jelly, 2 1 byte

Z


Try it online!

-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
Why does this post require moderator attention?

+5
−0

# APL (Dyalog Unicode), 10 bytes

Full program. Prompts for list of strings from stdin.

0~⍨,⍉↑0,¨⎕


Try it online!

⎕ 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

Why does this post require moderator attention?

+4
−0

# Risky, 1 byte

0?


Try it online!

0  transpose
?    input
Why does this post require moderator attention?

+3
−0

f((h:t):r)=h:f(r++[t])
f(_:r)=f r
f _=[]


Try it online!

Why does this post require moderator attention?

+3
−0

# Japt-P, 7 2 bytes

Õc


Takes input as character arrays. -5 bytes thanks to @Shaggy

Try it

Why does this post require moderator attention?

+2
−0

# BQN, 8 bytesSBCS

∾⊒˜¨⊔○∾⊢


The idea here is to build a list of groups, with group i containing the ith 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.

Why does this post require moderator attention?

+2
−0

# Python 2, 58 54 bytes

lambda l:''.join(filter(None,sum(map(None,'',*l),())))


Try it online!

Amazingly, shorter than my Python 3 answer! Uses this method to zip unequal-length lists and this method to flatten the result.

Why does this post require moderator attention?

+2
−0

# JavaScript (Node.js), 53 bytes

f=(a,b=[],c=a.map(([a,...c])=>(b+=[a],c)))=>b&&b+f(c)


Try it online!

Why does this post require moderator attention?

+2
−0

# Python 3, 72 69 bytes

lambda l:''.join(sum(zip(*[[*i]+['']*max(map(len,l))for i in l]),()))


Try it online!

Uses this method to flat zip, with modifications to pad the shorter lists.

Why does this post require moderator attention?

+2
−0

# 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"


Try it online!

Why does this post require moderator attention?

+2
−0

# Scala, 60 bytes

_.map(_ map "".+).reduce(_.zipAll(_,"","")map(_+_)).mkString


Try it in Scastie!

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.

Why does this post require moderator attention?

+1
−0

# Python 3, 155 bytes

r=range;l=len
def f(a):
x=0
for i in r(l(a)):
if x<l(a[i]):x=l(a[i])
for i in r(x):
for j in r(l(a)):
try:print(end=a[j][i])
except:continue


Try it online!

It took some time for this whole thing to work. I had trouble figuring out how to set the characters in place. I asked if errors are allowed as long as the output is what you wanted, looks like I don't need to.

Why does this post require moderator attention?

+1
−0

# 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*


Try it online!

Why does this post require moderator attention?