Print a Wavy String Line-by-Line

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
8
down vote

favorite












Challenge



Write a program or function that takes in a string s and integer n as parameters. Your program should print (or return) the string when transformed as follows:



Starting in the top-left and moving down and to the right, write s as a wave of height n. Then, from top to bottom, combine each row as a string (without spaces).



Example



Given the string "WATERMELON" and a height of 3:



The wave should look like this:



W R O
A E M L N
T E


Then, combine the rows from top to bottom:



WRO
AEMLN
TE


So, your program should return the string "WROAEMLNTE"



Likewise, "WATERMELON" with height 4 should produce the following wave:



W E
A M L
T R O
E N


Your program should then return the string "WEAMLTROEN"



Rules



Input



Input can be taken in any reasonable format. The string can be in any case you prefer. You may assume that 0 < n <= s.length



Output



Output should consist only of the transformed string (whether returned or printed to STDOUT), plus any trailing newlines.



Scoring



This is code-golf, so shortest answer in bytes wins! Standard loopholes are not allowed.



Test Cases



Input Output

programmingpuzzles, 5 -> piermnlsomgzgapzru
codegolf, 3 -> cgoeofdl
elephant, 4 -> enlatehp
1234567, 3 -> 1524637
qwertyuiop, 1 -> qwertyuiop









share|improve this question























  • Can we assume n>1? Please clarify and if not add a test case
    – Luis Mendo
    8 mins ago











  • Note that at least three current answers fail for n=1. You would have to notify the users if that case needs to be supported
    – Luis Mendo
    2 mins ago










  • You may assume n > 0, but n=1 is a valid case. I will update the question now.
    – Cowabunghole
    2 mins ago














up vote
8
down vote

favorite












Challenge



Write a program or function that takes in a string s and integer n as parameters. Your program should print (or return) the string when transformed as follows:



Starting in the top-left and moving down and to the right, write s as a wave of height n. Then, from top to bottom, combine each row as a string (without spaces).



Example



Given the string "WATERMELON" and a height of 3:



The wave should look like this:



W R O
A E M L N
T E


Then, combine the rows from top to bottom:



WRO
AEMLN
TE


So, your program should return the string "WROAEMLNTE"



Likewise, "WATERMELON" with height 4 should produce the following wave:



W E
A M L
T R O
E N


Your program should then return the string "WEAMLTROEN"



Rules



Input



Input can be taken in any reasonable format. The string can be in any case you prefer. You may assume that 0 < n <= s.length



Output



Output should consist only of the transformed string (whether returned or printed to STDOUT), plus any trailing newlines.



Scoring



This is code-golf, so shortest answer in bytes wins! Standard loopholes are not allowed.



Test Cases



Input Output

programmingpuzzles, 5 -> piermnlsomgzgapzru
codegolf, 3 -> cgoeofdl
elephant, 4 -> enlatehp
1234567, 3 -> 1524637
qwertyuiop, 1 -> qwertyuiop









share|improve this question























  • Can we assume n>1? Please clarify and if not add a test case
    – Luis Mendo
    8 mins ago











  • Note that at least three current answers fail for n=1. You would have to notify the users if that case needs to be supported
    – Luis Mendo
    2 mins ago










  • You may assume n > 0, but n=1 is a valid case. I will update the question now.
    – Cowabunghole
    2 mins ago












up vote
8
down vote

favorite









up vote
8
down vote

favorite











Challenge



Write a program or function that takes in a string s and integer n as parameters. Your program should print (or return) the string when transformed as follows:



Starting in the top-left and moving down and to the right, write s as a wave of height n. Then, from top to bottom, combine each row as a string (without spaces).



Example



Given the string "WATERMELON" and a height of 3:



The wave should look like this:



W R O
A E M L N
T E


Then, combine the rows from top to bottom:



WRO
AEMLN
TE


So, your program should return the string "WROAEMLNTE"



Likewise, "WATERMELON" with height 4 should produce the following wave:



W E
A M L
T R O
E N


Your program should then return the string "WEAMLTROEN"



Rules



Input



Input can be taken in any reasonable format. The string can be in any case you prefer. You may assume that 0 < n <= s.length



Output



Output should consist only of the transformed string (whether returned or printed to STDOUT), plus any trailing newlines.



Scoring



This is code-golf, so shortest answer in bytes wins! Standard loopholes are not allowed.



Test Cases



Input Output

programmingpuzzles, 5 -> piermnlsomgzgapzru
codegolf, 3 -> cgoeofdl
elephant, 4 -> enlatehp
1234567, 3 -> 1524637
qwertyuiop, 1 -> qwertyuiop









share|improve this question















Challenge



Write a program or function that takes in a string s and integer n as parameters. Your program should print (or return) the string when transformed as follows:



Starting in the top-left and moving down and to the right, write s as a wave of height n. Then, from top to bottom, combine each row as a string (without spaces).



Example



Given the string "WATERMELON" and a height of 3:



The wave should look like this:



W R O
A E M L N
T E


Then, combine the rows from top to bottom:



WRO
AEMLN
TE


So, your program should return the string "WROAEMLNTE"



Likewise, "WATERMELON" with height 4 should produce the following wave:



W E
A M L
T R O
E N


Your program should then return the string "WEAMLTROEN"



Rules



Input



Input can be taken in any reasonable format. The string can be in any case you prefer. You may assume that 0 < n <= s.length



Output



Output should consist only of the transformed string (whether returned or printed to STDOUT), plus any trailing newlines.



Scoring



This is code-golf, so shortest answer in bytes wins! Standard loopholes are not allowed.



Test Cases



Input Output

programmingpuzzles, 5 -> piermnlsomgzgapzru
codegolf, 3 -> cgoeofdl
elephant, 4 -> enlatehp
1234567, 3 -> 1524637
qwertyuiop, 1 -> qwertyuiop






code-golf string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 18 secs ago

























asked 1 hour ago









Cowabunghole

548212




548212











  • Can we assume n>1? Please clarify and if not add a test case
    – Luis Mendo
    8 mins ago











  • Note that at least three current answers fail for n=1. You would have to notify the users if that case needs to be supported
    – Luis Mendo
    2 mins ago










  • You may assume n > 0, but n=1 is a valid case. I will update the question now.
    – Cowabunghole
    2 mins ago
















  • Can we assume n>1? Please clarify and if not add a test case
    – Luis Mendo
    8 mins ago











  • Note that at least three current answers fail for n=1. You would have to notify the users if that case needs to be supported
    – Luis Mendo
    2 mins ago










  • You may assume n > 0, but n=1 is a valid case. I will update the question now.
    – Cowabunghole
    2 mins ago















Can we assume n>1? Please clarify and if not add a test case
– Luis Mendo
8 mins ago





Can we assume n>1? Please clarify and if not add a test case
– Luis Mendo
8 mins ago













Note that at least three current answers fail for n=1. You would have to notify the users if that case needs to be supported
– Luis Mendo
2 mins ago




Note that at least three current answers fail for n=1. You would have to notify the users if that case needs to be supported
– Luis Mendo
2 mins ago












You may assume n > 0, but n=1 is a valid case. I will update the question now.
– Cowabunghole
2 mins ago




You may assume n > 0, but n=1 is a valid case. I will update the question now.
– Cowabunghole
2 mins ago










4 Answers
4






active

oldest

votes

















up vote
1
down vote














J, 48 bytes



' '-.~[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@<:@]


Try it online!



Quick initial solution, will golf it (and explain) later.



Here's my slightly cleaned REPL session in the J console:



3-|i:3
0 1 2 3 2 1 0

f=.#@[.]-[::]-[:|i:@]

'watermelon' f 3
0 1 2 3 2 1
f=.#@[$[:}:]-[:|i:@]
'watermelon' f 3
0 1 2 3 2 1 0 1 2 3
f=.1,.~#@[$[:}:]-[:|i:@]

'watermelon' f 3
0 1
1 1
2 1
3 1
2 1
1 1
0 1
1 1
2 1
3 1
f=.(],.' ')#~1,.~#@[$[:}:]-[:|i:@]
f=.(' ',.])#~1,.~#@[$[:}:]-[:|i:@]
'watermelon' f 3
f=.' ',.]
f 'watermelon'
w
a
t
e
r
m
e
l
o
n

f=.(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
'watermelon' f 3
w
a
t
e
r
m
e
l
o
n

f=.[:|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
'watermelon' f 3
w e
a m l
t r o
e n
'programmingpuzzles' f 5
p g
r n p
o i u
g m z s
r m z e
a l
f=.[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
'watermelon' f 3
w e a m l t r o e n
f=.' '-.~[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
'watermelon' f 3
weamltroen
'watermelon' f 4
woalnteemr
f=.' '-.~[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@<:@]
'watermelon' f 3
wroaemlnte
'watermelon' f 4
weamltroen
'programmingpuzzles' f 5
piermnlsomgzgapzru
'codegolf' f 3
cgoeofdl





share|improve this answer





























    up vote
    1
    down vote














    MATL, 12 bytes



    Zv3L)yn:)2$S


    Try it online! Or verify all test cases.



    Explanation



    Zv % Implicit input: number n. Symmetric range
    % STACK: [1 2 3 4 5 4 3 2 1]
    3L % Push [1 -1+1j]. When used as an index, this means 1:end-1
    % STACK: [1 2 3 4 5 4 3 2 1], [1 -1+1j]
    ) % Index. Removes last element
    % STACK: [1 2 3 4 5 4 3 2]
    y % Implict input: string s. Duplicate from below
    % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], 'programmingpuzzles'
    n % Number of elements
    % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], 18
    : % Range
    % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], [1 2 3 ··· 17 18]
    ) % Index modularly
    % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2 1 2 3 4 5 4 3 2 1 2]
    2$S % Two-input sort: stably sorts first input as given by the second
    % STACK: 'piermnlsomgzgapzru'
    % Implicit display





    share|improve this answer





























      up vote
      0
      down vote














      MBASIC, 146 bytes



      1 INPUT S$,N:DIM C$(N):P=1:D=1:FOR I=1 TO LEN(S$):C$(P)=C$(P)+MID$(S$,I,1)
      2 IF P+D>N OR P+D=0 THEN D=-D
      3 P=P+D:NEXT:FOR I=1 TO N:PRINT C$(I);:NEXT


      Output:



      ? programmingpuzzles, 5
      piermnlsomgzgapzru

      ? codegolf, 3
      cgoeofdl

      ? elephant, 4
      enlatehp

      ? 1234567, 3
      1524637

      ? WATERMELON, 4
      WEAMLTROEN





      share|improve this answer



























        up vote
        0
        down vote














        Python 2, 119 108 98 bytes





        lambda s,n:''.join(s[j]for i in range(n)for j in range(len(s))if 0in((j-i)%(2*n-2),(j+i)%(2*n-2)))


        Try it online!






        share|improve this answer






















          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "200"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f174116%2fprint-a-wavy-string-line-by-line%23new-answer', 'question_page');

          );

          Post as a guest






























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote














          J, 48 bytes



          ' '-.~[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@<:@]


          Try it online!



          Quick initial solution, will golf it (and explain) later.



          Here's my slightly cleaned REPL session in the J console:



          3-|i:3
          0 1 2 3 2 1 0

          f=.#@[.]-[::]-[:|i:@]

          'watermelon' f 3
          0 1 2 3 2 1
          f=.#@[$[:}:]-[:|i:@]
          'watermelon' f 3
          0 1 2 3 2 1 0 1 2 3
          f=.1,.~#@[$[:}:]-[:|i:@]

          'watermelon' f 3
          0 1
          1 1
          2 1
          3 1
          2 1
          1 1
          0 1
          1 1
          2 1
          3 1
          f=.(],.' ')#~1,.~#@[$[:}:]-[:|i:@]
          f=.(' ',.])#~1,.~#@[$[:}:]-[:|i:@]
          'watermelon' f 3
          f=.' ',.]
          f 'watermelon'
          w
          a
          t
          e
          r
          m
          e
          l
          o
          n

          f=.(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
          'watermelon' f 3
          w
          a
          t
          e
          r
          m
          e
          l
          o
          n

          f=.[:|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
          'watermelon' f 3
          w e
          a m l
          t r o
          e n
          'programmingpuzzles' f 5
          p g
          r n p
          o i u
          g m z s
          r m z e
          a l
          f=.[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
          'watermelon' f 3
          w e a m l t r o e n
          f=.' '-.~[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
          'watermelon' f 3
          weamltroen
          'watermelon' f 4
          woalnteemr
          f=.' '-.~[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@<:@]
          'watermelon' f 3
          wroaemlnte
          'watermelon' f 4
          weamltroen
          'programmingpuzzles' f 5
          piermnlsomgzgapzru
          'codegolf' f 3
          cgoeofdl





          share|improve this answer


























            up vote
            1
            down vote














            J, 48 bytes



            ' '-.~[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@<:@]


            Try it online!



            Quick initial solution, will golf it (and explain) later.



            Here's my slightly cleaned REPL session in the J console:



            3-|i:3
            0 1 2 3 2 1 0

            f=.#@[.]-[::]-[:|i:@]

            'watermelon' f 3
            0 1 2 3 2 1
            f=.#@[$[:}:]-[:|i:@]
            'watermelon' f 3
            0 1 2 3 2 1 0 1 2 3
            f=.1,.~#@[$[:}:]-[:|i:@]

            'watermelon' f 3
            0 1
            1 1
            2 1
            3 1
            2 1
            1 1
            0 1
            1 1
            2 1
            3 1
            f=.(],.' ')#~1,.~#@[$[:}:]-[:|i:@]
            f=.(' ',.])#~1,.~#@[$[:}:]-[:|i:@]
            'watermelon' f 3
            f=.' ',.]
            f 'watermelon'
            w
            a
            t
            e
            r
            m
            e
            l
            o
            n

            f=.(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
            'watermelon' f 3
            w
            a
            t
            e
            r
            m
            e
            l
            o
            n

            f=.[:|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
            'watermelon' f 3
            w e
            a m l
            t r o
            e n
            'programmingpuzzles' f 5
            p g
            r n p
            o i u
            g m z s
            r m z e
            a l
            f=.[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
            'watermelon' f 3
            w e a m l t r o e n
            f=.' '-.~[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
            'watermelon' f 3
            weamltroen
            'watermelon' f 4
            woalnteemr
            f=.' '-.~[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@<:@]
            'watermelon' f 3
            wroaemlnte
            'watermelon' f 4
            weamltroen
            'programmingpuzzles' f 5
            piermnlsomgzgapzru
            'codegolf' f 3
            cgoeofdl





            share|improve this answer
























              up vote
              1
              down vote










              up vote
              1
              down vote










              J, 48 bytes



              ' '-.~[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@<:@]


              Try it online!



              Quick initial solution, will golf it (and explain) later.



              Here's my slightly cleaned REPL session in the J console:



              3-|i:3
              0 1 2 3 2 1 0

              f=.#@[.]-[::]-[:|i:@]

              'watermelon' f 3
              0 1 2 3 2 1
              f=.#@[$[:}:]-[:|i:@]
              'watermelon' f 3
              0 1 2 3 2 1 0 1 2 3
              f=.1,.~#@[$[:}:]-[:|i:@]

              'watermelon' f 3
              0 1
              1 1
              2 1
              3 1
              2 1
              1 1
              0 1
              1 1
              2 1
              3 1
              f=.(],.' ')#~1,.~#@[$[:}:]-[:|i:@]
              f=.(' ',.])#~1,.~#@[$[:}:]-[:|i:@]
              'watermelon' f 3
              f=.' ',.]
              f 'watermelon'
              w
              a
              t
              e
              r
              m
              e
              l
              o
              n

              f=.(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
              'watermelon' f 3
              w
              a
              t
              e
              r
              m
              e
              l
              o
              n

              f=.[:|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
              'watermelon' f 3
              w e
              a m l
              t r o
              e n
              'programmingpuzzles' f 5
              p g
              r n p
              o i u
              g m z s
              r m z e
              a l
              f=.[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
              'watermelon' f 3
              w e a m l t r o e n
              f=.' '-.~[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
              'watermelon' f 3
              weamltroen
              'watermelon' f 4
              woalnteemr
              f=.' '-.~[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@<:@]
              'watermelon' f 3
              wroaemlnte
              'watermelon' f 4
              weamltroen
              'programmingpuzzles' f 5
              piermnlsomgzgapzru
              'codegolf' f 3
              cgoeofdl





              share|improve this answer















              J, 48 bytes



              ' '-.~[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@<:@]


              Try it online!



              Quick initial solution, will golf it (and explain) later.



              Here's my slightly cleaned REPL session in the J console:



              3-|i:3
              0 1 2 3 2 1 0

              f=.#@[.]-[::]-[:|i:@]

              'watermelon' f 3
              0 1 2 3 2 1
              f=.#@[$[:}:]-[:|i:@]
              'watermelon' f 3
              0 1 2 3 2 1 0 1 2 3
              f=.1,.~#@[$[:}:]-[:|i:@]

              'watermelon' f 3
              0 1
              1 1
              2 1
              3 1
              2 1
              1 1
              0 1
              1 1
              2 1
              3 1
              f=.(],.' ')#~1,.~#@[$[:}:]-[:|i:@]
              f=.(' ',.])#~1,.~#@[$[:}:]-[:|i:@]
              'watermelon' f 3
              f=.' ',.]
              f 'watermelon'
              w
              a
              t
              e
              r
              m
              e
              l
              o
              n

              f=.(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
              'watermelon' f 3
              w
              a
              t
              e
              r
              m
              e
              l
              o
              n

              f=.[:|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
              'watermelon' f 3
              w e
              a m l
              t r o
              e n
              'programmingpuzzles' f 5
              p g
              r n p
              o i u
              g m z s
              r m z e
              a l
              f=.[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
              'watermelon' f 3
              w e a m l t r o e n
              f=.' '-.~[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@]
              'watermelon' f 3
              weamltroen
              'watermelon' f 4
              woalnteemr
              f=.' '-.~[:,@|:(' ',.[)#"1~1,.~#@[$[:}:]-[:|i:@<:@]
              'watermelon' f 3
              wroaemlnte
              'watermelon' f 4
              weamltroen
              'programmingpuzzles' f 5
              piermnlsomgzgapzru
              'codegolf' f 3
              cgoeofdl






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 22 mins ago

























              answered 29 mins ago









              Galen Ivanov

              5,2821931




              5,2821931




















                  up vote
                  1
                  down vote














                  MATL, 12 bytes



                  Zv3L)yn:)2$S


                  Try it online! Or verify all test cases.



                  Explanation



                  Zv % Implicit input: number n. Symmetric range
                  % STACK: [1 2 3 4 5 4 3 2 1]
                  3L % Push [1 -1+1j]. When used as an index, this means 1:end-1
                  % STACK: [1 2 3 4 5 4 3 2 1], [1 -1+1j]
                  ) % Index. Removes last element
                  % STACK: [1 2 3 4 5 4 3 2]
                  y % Implict input: string s. Duplicate from below
                  % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], 'programmingpuzzles'
                  n % Number of elements
                  % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], 18
                  : % Range
                  % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], [1 2 3 ··· 17 18]
                  ) % Index modularly
                  % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2 1 2 3 4 5 4 3 2 1 2]
                  2$S % Two-input sort: stably sorts first input as given by the second
                  % STACK: 'piermnlsomgzgapzru'
                  % Implicit display





                  share|improve this answer


























                    up vote
                    1
                    down vote














                    MATL, 12 bytes



                    Zv3L)yn:)2$S


                    Try it online! Or verify all test cases.



                    Explanation



                    Zv % Implicit input: number n. Symmetric range
                    % STACK: [1 2 3 4 5 4 3 2 1]
                    3L % Push [1 -1+1j]. When used as an index, this means 1:end-1
                    % STACK: [1 2 3 4 5 4 3 2 1], [1 -1+1j]
                    ) % Index. Removes last element
                    % STACK: [1 2 3 4 5 4 3 2]
                    y % Implict input: string s. Duplicate from below
                    % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], 'programmingpuzzles'
                    n % Number of elements
                    % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], 18
                    : % Range
                    % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], [1 2 3 ··· 17 18]
                    ) % Index modularly
                    % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2 1 2 3 4 5 4 3 2 1 2]
                    2$S % Two-input sort: stably sorts first input as given by the second
                    % STACK: 'piermnlsomgzgapzru'
                    % Implicit display





                    share|improve this answer
























                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote










                      MATL, 12 bytes



                      Zv3L)yn:)2$S


                      Try it online! Or verify all test cases.



                      Explanation



                      Zv % Implicit input: number n. Symmetric range
                      % STACK: [1 2 3 4 5 4 3 2 1]
                      3L % Push [1 -1+1j]. When used as an index, this means 1:end-1
                      % STACK: [1 2 3 4 5 4 3 2 1], [1 -1+1j]
                      ) % Index. Removes last element
                      % STACK: [1 2 3 4 5 4 3 2]
                      y % Implict input: string s. Duplicate from below
                      % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], 'programmingpuzzles'
                      n % Number of elements
                      % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], 18
                      : % Range
                      % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], [1 2 3 ··· 17 18]
                      ) % Index modularly
                      % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2 1 2 3 4 5 4 3 2 1 2]
                      2$S % Two-input sort: stably sorts first input as given by the second
                      % STACK: 'piermnlsomgzgapzru'
                      % Implicit display





                      share|improve this answer















                      MATL, 12 bytes



                      Zv3L)yn:)2$S


                      Try it online! Or verify all test cases.



                      Explanation



                      Zv % Implicit input: number n. Symmetric range
                      % STACK: [1 2 3 4 5 4 3 2 1]
                      3L % Push [1 -1+1j]. When used as an index, this means 1:end-1
                      % STACK: [1 2 3 4 5 4 3 2 1], [1 -1+1j]
                      ) % Index. Removes last element
                      % STACK: [1 2 3 4 5 4 3 2]
                      y % Implict input: string s. Duplicate from below
                      % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], 'programmingpuzzles'
                      n % Number of elements
                      % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], 18
                      : % Range
                      % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2], [1 2 3 ··· 17 18]
                      ) % Index modularly
                      % STACK: 'programmingpuzzles', [1 2 3 4 5 4 3 2 1 2 3 4 5 4 3 2 1 2]
                      2$S % Two-input sort: stably sorts first input as given by the second
                      % STACK: 'piermnlsomgzgapzru'
                      % Implicit display






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 15 mins ago

























                      answered 20 mins ago









                      Luis Mendo

                      73.1k885286




                      73.1k885286




















                          up vote
                          0
                          down vote














                          MBASIC, 146 bytes



                          1 INPUT S$,N:DIM C$(N):P=1:D=1:FOR I=1 TO LEN(S$):C$(P)=C$(P)+MID$(S$,I,1)
                          2 IF P+D>N OR P+D=0 THEN D=-D
                          3 P=P+D:NEXT:FOR I=1 TO N:PRINT C$(I);:NEXT


                          Output:



                          ? programmingpuzzles, 5
                          piermnlsomgzgapzru

                          ? codegolf, 3
                          cgoeofdl

                          ? elephant, 4
                          enlatehp

                          ? 1234567, 3
                          1524637

                          ? WATERMELON, 4
                          WEAMLTROEN





                          share|improve this answer
























                            up vote
                            0
                            down vote














                            MBASIC, 146 bytes



                            1 INPUT S$,N:DIM C$(N):P=1:D=1:FOR I=1 TO LEN(S$):C$(P)=C$(P)+MID$(S$,I,1)
                            2 IF P+D>N OR P+D=0 THEN D=-D
                            3 P=P+D:NEXT:FOR I=1 TO N:PRINT C$(I);:NEXT


                            Output:



                            ? programmingpuzzles, 5
                            piermnlsomgzgapzru

                            ? codegolf, 3
                            cgoeofdl

                            ? elephant, 4
                            enlatehp

                            ? 1234567, 3
                            1524637

                            ? WATERMELON, 4
                            WEAMLTROEN





                            share|improve this answer






















                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote










                              MBASIC, 146 bytes



                              1 INPUT S$,N:DIM C$(N):P=1:D=1:FOR I=1 TO LEN(S$):C$(P)=C$(P)+MID$(S$,I,1)
                              2 IF P+D>N OR P+D=0 THEN D=-D
                              3 P=P+D:NEXT:FOR I=1 TO N:PRINT C$(I);:NEXT


                              Output:



                              ? programmingpuzzles, 5
                              piermnlsomgzgapzru

                              ? codegolf, 3
                              cgoeofdl

                              ? elephant, 4
                              enlatehp

                              ? 1234567, 3
                              1524637

                              ? WATERMELON, 4
                              WEAMLTROEN





                              share|improve this answer













                              MBASIC, 146 bytes



                              1 INPUT S$,N:DIM C$(N):P=1:D=1:FOR I=1 TO LEN(S$):C$(P)=C$(P)+MID$(S$,I,1)
                              2 IF P+D>N OR P+D=0 THEN D=-D
                              3 P=P+D:NEXT:FOR I=1 TO N:PRINT C$(I);:NEXT


                              Output:



                              ? programmingpuzzles, 5
                              piermnlsomgzgapzru

                              ? codegolf, 3
                              cgoeofdl

                              ? elephant, 4
                              enlatehp

                              ? 1234567, 3
                              1524637

                              ? WATERMELON, 4
                              WEAMLTROEN






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 16 mins ago









                              wooshinyobject

                              714




                              714




















                                  up vote
                                  0
                                  down vote














                                  Python 2, 119 108 98 bytes





                                  lambda s,n:''.join(s[j]for i in range(n)for j in range(len(s))if 0in((j-i)%(2*n-2),(j+i)%(2*n-2)))


                                  Try it online!






                                  share|improve this answer


























                                    up vote
                                    0
                                    down vote














                                    Python 2, 119 108 98 bytes





                                    lambda s,n:''.join(s[j]for i in range(n)for j in range(len(s))if 0in((j-i)%(2*n-2),(j+i)%(2*n-2)))


                                    Try it online!






                                    share|improve this answer
























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote










                                      Python 2, 119 108 98 bytes





                                      lambda s,n:''.join(s[j]for i in range(n)for j in range(len(s))if 0in((j-i)%(2*n-2),(j+i)%(2*n-2)))


                                      Try it online!






                                      share|improve this answer















                                      Python 2, 119 108 98 bytes





                                      lambda s,n:''.join(s[j]for i in range(n)for j in range(len(s))if 0in((j-i)%(2*n-2),(j+i)%(2*n-2)))


                                      Try it online!







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited 10 mins ago

























                                      answered 20 mins ago









                                      TFeld

                                      12.6k2835




                                      12.6k2835



























                                           

                                          draft saved


                                          draft discarded















































                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f174116%2fprint-a-wavy-string-line-by-line%23new-answer', 'question_page');

                                          );

                                          Post as a guest













































































                                          Comments

                                          Popular posts from this blog

                                          What does second last employer means? [closed]

                                          List of Gilmore Girls characters

                                          Confectionery