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.
code-golf string
add a comment |
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.
code-golf string
1
Can we assume no words will be longer thann
?
– lirtosiast
1 hour ago
Format a list of words
– user202729
1 hour ago
add a comment |
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.
code-golf string
(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
code-golf string
asked 1 hour ago
ATMunn
415
415
1
Can we assume no words will be longer thann
?
– lirtosiast
1 hour ago
Format a list of words
– user202729
1 hour ago
add a comment |
1
Can we assume no words will be longer thann
?
– 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
add a comment |
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!
add a comment |
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!
add a comment |
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)
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
edited 1 hour ago
answered 1 hour ago
DJMcMayhem♦
40.7k11145307
40.7k11145307
add a comment |
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
answered 1 hour ago
Arnauld
70.2k686295
70.2k686295
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered 1 hour ago
guest271314
317211
317211
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Can we assume no words will be longer than
n
?– lirtosiast
1 hour ago
Format a list of words
– user202729
1 hour ago