26
3
Given a width and a block of
text containing possible hyphen-
ation points, format it fully-
justified (in monospace).
Fully justified means it is aligned on the left and the right, and is achieved by increasing the spacing between words until each line fits.
Related:
- Justify a text by adding spaces
- Align the text to a block
- And in a way this can be considered the next step in Text Processing #1: Hyphenation (which seems to have never been posted).
Input
You can take input in any format you like. You will be given:
- A target width (in characters), in the range 5-100 (inclusive);
- A block of text containing possibly hyphenated words. This could be a space-separated string, an array of words, or an array of arrays of word fragments (or any other data representation you desire).
A typical input might be:
Width: 25
Text: There's no bu-si-ne-ss lik-e s-h-o-w busine-ss, n-o bus-iness I know.
Where the hyphens denote possible hyphenation points, and the spaces denote word boundaries. A possible alternative representation of the text:
[["There's"], ["no"], ["bu", "si", "ne", "ss"], ["lik", "e"], (etc.)]
Output
The input text with spaces added between words, newlines at the column width, and hyphenation points chosen to fully-justify it to the column width. For functions, an array of strings (one for each line) can be returned instead of using newline separation.
A possible output for the above input might be:
There's no business like
show business, no bus-
iness I know.
Note that all hyphens have been removed except the one in the final "bus-iness", which is kept to show that the word wraps to the next line, and was chosen to ensure the second line contains as much text as possible.
Rules
Within each line, the number of spaces between words cannot vary by more than 1, but where you insert the extra spaces is otherwise up to you:
hello hi foo bar <-- not permitted (1,1,5) hello hi foo bar <-- not permitted (2,1,4) hello hi foo bar <-- OK (2,2,3) hello hi foo bar <-- OK (2,3,2) hello hi foo bar <-- OK (3,2,2)
No line can begin or end with spaces (except the last line, which can end with spaces).
The last line should be left justified, containing single spaces between each word. It can be followed by arbitrary whitespace / a newline if desired, but this is not required.
Words will consist of A-Z, a-z, 0-9 and simple punctuation (
.,'()&
)You can assume that no word fragment will be longer than the target width, and it will always be possible to fill lines according to the rules (i.e. there will be at least 2 word fragments on each line, or 1 word fragment which fills the line perfectly)
You must choose hyphenation points which maximise the number of word characters on earlier lines (i.e. words must be consumed greedily by lines), for example:
This is an input stri-ng with hyph-en-at-ion poi-nts. This is an input stri- <-- not permitted ng with hyphenation points. This is an input string with hyph- <-- not permitted enation points. This is an input string with hyphen- <-- OK ation points.
Shortest code in bytes wins
Examples
Width: 20
Text: The q-uick brown fox ju-mp-s ove-r t-h-e lazy dog.
The quick brown fox
jumps over the lazy
dog.
Width: 32
Text: Given a width and a block of text cont-ain-ing pos-sible hyphen-ation points, for-mat it ful-ly-just-ified (in mono-space).
Given a width and a block of
text containing possible hyphen-
ation points, format it fully-
justified (in monospace).
Width: 80
Text: Pro-gram-ming Puz-zles & Code Golf is a ques-tion and ans-wer site for pro-gram-ming puz-zle enth-usi-asts and code golf-ers. It's built and run by you as part of the St-ack Exch-ange net-work of Q&A sites. With your help, we're work-ing to-g-et-her to build a lib-rary of pro-gram-ming puz-zles and their sol-ut-ions.
Programming Puzzles & Code Golf is a question and answer site for programming
puzzle enthusiasts and code golfers. It's built and run by you as part of the
Stack Exchange network of Q&A sites. With your help, we're working together to
build a library of programming puzzles and their solutions.
Width: 20
Text: Pro-gram-ming Puz-zles & Code Golf is a ques-tion and ans-wer site for pro-gram-ming puz-zle enth-usi-asts and code golf-ers. It's built and run by you as part of the St-ack Exch-ange net-work of Q&A sites. With your help, we're work-ing to-g-et-her to build a lib-rary of pro-gram-ming puz-zles and their sol-ut-ions.
Programming Puzzles
& Code Golf is a
question and answer
site for programming
puzzle enthusiasts
and code golfers.
It's built and run
by you as part of
the Stack Exchange
network of Q&A
sites. With your
help, we're working
together to build a
library of program-
ming puzzles and
their solutions.
Width: 5
Text: a b c d e f g h i j k l mm nn oo p-p qq rr ss t u vv ww x yy z
a b c
d e f
g h i
j k l
mm nn
oo pp
qq rr
ss t
u vv
ww x
yy z
Width: 10
Text: It's the bl-ack be-ast of Araghhhhh-hhh-h-hhh-h-h-h-hh!
It's the
black be-
ast of
Araghhhhh-
hhhhhhhhh-
hhh!
Yesss, finally another (text-based) [tag:typography] challenge :-) – ETHproductions – 2017-06-06T22:45:45.733
May we use built-ins and libraries? – Adám – 2017-06-06T22:59:25.543
1@Adám yes to builtins: there's no code restrictions, and shortest code wins. Though of course, it might make for a boring answer! As for libraries, you can as long as the library is freely available and you mark your answer as "language + library". Also the library version has to pre-date this challenge. – Dave – 2017-06-06T23:04:20.843
Mathematica: Hyphenation -> True, TextJustification -> 1, Done! ;-) – J42161217 – 2017-06-06T23:45:18.317
1In the event that a line can end with either a hyphen or a single character, e.g.
anybod-y
with width 7, may we choose to output eitheranybody
oranybod-\ny
? – darrylyeo – 2017-06-07T02:32:32.9771@JonathanAllan yes; sorry, I'll fix that – Dave – 2017-06-07T06:53:38.097
3@darrylyeo no you have to output the full word in that case, since it must greedily have as many word characters as possible on each line. – Dave – 2017-06-07T06:56:42.403