### Text Formatting: dividing the problem in order to conquer it

• A common sense approach to problem solving

• I always ask students the following when they try to design an algorithm:

 What are the precise steps that they would take to solve the problem if they had to do it by hand (with pen and paper)

• Example:

• You are given the following data representation:

 ``` String[] words = new String[10000]; int numWords = 35 word[0] = "WE" word[1] = "hold" word[2] = "these" word[3] = "Truths" word[4] = "to" word[5] = "be" word[6] = "self-evident," word[7] = "that" word[8] = "all" word[9] = "Men" word[10] = "are" word[11] = "created" word[12] = "equal," word[13] = "that" word[14] = "they" .... (and so on) ```

• You must produce the following result:

 ``` WE hold these Truths to be certain unalienable Rights, self-evident, that all Men are that among these are Life, created equal, that they are Liberty and the Pursuit of endowed by their Creator with Happiness. ```

• Steps to transform input (array of words) into 2 column text

• Step 1: fit as many words as you can on one line (of at most 30 characters)

Intermediate result:

 ``` 012345678901234567890123456789 (ruler) ------------------------------ WE hold these Truths to be self-evident, that all Men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the Pursuit of Happiness. ```

(Think: how do we store the result to make this easy for the next step ?)

• Step 2: right adjust each line

Intermediate result:

 ``` 012345678901234567890123456789 Before:>WE hold these Truths to be After: >WE hold these Truths to be 012345678901234567890123456789 Before:>self-evident, that all Men are After: >self-evident, that all Men are 012345678901234567890123456789 Before:>created equal, that they are After: >created equal, that they are 012345678901234567890123456789 Before:>endowed by their Creator with After: >endowed by their Creator with 012345678901234567890123456789 Before:>certain unalienable Rights, After: >certain unalienable Rights, 012345678901234567890123456789 Before:>that among these are Life, After: >that among these are Life, 012345678901234567890123456789 Before:>Liberty and the Pursuit of After: >Liberty and the Pursuit of 012345678901234567890123456789 Before:>Happiness. After: >Happiness. ```

(Think: how do we store the result to make this easy for the next step ?)

• Step 3: print the right-adjusted line in 2 columns

 ``` WE hold these Truths to be self-evident, that all Men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the Pursuit of Happiness. Print as: WE hold these Truths to be certain unalienable Rights, self-evident, that all Men are that among these are Life, created equal, that they are Liberty and the Pursuit of endowed by their Creator with Happiness. ```

(We are done !)

• We will now solve the 3 simpler sub-problems while keeping in mind the question:

 How should the solution of a sub-problem be stored so that the next step in the solution can be done as easy as possible.