I\'ve been trying to create a show instance in order to visualize a given matrix and also, to create an outline with columns around and in between the matrix. What I managed to
If you make the assumptions that all rows of a matrix have the same number of columns (or at least that the first row of any matrix has at least as many columns as any other), and that three dashes suffice as the border for any cell, you can calculate the number of dashes to use between rows by taking the length
of the leading row.
If these assumptions do not hold, you need to make one pass over the array to calculate the maximum width of any row, then a second pass to draw the matrix. If matrices can contain numbers outside the range [0..9]
, you would also need to calculate the width of each column.
The algorithm might be:
String
, generating a [[String]]
.[[String]]
with spaces to its column width.[String]
whose elements are the graphical representation of each row.[String]
(As all columns are now padded to their maximum width, all rows should now have the same length.) and generate that number of box-drawing characters, such as ASCII "---"
or Unicode "┌─┬─┐"
.String
separated by newlines.As Rein Henrichs mentioned in a comment, you might consider naming this function something other than show
.