# Twisting Words!

Given a string and a positive integer. You must twist the string, back and forth.

### Example Input / Output

Input

Programming Puzzles & Code Golf
4


Output

Prog
mmar
ing
zzuP
les
oC &
de G
flo


## Input

The input can be taken in through STDIN, or function argument. The input will consist of a string and a positive integer, n. The integer will determine the length of each twisted line.

The string is twisted back-and-forth. An input of HELLO, WORLD! and 5 would look like:

## Output

The output will be the twisted text. It may not any trailing whitespace. If the input string length is not divisible be the line length, add a space until the line is filled:

An example of this:

Input

Hello, World!
5


Output (Note the whitespace at the very end)

Hello
roW ,
ld!

• – Caridorc Aug 23 '15 at 20:59
• It may not any trailing whitespace. Golfed away a verb? – Adám May 13 '18 at 21:19
• May we return a list of lines? – Adám May 13 '18 at 21:19

## Shell/Bash, 135 bytes

s=0;while true;do ((s%($2*2)))&&{ echo "${1:s:$2}"|rev;s=$((s+$2)); }||{ echo "${1:s:$2}";s=$((s+$2)); };((s>=${#1}))&&{ exit 1; };done

• Would it be possible to replace the true with either 1 or 1=1? – Taylor Scott May 19 '18 at 22:51
• Yes, you can do while :. I didn't see this answer when I composed my answer, but it has very similar logic, and I borrowed the more concise arithmetic evaluation to reduce my version from 83 bytes to 74. – Adam Katz Aug 24 '18 at 18:51

# Python 2, 86 bytes

Here's my answer (borrowing some input processing from @mbomb007). Unfortunately can't define the len(s) as a variable as the length of the string changes after padding with spaces. Now also using rjust() instead of ljust() as @AlexN suggested.

s,n=input()
k=n
while k<=len(s):print s[k-n:k]+'\n'+s[k+n-1:k-1:-1].rjust(n);k+=2*n


## Groovy, 68 bytes

f=-n;s.eachMatch(/.{1,$n}/){printf "%${f}s%n",f>0?it[-1..0]:it;f=-f}


revised after I realized the initial version didn't handle padding correctly.

Try it online!

# Java 8, 197146 136 bytes

s->n->{String r="",t,u;for(int l=s.length,i=0,j;i<l/n*n+n;r+=t+"\n",i=j)for(t="",j=i;j<i+n;t=j++/n%2<1?t+u:u+t)u=j<l?s[j]:" ";return r;}


Try it online.

Explanation:

s->n->{                 // Method with String and integer parameters and String return-type
String r="",          //  Result-String, starting empty
t,u;           //  Temp-Strings
for(int l=s.length,   //  Length of the input-String
i=0,j;i<l/n*n+n   //  Loop i in the range [0, l + whatever to make blocks of n)
;                 //    After every iteration:
r+=t+"\n",       //     Append the result-String with t and a new-line
i=j)             //     And set i to j
for(t="",           //   Reset t
j=i;j<i+n;      //   Inner loop j in the range [i, i+n)
;t=j++/n%2<1?   //     After every iteration: If the k'th block of n is even:
t+u         //      Append u to t
:            //     Else (it's odd):
u+t)        //      Prepend u to t
u=j<l?            //    If j is still within bounds:
s[j]           //     Set u to the current character
:               //    Else:
" ";           //     Set u to a space
return r;}            //  Return the result-String


# C (gcc), 145 141 bytes

Not allowing trailing whitespace made it clunkier than otherwise.

Thanks to ceilingcat for the tip that led me on the track to save 4 bytes.

f(s,n,l,i,j)char*s;{for(l=strlen(s),i=0;i<l;i+=2*n)if(printf("%-*.*s\n",n,n,s+i),i+n<l){for(j=i+2*n;j>i+n;)putchar(l/j--?s[j]:32);puts("");}}


Try it online!

# Japt, 14 bytes

óV y £Yv ?X:Xw


Try it online!