Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs

Dashboard
Notifications
Mark all as read
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
["Dyalog APL","Cultivation","Orchard"] → DCOyuralclthoiagvr adAtPiLon

You check any other test cases with this program.

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (5 comments)

13 answers

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?
You might want to add some details to your flag.

1 comment thread

General comments (2 comments)
+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?
You might want to add some details to your flag.

0 comment threads

+4
−0

Risky, 1 byte

0?

Try it online!

0  transpose
?    input
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+3
−0

Haskell, 40 bytes

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

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

General comments (1 comment)
+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?
You might want to add some details to your flag.

1 comment thread

General comments (2 comments)
+2
−0

BQN, 8 bytesSBCS

∾⊒˜¨⊔○∾⊢

Run online! and parsing diagram

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?
You might want to add some details to your flag.

0 comment threads

+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?
You might want to add some details to your flag.

0 comment threads

+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?
You might want to add some details to your flag.

0 comment threads

+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?
You might want to add some details to your flag.

0 comment threads

+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?
You might want to add some details to your flag.

0 comment threads

+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?
You might want to add some details to your flag.

0 comment threads

+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?
You might want to add some details to your flag.

0 comment threads

+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?
You might want to add some details to your flag.

1 comment thread

Gofling tips (2 comments)

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!