A shop prints its price lists as boxed text tables. Given rows (a list of rows, each a list of
strings), return the table as a list of lines. The first row is the header; the others are body
rows. Surrounding spaces of every cell are removed first, and a row shorter than the header is
treated as if its missing cells were empty.
Number columns. A number is an optional -, one or more digits, and optionally a .
followed by one or more digits (7, -12, 3.50). A column is a number column when it has at
least one non-empty body cell and every non-empty body cell in it is a number. Its body cells
are written so the decimal points line up: let F be the longest fraction (digits after the .)
in the column. If F > 0, a number with a fraction gets spaces on its right until its fraction
has F characters, and a number without one gets F + 1 spaces on its right. Then every body
cell and the header of a number column are right-aligned. Every other column is
left-aligned.
Layout. A column's width is the length of its longest written cell (header included).
A border line is + followed by, for each column, width + 2 dashes and a +. A row line is
| followed by, for each column, a space, the cell padded to width, a space and |. The
table is: border, header, border, the body rows, border. With no body rows it is just the first
three lines. If rows is empty, return [].
Examples
Input: rows = [["item", "price", "qty"], ["tea", "3.5", "12"],
["biscuits", " 12.25 ", ""], ["jam", "4", "-1"]]
Output: ["+----------+-------+-----+",
"| item | price | qty |",
"+----------+-------+-----+",
"| tea | 3.5 | 12 |",
"| biscuits | 12.25 | |",
"| jam | 4 | -1 |",
"+----------+-------+-----+"]
Input: rows = [["n"], ["1.0"], ["x"]]
Output: ["+-----+", "| n |", "+-----+", "| 1.0 |", "| x |", "+-----+"]
Constraints
0 <= len(rows) <= 200; the header has 1 to 10 cells; no row is longer than the header- Cells hold printable ASCII characters other than
|, and are at most 30 characters long
Goals
- Decide a column's type by checking every cell against a small grammar
- Line up decimal points by padding the fraction side first
- Build every border and row from the computed column widths