Build me some string stairs
Charcoal, 22 bytes
F⪪θ «↑⸿⸿FLι«M¬﹪κIη↙§ικ
Try it online! Link is to verbose version of code. Explanation:
θ First input
⪪ Split on spaces
F « Loop over each word
↑⸿⸿ Move the cursor to the top row and two columns right*
ι Current word
L Length
F « Loop over implicit range
κ Current index
η Second input
I Cast to integer
﹪ Modulo
¬ Logical not
M ↙ Move that many characters down and left
ι Current word
κ Current index
§ Index into word and implicitly print
*More precisely, "move to the start of the next line twice, but as if the canvas was rotated." Edit: In between this challenge being set and this answer being accepted, Charcoal actually acquired a means of splitting a string into pairs of characters, reducing the code 16 bytes:
F⪪θ «↑⸿⸿F⪪ιIη«κ↙
Try it online! Link is to verbose version of code. Explanation:
θ First input
⪪ Split on spaces
F « Loop over each word
↑⸿⸿ Move the cursor to the top row and two columns right
ι Current wordIη
η Second input
I Cast to integer
⪪ Split into substrings of that length
F « Loop over each substring
κ Print the substring
↙ Move the cursor down and left
SOGL V0.12, 28 27 26 bytes
ā,θ{0Eā;{ēb÷eb‰⁴bH*+I;Iž}┼
Try it Here!
I did implement ‰
while making this, but the documentation for it existed before.
Explanation:
ā push an empty array - the main canvas
, push the first input
θ{ for each word (pushing the word each time)
0E set the variable E to 0
ā; below the current word place an empty array - current word canvas
{ } for each character of the word
ēb÷ push (E++ // B) - note that E is incremented after being used
eb‰ push E positive modulo B - like regular modulo but in the 0 output case it gives B
⁴ duplicate the item below ToS
bH push B-1
* multiply [(E++ // B) and B-1]
+ add [that multiplication to E‰B] - current letters X position
I increase the X position to have one leading space row
; swap top 2 items - current X position and (E++ // B)
I increase to create current letters Y position
ž in those positions insert the current letter in the current words canvas
┼ append to main canvas current word canvas horizontally
Javascript ES6 , 187 183 174 166 163 148 145 143 141 140 138 bytes
- for readability added some bytes in the code and removed them in the bytes count
- instead of s="",j=0 i did j=s=""
- instead of for(i in s) - regular for loop - removed 1 byte
- using already generated values in the indexers of the arrays - removed 8 bytes
- using already with value i= s.length (from the first loop) in the eval - instead of the real array length - causes trailing space which are allowed
- using map of S instead of eval - reduces by 3 bytes
- using fill instead on initializing empty array- so there is not need for the loop in the map result
- could replace || with | - reduced by 2 bytes
- thanks to @Justin Mariner - replace occurrences ==" " with <"!" reduces 2 bytes
- moved the conditions from the a[I] to the other statement to reduces one " u<"!" " - reduces 2 bytes
- instead of (I+=2,j=0) - j=!(I+=2) - reduced 1 byte
- "for of" instead of for
F=(s,n)=>{R=[I=j=i=0] for(u of s) a=R[z=u<"!"?j=!(I+=2):(j%n&&I++,j++/n|0)]=R[z]||[...s].fill` `,a[I]=u return R.map(x=>x.join``).join` `} console.log(F("This is a large string", 3)); console.log(F("This is an even larger string!", 2)); console.log(F("Ooooh dear, what a big string you have!", 3)); console.log(F("Staphylococcus saprophyticus", 4)); console.log(F("I hope you find this challenge interesting", 2)); console.log(F("Well, this test case looks kinda pointless now doesn't it?", 15)); console.log(F("This one looks a lot more interesting!", 1)) console.log(F("Keep in mind, people: 'Punctuation! Does! Matter!'", 2));