15
1
The task is to display ascii table for a given array.
Input
The input is an 2D matrix. The matrix's row length is same with the length of an array. Alternatively, you can take input of an 2D matrix with the first row is a header. The outer dimension is the row.
Example Input:
[["License","2008-05-08","2009-03-11","2011-11-22","2013-08-12","2015-11-19"],["GPLv2","58.69%","52.2%","42.5%","33%","23%"],["GPLv3","1.64%","4.15%","6.5%","12%","9%"],["LGPL 2.1","11.39%","9.84%","?","6%","5%"],["LGPL 3.0","? (<0.64%)","0.37%","?","3%","2%"],["GPL family together","71.72% (+ <0.64%)","66.56%","?","54%","39%"]]
Output
The output of a table looks like below.
+---------------------+-------------------+------------+------------+------------+------------+
| License | 2008-05-08 | 2009-03-11 | 2011-11-22 | 2013-08-12 | 2015-11-19 |
+---------------------+-------------------+------------+------------+------------+------------+
| GPLv2 | 58.69% | 52.2% | 42.5% | 33% | 23% |
| GPLv3 | 1.64% | 4.15% | 6.5% | 12% | 9% |
| LGPL 2.1 | 11.39% | 9.84% | ? | 6% | 5% |
| LGPL 3.0 | ? (<0.64%) | 0.37% | ? | 3% | 2% |
| GPL family together | 71.72% (+ <0.64%) | 66.56% | ? | 54% | 39% |
+---------------------+-------------------+------------+------------+------------+------------+
Each cell has exactly one space on the left and at least one space on the right, padded until the bars aligned. However, at least one cell has only one space on its right.
Test cases
Input:
[["Hello", "World", "!!!"],["aa", "bbbbbbbbb", "CcC"], ["Pyth", "CJam", "GolfScript"]]
Output:
+-------+-----------+------------+
| Hello | World | !!! |
+-------+-----------+------------+
| aa | bbbbbbbbb | CcC |
| Pyth | CJam | GolfScript |
+-------+-----------+------------+
Example submission
function ascii_table(array, header) {
var lengths = array[0].map(function(_, i) {
var col = array.map(function(row) {
if (row[i] != undefined) {
return row[i].length;
} else {
return 0;
}
});
return Math.max.apply(Math, col);
});
array = array.map(function(row) {
return '| ' + row.map(function(item, i) {
var size = item.length;
if (size < lengths[i]) {
item += new Array(lengths[i]-size+1).join(' ');
}
return item;
}).join(' | ') + ' |';
});
var sep = '+' + lengths.map(function(length) {
return new Array(length+3).join('-');
}).join('+') + '+';
if (header) {
return sep + '\n' + array[0] + '\n' + sep + '\n' +
array.slice(1).join('\n') + '\n' + sep;
} else {
return sep + '\n' + array.join('\n') + '\n' + sep;
}
}
This is code-golf, so the submission with the least amount of bytes wins!
Looks like first answerer. Please, do not use example-of-solution. – Akangka – 2016-01-17T09:21:10.040
@ChristianIrwan deleted. – jcubic – 2016-01-17T09:22:20.823
Don't delete, I will try to figure out the challenge. – Akangka – 2016-01-17T09:23:23.263
You have enough reputation to be on chat. Do you think we should talk about improving this on Chat? (It seems like it will become extended discussion.) – wizzwizz4 – 2016-01-17T09:25:41.997
@wizzwizz4 what about this room? http://chat.stackoverflow.com/rooms/100629/coded-conversations
– jcubic – 2016-01-17T09:29:14.370@jcubic I mean creating a temporary room, which will be deleted after we've figured this out. – wizzwizz4 – 2016-01-17T09:30:33.727
@wizzwizz4 you can create such room. – jcubic – 2016-01-17T09:31:39.253
@jcubic I mean you must specify the rules. – Akangka – 2016-01-17T09:33:21.803
@ChristianIrwan, jcubic http://chat.stackexchange.com/rooms/34428/discussion-about-display-2d-array-as-ascii-table
– wizzwizz4 – 2016-01-17T09:34:16.933@ChristianIrwan the only rules are that the output need to look as in question, and first row may be a header. – jcubic – 2016-01-17T09:36:24.513
@Adnan I've updated the question. – jcubic – 2016-01-17T09:42:29.710
@jcubic I think you should add an input specification as well as [tag:code-golf] – wizzwizz4 – 2016-01-17T09:46:34.777
I don't think it's a good idea to keep making little edits. After a certain number of edits, the question will automatically become Community Wiki. – wizzwizz4 – 2016-01-17T09:50:06.273
@wizzwizz4 didn't know that. – jcubic – 2016-01-17T09:54:32.400
@wizzwizz4 I don't think so – edc65 – 2016-01-17T11:30:46.283
However, at least one cell in each row has only one space on its right. – edc65 – 2016-01-17T11:31:17.970
2If we want to take input as a single string instead of an array, does it have to be in the exact same format as the example inputs? If so, will there ever be any escaped characters (or will there ever be quotes in the input)? Can we also assume that there are no brackets or commas in the input other than the array delimiters? What about vertical bars? In general, what are the valid characters that can make up the strings in the table? – Doorknob – 2016-01-17T16:07:01.580
Both test cases being squares (6x6 and 3x3), they not really help spotting out dimension related errors. – manatwork – 2016-01-22T12:27:41.683