Skip to content

DP Algorithm to print a paragraph neatly on a page

Notifications You must be signed in to change notification settings

samuelklam/print-neatly

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages