Zig-zag strings

Given a string and a positive integer, output the string in a zig-zag wave pattern with a peak amplitude and character spacing equal to the integer.

This is similar to this question but also includes character spacing.

Input

Input can be read from STDIN or taken as function arguments. Input is a string without newlines and a positive integer.

Output

Expected output is the string transformed into a zig-zag wave pattern -- starting upwards -- with the first character on the base line. The wave patter must have an amplitude and character spacing equal to the integer.

Examples

Input

Programming Puzzles & Code Golf
2


Output

    o               g                s
r   g           n                e               e   G
P       r       i       P        l       &       d       o
a   m           u    z               o           l
m                z               C               f


Input

Programming Puzzles & Code Golf
1


Output

 r   a   n   u   e       e   l
P o r m i g P z l s & C d   o f
g   m       z       o   G


This is code golf -- shortest program wins.

• Very closely related. Aug 22, 2015 at 22:02
• I don't think this is a duplicate of the challenge Martin mentioned, since this challenge has a varying wave size, slightly different rules for making waves, and does not require printing the program's language. Aug 23, 2015 at 0:20
• I don't think this is a duplicate either.
– orlp
Aug 23, 2015 at 6:17
• I also do not think this is a duplicate Aug 23, 2015 at 14:47

Javascript (ES6), 133132136 134 bytes

(s,n,o=Array(2*n+1).fill)=>[...s].map((v,i)=>o=o.map((w,a)=>w+(a-((j=(i+3*n)%(4*n))>2*n?4*n-j:j)?" ":v)+" ".repeat(n-1)))&&o.join



Defines an anonymous function. To call it, add f= to the beginning to call the function f. Then, it can be tested with console.log(f("Programming Puzzles & Code Golf", 2))

Thanks to @vihan and @edc65 for helping save some bytes! :)

@kvill: The code has been modified to address the variable character spacing requirement in the spec. Didn't notice that at first!

• Instead of join and split, you can use keys or fill Aug 23, 2015 at 3:54
• Well done, can be 118: (s,n,o=Array(2*n+1).fill'')=>[...s].map((v,i)=>o=o.map((w,a)=>a-((j=(i+3*n)%(4*n))>2*n?4*n-j:j)?w+" ":w+v))&&o.join'' (fill and join using template string with no brackets) Aug 23, 2015 at 7:54
• This does not fulfill the specs - character spacing must vary with the size of the integer. See examples. Aug 23, 2015 at 10:51

J, 75 bytes

f=:4 :0
|:((]*>:@i.@#)((+:<:b)|i.#y){x|.}:(],}.@|.)|.=/~i.b=.>:+:x){' ',y
)


Here is function in use

   7 f'code golf is awesome and I like it'

l
o f
g                           t
i                     i
e       s
d                           e
o           a               k
c             w             i
e           l
s
o       I
m
e   d
n
a

• Nice, but this does not fulfill the specs - character spacing must vary with the size of the integer. See examples. Aug 23, 2015 at 10:52

Python 2, 151

Here is my answer using Python 2:

s,n=input()
a=2*n+1
l,j,o=['',]*a,n,1
for c in s:
for k in range(a):l[k]+=c+(n-1)*' ' if j==k else' '*n
j-=o
if j in [0,2*n]:o=-o
print'\n'.join(l)


Sample output:

         t                                   e                                   h                                   t                                   e
s     i                             v     ,                             t     e                             s     i                             v
e           n                       a                                                                       e           n                       a
t                 g                 w                 t                 g                 w                 t                 g                 w
e           n                       a
t     e                             s     i                             v     ,                             t     e
h                                   t                                   e                                   h


and

 r   a   n   u   e       e   l
P o r m i g P z l s & C d   o f
g   m       z       o   G

• This does not fulfill the specs - character spacing must vary with the size of the integer. See examples. Aug 23, 2015 at 10:52
• updated to include character spacing Aug 23, 2015 at 14:47