Make a simple word wrapper











up vote
2
down vote

favorite












(Note: This is my first ever code golf question, but as far as I can tell, nobody else has done exactly this, so I should be good.)



Your task is to make a program or function that takes in a string s and an integer n, and returns or outputs that text wrapped into multiple lines. Each word must be wholly on a line; i.e. no words split in the middle.



Example:



s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget erat lectus. Morbi mi mi, fringilla sed suscipit ullamcorper, tristique at mauris. Morbi non commodo nibh. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed at iaculis mauris. Praesent a sem augue. Nulla lectus sapien, auctor nec pharetra eu, tincidunt ac diam. Sed ligula arcu, aliquam quis velit aliquam, dictum varius erat." 
n = 50

output:
Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Sed eget erat lectus. Morbi mi mi, fringilla
sed suscipit ullamcorper, tristique at mauris.
Morbi non commodo nibh. Pellentesque habitant
morbi tristique senectus et netus et malesuada
fames ac turpis egestas. Sed at iaculis mauris.
Praesent a sem augue. Nulla lectus sapien, auctor
nec pharetra eu, tincidunt ac diam. Sed ligula
arcu, aliquam quis velit aliquam, dictum varius
erat.


Your output can be an array of strings or a single string with line breaks. Also, you can assume n will be above 10, so don't worry about dealing with weird cases.



Standard I/O rules apply, and standard loopholes are prohibited.



Since this is code-golf, the shortes solution in bytes wins.



Here is an example program in Python that would work.










share|improve this question


















  • 1




    Can we assume no words will be longer than n?
    – lirtosiast
    1 hour ago










  • Format a list of words
    – user202729
    1 hour ago

















up vote
2
down vote

favorite












(Note: This is my first ever code golf question, but as far as I can tell, nobody else has done exactly this, so I should be good.)



Your task is to make a program or function that takes in a string s and an integer n, and returns or outputs that text wrapped into multiple lines. Each word must be wholly on a line; i.e. no words split in the middle.



Example:



s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget erat lectus. Morbi mi mi, fringilla sed suscipit ullamcorper, tristique at mauris. Morbi non commodo nibh. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed at iaculis mauris. Praesent a sem augue. Nulla lectus sapien, auctor nec pharetra eu, tincidunt ac diam. Sed ligula arcu, aliquam quis velit aliquam, dictum varius erat." 
n = 50

output:
Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Sed eget erat lectus. Morbi mi mi, fringilla
sed suscipit ullamcorper, tristique at mauris.
Morbi non commodo nibh. Pellentesque habitant
morbi tristique senectus et netus et malesuada
fames ac turpis egestas. Sed at iaculis mauris.
Praesent a sem augue. Nulla lectus sapien, auctor
nec pharetra eu, tincidunt ac diam. Sed ligula
arcu, aliquam quis velit aliquam, dictum varius
erat.


Your output can be an array of strings or a single string with line breaks. Also, you can assume n will be above 10, so don't worry about dealing with weird cases.



Standard I/O rules apply, and standard loopholes are prohibited.



Since this is code-golf, the shortes solution in bytes wins.



Here is an example program in Python that would work.










share|improve this question


















  • 1




    Can we assume no words will be longer than n?
    – lirtosiast
    1 hour ago










  • Format a list of words
    – user202729
    1 hour ago















up vote
2
down vote

favorite









up vote
2
down vote

favorite











(Note: This is my first ever code golf question, but as far as I can tell, nobody else has done exactly this, so I should be good.)



Your task is to make a program or function that takes in a string s and an integer n, and returns or outputs that text wrapped into multiple lines. Each word must be wholly on a line; i.e. no words split in the middle.



Example:



s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget erat lectus. Morbi mi mi, fringilla sed suscipit ullamcorper, tristique at mauris. Morbi non commodo nibh. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed at iaculis mauris. Praesent a sem augue. Nulla lectus sapien, auctor nec pharetra eu, tincidunt ac diam. Sed ligula arcu, aliquam quis velit aliquam, dictum varius erat." 
n = 50

output:
Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Sed eget erat lectus. Morbi mi mi, fringilla
sed suscipit ullamcorper, tristique at mauris.
Morbi non commodo nibh. Pellentesque habitant
morbi tristique senectus et netus et malesuada
fames ac turpis egestas. Sed at iaculis mauris.
Praesent a sem augue. Nulla lectus sapien, auctor
nec pharetra eu, tincidunt ac diam. Sed ligula
arcu, aliquam quis velit aliquam, dictum varius
erat.


Your output can be an array of strings or a single string with line breaks. Also, you can assume n will be above 10, so don't worry about dealing with weird cases.



Standard I/O rules apply, and standard loopholes are prohibited.



Since this is code-golf, the shortes solution in bytes wins.



Here is an example program in Python that would work.










share|improve this question













(Note: This is my first ever code golf question, but as far as I can tell, nobody else has done exactly this, so I should be good.)



Your task is to make a program or function that takes in a string s and an integer n, and returns or outputs that text wrapped into multiple lines. Each word must be wholly on a line; i.e. no words split in the middle.



Example:



s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget erat lectus. Morbi mi mi, fringilla sed suscipit ullamcorper, tristique at mauris. Morbi non commodo nibh. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed at iaculis mauris. Praesent a sem augue. Nulla lectus sapien, auctor nec pharetra eu, tincidunt ac diam. Sed ligula arcu, aliquam quis velit aliquam, dictum varius erat." 
n = 50

output:
Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Sed eget erat lectus. Morbi mi mi, fringilla
sed suscipit ullamcorper, tristique at mauris.
Morbi non commodo nibh. Pellentesque habitant
morbi tristique senectus et netus et malesuada
fames ac turpis egestas. Sed at iaculis mauris.
Praesent a sem augue. Nulla lectus sapien, auctor
nec pharetra eu, tincidunt ac diam. Sed ligula
arcu, aliquam quis velit aliquam, dictum varius
erat.


Your output can be an array of strings or a single string with line breaks. Also, you can assume n will be above 10, so don't worry about dealing with weird cases.



Standard I/O rules apply, and standard loopholes are prohibited.



Since this is code-golf, the shortes solution in bytes wins.



Here is an example program in Python that would work.







code-golf string






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 1 hour ago









ATMunn

415




415








  • 1




    Can we assume no words will be longer than n?
    – lirtosiast
    1 hour ago










  • Format a list of words
    – user202729
    1 hour ago
















  • 1




    Can we assume no words will be longer than n?
    – lirtosiast
    1 hour ago










  • Format a list of words
    – user202729
    1 hour ago










1




1




Can we assume no words will be longer than n?
– lirtosiast
1 hour ago




Can we assume no words will be longer than n?
– lirtosiast
1 hour ago












Format a list of words
– user202729
1 hour ago






Format a list of words
– user202729
1 hour ago












3 Answers
3






active

oldest

votes

















up vote
2
down vote













Vim, 15 bytes/keystrokes



DJ:se tw=<C-r>"
gq_


A text formatting question? I know just the tool for the job! And it even has my name in the first two keystrokes :D



<C-r> means ctrl-r.



This could ever so slightly shorter in V, but I prefer answering in vanilla vim for answers that really show off how concise vim can be for the right challenge. And the difference is so small anyway.



This could also be the following for 15 bytes as well:



:se tw=<C-r><C-w>
ddgq_


Try it online!






share|improve this answer






























    up vote
    1
    down vote













    JavaScript (ES6), 75 bytes



    Takes input as (string)(n).





    s=>n=>s.split` `.map(w=>(r+w)[n-1]?(o+=r+`
    `,r=w):r+=r?' '+w:w,o=r='')&&o+r


    Try it online!






    share|improve this answer




























      up vote
      0
      down vote













      JavaScript + HTML + CSS, 117 bytes






      n=50
      s="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget erat lectus. Morbi mi mi, fringilla sed suscipit ullamcorper, tristique at mauris. Morbi non commodo nibh. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed at iaculis mauris. Praesent a sem augue. Nulla lectus sapien, auctor nec pharetra eu, tincidunt ac diam. Sed ligula arcu, aliquam quis velit aliquam, dictum varius erat."
      f=(n,s)=>document.body.innerHTML+=`<p style='width:calc(10px*${n}*.60);font-family:monospace;font-size:10px;'>${s}</p>`
      f(n,s)








      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: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        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%2f176870%2fmake-a-simple-word-wrapper%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote













        Vim, 15 bytes/keystrokes



        DJ:se tw=<C-r>"
        gq_


        A text formatting question? I know just the tool for the job! And it even has my name in the first two keystrokes :D



        <C-r> means ctrl-r.



        This could ever so slightly shorter in V, but I prefer answering in vanilla vim for answers that really show off how concise vim can be for the right challenge. And the difference is so small anyway.



        This could also be the following for 15 bytes as well:



        :se tw=<C-r><C-w>
        ddgq_


        Try it online!






        share|improve this answer



























          up vote
          2
          down vote













          Vim, 15 bytes/keystrokes



          DJ:se tw=<C-r>"
          gq_


          A text formatting question? I know just the tool for the job! And it even has my name in the first two keystrokes :D



          <C-r> means ctrl-r.



          This could ever so slightly shorter in V, but I prefer answering in vanilla vim for answers that really show off how concise vim can be for the right challenge. And the difference is so small anyway.



          This could also be the following for 15 bytes as well:



          :se tw=<C-r><C-w>
          ddgq_


          Try it online!






          share|improve this answer

























            up vote
            2
            down vote










            up vote
            2
            down vote









            Vim, 15 bytes/keystrokes



            DJ:se tw=<C-r>"
            gq_


            A text formatting question? I know just the tool for the job! And it even has my name in the first two keystrokes :D



            <C-r> means ctrl-r.



            This could ever so slightly shorter in V, but I prefer answering in vanilla vim for answers that really show off how concise vim can be for the right challenge. And the difference is so small anyway.



            This could also be the following for 15 bytes as well:



            :se tw=<C-r><C-w>
            ddgq_


            Try it online!






            share|improve this answer














            Vim, 15 bytes/keystrokes



            DJ:se tw=<C-r>"
            gq_


            A text formatting question? I know just the tool for the job! And it even has my name in the first two keystrokes :D



            <C-r> means ctrl-r.



            This could ever so slightly shorter in V, but I prefer answering in vanilla vim for answers that really show off how concise vim can be for the right challenge. And the difference is so small anyway.



            This could also be the following for 15 bytes as well:



            :se tw=<C-r><C-w>
            ddgq_


            Try it online!







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 1 hour ago

























            answered 1 hour ago









            DJMcMayhem

            40.7k11145307




            40.7k11145307






















                up vote
                1
                down vote













                JavaScript (ES6), 75 bytes



                Takes input as (string)(n).





                s=>n=>s.split` `.map(w=>(r+w)[n-1]?(o+=r+`
                `,r=w):r+=r?' '+w:w,o=r='')&&o+r


                Try it online!






                share|improve this answer

























                  up vote
                  1
                  down vote













                  JavaScript (ES6), 75 bytes



                  Takes input as (string)(n).





                  s=>n=>s.split` `.map(w=>(r+w)[n-1]?(o+=r+`
                  `,r=w):r+=r?' '+w:w,o=r='')&&o+r


                  Try it online!






                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    JavaScript (ES6), 75 bytes



                    Takes input as (string)(n).





                    s=>n=>s.split` `.map(w=>(r+w)[n-1]?(o+=r+`
                    `,r=w):r+=r?' '+w:w,o=r='')&&o+r


                    Try it online!






                    share|improve this answer












                    JavaScript (ES6), 75 bytes



                    Takes input as (string)(n).





                    s=>n=>s.split` `.map(w=>(r+w)[n-1]?(o+=r+`
                    `,r=w):r+=r?' '+w:w,o=r='')&&o+r


                    Try it online!







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 1 hour ago









                    Arnauld

                    70.2k686295




                    70.2k686295






















                        up vote
                        0
                        down vote













                        JavaScript + HTML + CSS, 117 bytes






                        n=50
                        s="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget erat lectus. Morbi mi mi, fringilla sed suscipit ullamcorper, tristique at mauris. Morbi non commodo nibh. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed at iaculis mauris. Praesent a sem augue. Nulla lectus sapien, auctor nec pharetra eu, tincidunt ac diam. Sed ligula arcu, aliquam quis velit aliquam, dictum varius erat."
                        f=(n,s)=>document.body.innerHTML+=`<p style='width:calc(10px*${n}*.60);font-family:monospace;font-size:10px;'>${s}</p>`
                        f(n,s)








                        share|improve this answer

























                          up vote
                          0
                          down vote













                          JavaScript + HTML + CSS, 117 bytes






                          n=50
                          s="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget erat lectus. Morbi mi mi, fringilla sed suscipit ullamcorper, tristique at mauris. Morbi non commodo nibh. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed at iaculis mauris. Praesent a sem augue. Nulla lectus sapien, auctor nec pharetra eu, tincidunt ac diam. Sed ligula arcu, aliquam quis velit aliquam, dictum varius erat."
                          f=(n,s)=>document.body.innerHTML+=`<p style='width:calc(10px*${n}*.60);font-family:monospace;font-size:10px;'>${s}</p>`
                          f(n,s)








                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            JavaScript + HTML + CSS, 117 bytes






                            n=50
                            s="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget erat lectus. Morbi mi mi, fringilla sed suscipit ullamcorper, tristique at mauris. Morbi non commodo nibh. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed at iaculis mauris. Praesent a sem augue. Nulla lectus sapien, auctor nec pharetra eu, tincidunt ac diam. Sed ligula arcu, aliquam quis velit aliquam, dictum varius erat."
                            f=(n,s)=>document.body.innerHTML+=`<p style='width:calc(10px*${n}*.60);font-family:monospace;font-size:10px;'>${s}</p>`
                            f(n,s)








                            share|improve this answer












                            JavaScript + HTML + CSS, 117 bytes






                            n=50
                            s="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget erat lectus. Morbi mi mi, fringilla sed suscipit ullamcorper, tristique at mauris. Morbi non commodo nibh. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed at iaculis mauris. Praesent a sem augue. Nulla lectus sapien, auctor nec pharetra eu, tincidunt ac diam. Sed ligula arcu, aliquam quis velit aliquam, dictum varius erat."
                            f=(n,s)=>document.body.innerHTML+=`<p style='width:calc(10px*${n}*.60);font-family:monospace;font-size:10px;'>${s}</p>`
                            f(n,s)








                            n=50
                            s="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget erat lectus. Morbi mi mi, fringilla sed suscipit ullamcorper, tristique at mauris. Morbi non commodo nibh. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed at iaculis mauris. Praesent a sem augue. Nulla lectus sapien, auctor nec pharetra eu, tincidunt ac diam. Sed ligula arcu, aliquam quis velit aliquam, dictum varius erat."
                            f=(n,s)=>document.body.innerHTML+=`<p style='width:calc(10px*${n}*.60);font-family:monospace;font-size:10px;'>${s}</p>`
                            f(n,s)





                            n=50
                            s="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget erat lectus. Morbi mi mi, fringilla sed suscipit ullamcorper, tristique at mauris. Morbi non commodo nibh. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed at iaculis mauris. Praesent a sem augue. Nulla lectus sapien, auctor nec pharetra eu, tincidunt ac diam. Sed ligula arcu, aliquam quis velit aliquam, dictum varius erat."
                            f=(n,s)=>document.body.innerHTML+=`<p style='width:calc(10px*${n}*.60);font-family:monospace;font-size:10px;'>${s}</p>`
                            f(n,s)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 1 hour ago









                            guest271314

                            317211




                            317211






























                                draft saved

                                draft discarded




















































                                If this is an answer to a challenge…




                                • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


                                • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
                                  Explanations of your answer make it more interesting to read and are very much encouraged.


                                • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.



                                More generally…




                                • …Please make sure to answer the question and provide sufficient detail.


                                • …Avoid asking for help, clarification or responding to other answers (use comments instead).






                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f176870%2fmake-a-simple-word-wrapper%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                數位音樂下載

                                When can things happen in Etherscan, such as the picture below?

                                格利澤436b