Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
72
Task/Pascals-triangle/JavaScript/pascals-triangle-2.js
Normal file
72
Task/Pascals-triangle/JavaScript/pascals-triangle-2.js
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
(function (n) {
|
||||
|
||||
// A Pascal triangle of n rows
|
||||
// n --> [[n]]
|
||||
function pascalTriangle(n) {
|
||||
|
||||
// Sums of each consecutive pair of numbers
|
||||
// [n] --> [n]
|
||||
function pairSums(lst) {
|
||||
return lst.reduce(function (acc, n, i, l) {
|
||||
var iPrev = i ? i - 1 : 0;
|
||||
return i ? acc.concat(l[iPrev] + l[i]) : acc
|
||||
}, []);
|
||||
}
|
||||
|
||||
// Next line in a Pascal triangle series
|
||||
// [n] --> [n]
|
||||
function nextPascal(lst) {
|
||||
return lst.length ? [1].concat(
|
||||
pairSums(lst)
|
||||
).concat(1) : [1];
|
||||
}
|
||||
|
||||
// Each row is a function of the preceding row
|
||||
return n ? Array.apply(null, Array(n - 1)).reduce(
|
||||
function (a, _, i) {
|
||||
return a.concat([nextPascal(a[i])]);
|
||||
}, [[1]]) : [];
|
||||
}
|
||||
|
||||
// TEST
|
||||
var lstTriangle = pascalTriangle(n);
|
||||
|
||||
|
||||
// FORMAT OUTPUT AS WIKI TABLE
|
||||
|
||||
// [[a]] -> bool -> s -> s
|
||||
function wikiTable(lstRows, blnHeaderRow, strStyle) {
|
||||
return '{| class="wikitable" ' + (
|
||||
strStyle ? 'style="' + strStyle + '"' : ''
|
||||
) + lstRows.map(function (lstRow, iRow) {
|
||||
var strDelim = ((blnHeaderRow && !iRow) ? '!' : '|');
|
||||
|
||||
return '\n|-\n' + strDelim + ' ' + lstRow.map(function (v) {
|
||||
return typeof v === 'undefined' ? ' ' : v;
|
||||
}).join(' ' + strDelim + strDelim + ' ');
|
||||
}).join('') + '\n|}';
|
||||
}
|
||||
|
||||
var lstLastLine = lstTriangle.slice(-1)[0],
|
||||
lngBase = (lstLastLine.length * 2) - 1,
|
||||
nWidth = lstLastLine.reduce(function (a, x) {
|
||||
var d = x.toString().length;
|
||||
return d > a ? d : a;
|
||||
}, 1) * lngBase;
|
||||
|
||||
return [
|
||||
wikiTable(
|
||||
lstTriangle.map(function (lst) {
|
||||
return lst.join(';;').split(';');
|
||||
}).map(function (line, i) {
|
||||
var lstPad = Array((lngBase - line.length) / 2);
|
||||
return lstPad.concat(line).concat(lstPad);
|
||||
}),
|
||||
false,
|
||||
'text-align:center;width:' + nWidth + 'em;height:' + nWidth +
|
||||
'em;table-layout:fixed;'
|
||||
),
|
||||
|
||||
JSON.stringify(lstTriangle)
|
||||
].join('\n\n');
|
||||
})(7);
|
||||
1
Task/Pascals-triangle/JavaScript/pascals-triangle-3.js
Normal file
1
Task/Pascals-triangle/JavaScript/pascals-triangle-3.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1],[1,5,10,10,5,1],[1,6,15,20,15,6,1]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue