The Social Geek

A never ending story of a never stopping idiot!

Text Justification Programming Problem

April22

Just for fun, I jumped in to solve a few programming problems that you get when you participate in one of those programming contests. Below is one such problem that I took out from the MIT Programming Contest Practice Set and came up with a C# based solution. The solution files are provided for download below. I was too lazy to create too many test cases of my own and I’ve only checked the solution on the given test cases plus a few of my own. But I feel it should work fine on others. Programming gurus are welcome to suggest improvements, after all, I am of the opinion to drop the programming best practices when solving such problems in quick time. Bottom line: Kaam hogaya poora, bhaar mei jaye Noora.. where Noora is best practices ;)


The Problem

A common task performed by word processors and desktop publishing programs is justifying a paragraph of text, breaking lines at appropriate places given a fixed column width. In this problem, you will write a program to perform simple text justification.

Input to your program will consist of pairs of lines. The first line in each pair is an integer between 0 and 100. If this number is 0, the program has reached the end of input and should terminate. Otherwise, the number indicates the maximum number of characters on each justified line output by your program. The second line is a paragraph to be formatted; this line contains only printable characters (i.e., ASCII 32 through 126), and is guaranteed to be
no longer than 2000 characters. In addition, this line is guaranteed to contain at least one character that is not a space. Your program should then format the paragraph according to the rules described below, then write out the resulting formatted lines, followed by a blank line, to standard output.

Each paragraph should be formatted as follows:

  • Each formatted line should not contain any spaces at the beginning or at the end, and should be no longer than the maximum width specified as the first line in the pair of lines in the input.
  • Line breaks should occur only at spaces in the original input paragraph. If a “word” will fit on a previous line, it should not be placed in the following line. In other words, each line should be filled up before the next line is started. Here, a “word” is defined as a maximal contiguous sequence of characters that are not space. The length of each word in the input is guaranteed to not exceed the given maximum width.
  • All spaces at the beginning of or at the end of the paragraph should be ignored. The number of spaces between each pair of adjacent words in the input paragraph should be preserved in the output, except at line breaks.

Again, please be reminded that exactly one blank line must be output after each formatted paragraph to signify that the program is done with the paragraph.

Sample Input

10
Please make check payable to Harvard Univ.
20
Take the T to Kendall.  Walk across the street.
0

Sample Output

Please
make check
payable to
Harvard
Univ.

Take the T to
Kendall.  Walk
across the street.

Note that there is one blank line at the end of the sample output above.

Downloads

TextJustificationProblem.zip (Contains the problem statement, the source file and the sample input file)

Email will not be published

Website example

Your Comment: