8
\$\begingroup\$

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.

\$\endgroup\$
  • 8
    \$\begingroup\$ Very closely related. \$\endgroup\$ – Martin Ender Aug 22 '15 at 22:02
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – ASCIIThenANSI Aug 23 '15 at 0:20
  • 1
    \$\begingroup\$ I don't think this is a duplicate either. \$\endgroup\$ – orlp Aug 23 '15 at 6:17
  • 1
    \$\begingroup\$ I also do not think this is a duplicate \$\endgroup\$ – Willem Aug 23 '15 at 14:47
4
\$\begingroup\$

Javascript (ES6), 133 132 136 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!

\$\endgroup\$
  • \$\begingroup\$ Instead of join and split, you can use keys or fill \$\endgroup\$ – Downgoat Aug 23 '15 at 3:54
  • 1
    \$\begingroup\$ 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) \$\endgroup\$ – edc65 Aug 23 '15 at 7:54
  • \$\begingroup\$ This does not fulfill the specs - character spacing must vary with the size of the integer. See examples. \$\endgroup\$ – kvill Aug 23 '15 at 10:51
0
\$\begingroup\$

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            
\$\endgroup\$
  • 1
    \$\begingroup\$ Nice, but this does not fulfill the specs - character spacing must vary with the size of the integer. See examples. \$\endgroup\$ – kvill Aug 23 '15 at 10:52
0
\$\begingroup\$

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   
\$\endgroup\$
  • 1
    \$\begingroup\$ This does not fulfill the specs - character spacing must vary with the size of the integer. See examples. \$\endgroup\$ – kvill Aug 23 '15 at 10:52
  • \$\begingroup\$ updated to include character spacing \$\endgroup\$ – Willem Aug 23 '15 at 14:47

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.