8
Write a program or function that given some text, a number of columns, and the column width, formats the text into columns. This is plain text typography.
Rules
Input
- The text will be a string of printable ASCII and may contain newlines and spaces (but not tabs).
- The number of columns will be a positive integer.
- The column width is an integer greater or equal to 2 that specifies how many characters per column.
For this challenge, a word will consist of any non-whitespace characters. The input consists of one string and two integers, which may be taken in any order.
Output
Output is the text formatted into balanced columns with as many words on each line as will fit.
- If a word is too long to fit in a column, put the word on the next line if there is another word on the current line and the line could be padded to the column width with three spaces or less.
- Otherwise, hyphenate the word so that it fills the rest of the line. Hyphenate wherever the line ends; don't worry about hyphenating between syllables.
- Columns should be separated by a margin of four space characters.
- The columns should be balanced so that they all have an equal number of lines if possible.
- The leftmost columns should have an extra line if necessary.
- Any line breaks and multiple spaces should be preserved. Trailing spaces are optional after the last column.
Examples
Text:
The number of columns will be a positive integer. Columns should be separated by a margin of four space characters.
The columns should be balanced. The column width is greater than two.
Columns: 3, width: 10
The number separated ns should
of columns by a marg- be balanc-
will be a in of four ed. The
positive space cha- column wi-
integer. racters. dth is gr-
Columns eater than
should be The colum- two.
Text:
This line is hyphenated.
This line, on the other hand, is not.
Columns: 1, width: 20
This line is hyphen-
ated.
This line, on the
other hand, is not.
Text: Tiny columns.
columns: 4, width: 2
T- c- u- s.
i- o- m-
ny l- n-
Text: Two spaces.<space>
columns: 5, width: 2
T- p- c- s.
wo s- a- e-
Text: <newline>A phrase
columns: 2, width: 5
rase
A ph-
Text: A short sentence.
columns: 10, width: 5
A sh- ort sent- ence.
Text: It's "no word" 1234567890 -+-+-+-+ (*&!)
columns: 3, width: 6
It's 12345- +-+
"no 67890 (*&!)
word" -+-+--
This is code-golf; standard rules apply.
Possible dupe – Mego – 2015-11-17T04:07:17.607
4@Mego The challenges are related, however, this one requires hyphenating certain words and balancing columns, so I think it is different enough. – intrepidcoder – 2015-11-17T04:24:27.280
Is
s:'tiny', c:4, w:2
=t- i- n- y
ort- i- ny
?? – TFeld – 2015-11-20T20:00:39.847@TFeld My examples were wrong, it should be
t- i- ny
. Is everything right now, or do I need to fix it again? – intrepidcoder – 2015-11-20T21:02:55.667Not sure, should
tiny s
bet- i- n- y_ s
ort- i- ny s
– TFeld – 2015-11-20T21:05:40.667@TFeld It should be
t- i- ny s
I'm trying to do it by hand, so I really apologize for changing the examples so much. – intrepidcoder – 2015-11-20T21:25:30.177