Problem Statement
Our list will be printed out row by row, with spaces inserted to position the names within each row properly. Each row must contain exactly pageWidth characters (including spaces). In order to avoid run-together, we require that only spaces can appear in the print position just prior to the start of a column. This applies even to the last row (where the final columns may contain no names).
Create a class Lister that contains a method doList that is given pageWidth and a String[] names and that returns the optimal listing as a String[] giving the rows to be printed (in order from top to bottom). Each element in the return should contain exactly pageWidth characters. Use the space character ' ' in your listing to position the names within each row (and to pad each row to pageWidth characters).
A listing is "optimal" if it has as few rows as possible. Among listings with the minimal number of rows, choose the one whose rightmost non-space character is as far to the left as possible. If multiple listings are still equally "optimal" choose the one with the fewest columns.
Definition
- Class:
- Lister
- Method:
- doList
- Parameters:
- int, String[]
- Returns:
- String[]
- Method signature:
- String[] doList(int pageWidth, String[] names)
- (be sure your method is public)
Constraints
- pageWidth will be between 10 and 80 inclusive.
- names will contain between 1 and 50 elements inclusive.
- Each element of names will contain between 1 and pageWidth characters inclusive.
- Each element of names will contain only lowercase letters 'a'-'z'.
Examples
10
{"bob","abernathy","x"}Returns: { "abernathy ", "bob ", "x " }Any column containing "abernathy" cannot be adjacent to another column since even a one-character name would be too much to avoid run-together and fit on the page (spaces shown as '.'): abernathy. bob....... x.........11
{"bob","abernathy","x"}Returns: { "abernathy x", "bob " }There is just enough page width to get abernathy and x on the same row and keep them from running together, producing the following listing (spaces shown as '.'): abernathy.x bob........10
{"bob","a","x"}Returns: { "a bob x " }10
{"x","teddy","andy"}Returns: { "andy x ", "teddy " }Note that there are 2 spaces between "andy" and "x" in the listing. It would be illegal to have only 1 space, since then the 'y' in "teddy" would occupy the print position just prior to the second column of names (spaces shown as '.'): andy..x... teddy.....33
{"g","babec","bafffebe","bffaf", "decdgfgcbc","bacfeddea","g","deecgabcg","c"}Returns: { "babec bffaf decdgfgcbc g ", "bacfeddea c deecgabcg g ", "bafffebe " }