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

Dashboard
Notifications
Mark all as read
Challenges

Weave Strings Together

+4
−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?

3 comments

Ugh, the "strings may have spaces" and the way you don't pad the strings makes it unusually annoying. Quintec‭ 7 months ago

Can we take the strings as an array of characters? Shaggy‭ 7 months ago

@Shaggy yes, you can Razetime‭ 7 months ago

6 answers

+4
−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 comments

+4
−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.

2 comments

doesn't Jelly smash print, removing the need for F? Razetime‭ about 2 months ago

@Razetime Indeed it does, thanks! caird coinheringaahing‭ about 1 month ago

+2
−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

35 bytes Hakerh400‭ 4 months ago

+2
−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.

2 comments

Õc with the -P flag would work, taking the strings as character arrays. Shaggy‭ 7 months ago

@Shaggy cheers, thanks Quintec‭ 7 months ago

+1
−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 comments

+1
−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 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!