# String Stairs

Disclaimer: This is the first challenge I propose. Any and all feedback is welcome. If this is a duplicate, please point it out.

Here's a link to the sandbox post.

# Goal

The goal of this challenge is to, given a string and an integer, print the string into blocks of that integer's size. If a word has more characters than the size of a block, print it into a descending "stair" pattern.

# Rules

• The "stair pattern" mentioned above means that, for every block of a same word, that block must begin exactly where the block above it ends. Check the test cases (or ask) if you have any questions.
• If a word is broken in multiple blocks, the following word must be printed with an adequate number of spaces, that is, it must be separated from the preceding word's lowermost block by exactly one whitespace. Check the test cases (or ask) for clarification.
• You can assume the input string will consist only of printable ASCII characters. Also, it will not have multiple whitespaces in a row.
• You can also assume that the integer will always be in the range [1, +∞).
• Trailing whitespace or newlines are allowed.
• You can use any reasonable method for I/O.
• Standard Loopholes apply.
• This is , so shortest code (in bytes, per language) wins. After a week (or so), I'll accept the overall shortest answer.

# Test Cases

(String, Integer) => (Output)

"This is a large string", 3 => Thi is a lar  str
s        ge   ing

"This is an even larger string!", 2 => Th  is an ev  la   st
is        en  rg   ri
er   ng
!
"Ooooh dear, what a big string you have!", 3
=> Ooo  dea  wha a big str   you hav
oh   r,   t         ing       e!

"Staphylococcus saprophyticus", 4 => Stap        sapr
hylo        ophy
cocc        ticu
us          s

"I hope you find this challenge interesting", 2
=> I ho  yo fi  th  ch    in
pe  u  nd  is  al    te
le    re
ng    st
e     in
g

"Well, this test case looks kinda pointless now doesn't it?", 15
=> Well, this test case looks kinda pointless now doesn't it?

"This one looks a lot more interesting!", 1 => T o l a l m i
h n o   o o n
i e o   t r t
s   k     e e
s       r
e
s
t
i
n
g
!
"Keep in mind, people: 'Punctuation! Does! Matter!'", 2
=> Ke  in mi  pe   'P      Do  Ma
ep     nd  op   un      es  tt
,   le   ct      !   er
:    ua          !'
ti
on
!

• can there be an equal amount of leading spaces on each line? Sep 28, 2017 at 20:03
• Bonus: use Zalgo for block size-1 t̳͔̲̻̫̪ḛ͕̦̙͔̩͎͉̝̞ͅx̳͖̬̥̱͓̭̙̤͇̘̲ț͎̣̫̪̩̟̯͈͙͈̗̳͕̹̙̣ͅ Sep 28, 2017 at 20:04
• @dzaima I'm not sure what you mean by that, but I don't see why not. Care to give an example? Sep 28, 2017 at 20:06
• @J.Salle this Sep 28, 2017 at 20:06
• @dzaima yeah sure, no problem. Sep 28, 2017 at 20:07

# Charcoal, 22 bytes

Ｆ⪪θ «↑⸿⸿ＦＬι«Ｍ¬﹪κＩη↙§ικ


Try it online! Link is to verbose version of code. Explanation:

  θ                      First input
⪪                      Split on spaces
Ｆ   «                   Loop over each word
↑⸿⸿                Move the cursor to the top row and two columns right*
ι             Current word
Ｌ              Length
Ｆ  «            Loop over implicit range
κ        Current index
η      Second input
Ｉ       Cast to integer
﹪         Modulo
¬          Logical not
Ｍ     ↙     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:
Ｆ⪪θ «↑⸿⸿Ｆ⪪ιＩη«κ↙ Try it online! Link is to verbose version of code. Explanation:

  θ                 First input
⪪                  Split on spaces
Ｆ   «               Loop over each word
↑⸿⸿            Move the cursor to the top row and two columns right
ι         Current wordＩη
η       Second input
Ｉ        Cast to integer
⪪          Split into substrings of that length
Ｆ    «      Loop over each substring
κ     Print the substring
↙    Move the cursor down and left

• Following the challenge rules, I've accepted this as the shortest answer (as of Oct. 6th, 2017). Oct 6, 2017 at 13:11

# SOGL V0.12, 2827 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 , 187183174166163148145143141140 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));
• You should be able to save a couple bytes by using <"!" instead of ==" ". Sep 29, 2017 at 22:01

## C#, 200 bytes

int x=-2,y=0;Regex.Split(i,@"\s+").ToList().ForEach(w =>{y=0;Regex.Matches(w,".{0,"+s+"}").Cast<Match>().ToList().ForEach(c=>{x+=(y==0?2:s-1);Console.SetCursorPosition(x,y);Console.Write(c);y++;});});


Where the string is specified by i and the size is specified by s.

E.g.

string i = "Staphylococcus saprophyticus";
int s = 2;
int x=-2,y=0;Regex.Split(i,@"\s+").ToList().ForEach(w =>{y=0;Regex.Matches(w,".{0,"+s+"}").Cast<Match>().ToList().ForEach(c=>{x+=(y==0?2:s-1);Console.SetCursorPosition(x,y);Console.Write(c);y++;});});


Basically, the first part Regex.Split uses white spaces to split the sentence into words, and the Regex.Matches splits each word into chunks specified by s. The chunk is written to Cursor position (x,y) where Y is set to 0 for every new word, and x is incremented by 2 for the first chunk of a word and subsequently (s-1) for each chunk.

x starts off it's life at -2 to ensure it's first use is set to 0.

I'm not knowledgable enough in C# trivia to be able to to make it smaller, but suspect it probably can be.

• Any particular reason why you use s for an int, and i for a string, instead of the other way around?
– Tahg
Sep 29, 2017 at 20:14
• Ha ha! No idea - just a quick time waster on my lunch break. I for input and s for size, perhaps? Sep 30, 2017 at 21:33

# Python 2, 203 199 bytes

s,l=input()
S=s.split()
x=[]
for w in S:L=len(w);W=[i/l*~-l*' '+w[i:i+l]for i in range(0,L,l)];x+=[[c.ljust(L/l*~-l+L%l)for c in W+['']*len(s)]]
for l in zip(*x):
L=' '.join(l)
if L.strip():print L


Try it online!

• ' '*(i/l*(l-1)) can be i/l*~-l*' '. Sep 30, 2017 at 9:58
• Oct 6, 2017 at 7:11

## Perl 5, 59 bytes

55 bytes code + 4 for -ai.

$-=s/.{$^I}\K(?=.)/\x1b[1B\x1b[1D/g,print$_,"\x1b[1A"x$-,\$"for@F


Note: the \x1bs are literal ESC chars, but escaped here for easy copy and paste.

This script utilises ANSI escape sequences and requires input via the -i flag which is non-standard. If either of these are not acceptable, please let me know and I'll updated.

### Example runs

perl -ai3 string-stairs.pl <<< 'This is a large string' 2>/dev/null
Thi is a lar  str
s        ge   ing

perl -ai2 string-stairs.pl <<< 'This is an even larger string!' 2>/dev/null
Th  is an ev  la   st
is        en  rg   ri
er   ng
!

perl -ai3 string-stairs.pl <<< 'Ooooh dear, what a big string you have!' 2>/dev/null
Ooo  dea  wha a big str   you hav
oh   r,   t         ing       e!

perl -ai4 string-stairs.pl <<< 'Staphylococcus saprophyticus' 2>/dev/null
Stap        sapr
hylo        ophy
cocc        ticu
us          s

perl -ai2 string-stairs.pl <<< 'I hope you find this challenge interesting' 2>/dev/null
I ho  yo fi  th  ch    in
pe  u  nd  is  al    te
le    re
ng    st
e     in
g

perl -ai15 string-stairs.pl <<< "Well, this test case looks kinda pointless now doesn't it?" 2>/dev/null
Well, this test case looks kinda pointless now doesn't it?

perl -ai1 string-stairs.pl <<< 'This one looks a lot more interesting!' 2>/dev/null
T o l a l m i
h n o   o o n
i e o   t r t
s   k     e e
s       r
e
s
t
i
n
g
!

perl -ai2 string-stairs.pl <<< "Keep in mind, people: 'Punctuation! Does! Matter!'" 2>/dev/null
Ke  in mi  pe   'P       Do   Ma
ep     nd  op   un       es   tt
,   le   ct       !    er
:    ua            !'
ti
on
!