samuelklam / print-neatly

DP Algorithm to print a paragraph neatly on a page

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Word Wrap (Print Pretty)

The following program prints a paragraph neatly on a page. The paragraph consists of words of length l1, l2, ..., ln. The maximum line length is M (assume li <= M always). We define a measure of neatness as follows. The extra space on a line (using one space between words) containing words li through lj is M - j + i - S, such that S = the sum from k=i to j, lk. The penalty is the sum over all lines except the last of the cube of the extra space at the end of the line. This has proven to be an effective heuristic for neatness in practice.

print-neatly.py contains a dynamic programming solution to determine the neatest way to print a paragraph.

The print_neatly class method implements an O(nM) time and O(n) space solution.

The print_neatly_non_optimized class method implements a O(n2) time and space solution.

About

DP Algorithm to print a paragraph neatly on a page


Languages

Language:Python 100.0%