任务是显示给定数组的ascii表。
输入值
输入是2D矩阵。矩阵的行长与数组的长度相同。另外,您也可以输入2D矩阵,第一行是标题。外部尺寸是该行。
输入示例:
[["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%"]]
输出量
表的输出如下所示。
+---------------------+-------------------+------------+------------+------------+------------+
| 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% |
+---------------------+-------------------+------------+------------+------------+------------+
每个单元格的左边正好有一个空格,右边至少有一个空格,填充直到条形对齐为止。但是,至少一个单元格的右侧只有一个空间。
测试用例
Input:
[["Hello", "World", "!!!"],["aa", "bbbbbbbbb", "CcC"], ["Pyth", "CJam", "GolfScript"]]
Output:
+-------+-----------+------------+
| Hello | World | !!! |
+-------+-----------+------------+
| aa | bbbbbbbbb | CcC |
| Pyth | CJam | GolfScript |
+-------+-----------+------------+
提交示例
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;
}
}
这是代码高尔夫球,因此,字节数最少的提交将获胜!
看起来像第一个答复者。请不要使用解决方案示例。
—
Akangka '16
@ChristianIrwan已删除。
—
jcubic '16
不要删除,我将尝试找出挑战。
—
Akangka '16
您有足够的声誉可以聊天。您认为我们应该谈谈在Chat上改善这一点吗?(似乎它将成为扩展讨论。)
—
wizzwizz4 2016年
如果我们想将输入作为单个字符串而不是数组,是否必须与示例输入的格式完全相同?如果是这样,会不会有任何转义的字符(或者输入中是否会有引号)?我们是否还可以假设输入中除了数组定界符之外没有方括号或逗号?那竖线呢?通常,可以组成表中字符串的有效字符是什么?
—
门把手