24
\$\begingroup\$

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

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
\$\endgroup\$
  • \$\begingroup\$ Following the challenge rules, I've accepted this as the shortest answer (as of Oct. 6th, 2017). \$\endgroup\$ – J. Sallé Oct 6 '17 at 13:11
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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));
\$\endgroup\$
  • 1
    \$\begingroup\$ You should be able to save a couple bytes by using <"!" instead of ==" ". \$\endgroup\$ – Justin Mariner Sep 29 '17 at 22:01
2
\$\begingroup\$

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.

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

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!

\$\endgroup\$
  • \$\begingroup\$ ' '*(i/l*(l-1)) can be i/l*~-l*' '. \$\endgroup\$ – Jonathan Frech Sep 30 '17 at 9:58
  • \$\begingroup\$ 202 bytes. \$\endgroup\$ – Jonathan Frech Oct 6 '17 at 7:11
1
\$\begingroup\$

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
                      !
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.