Pascal's triangle: Difference between revisions

Line 1,964:
<lang JavaScript>(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]]) : [];
}
 
// Each row is a function of the preceding row
// TEST
return n ? Array.apply(null, Array(n - 1)).reduce(function (a, _, i) {
var lstTriangle = pascalTriangle(n);
function (a, _, i) {
return a.concat([nextPascal(a[i])]);
}, [[1]]) : [];
}
 
// TEST
var lstTriangle = pascalTriangle(n);
 
// FORMAT OUTPUT AS WIKI TABLE
 
// [[a]] -> boolFORMAT ->OUTPUT sAS ->WIKI sTABLE
function wikiTable(lstRows, blnHeaderRow, strStyle) {
return '{| class="wikitable" ' + (
strStyle ? 'style="' + strStyle + '"' : ''
) + lstRows.map(function (lstRow, iRow) {
var strDelim = ((blnHeaderRow && !iRow) ? '!' : '|');
 
// [[a]] -> bool -> s -> s
return '\n|-\n' + strDelim + ' ' + lstRow.map(function (v) {
function wikiTable(lstRows, blnHeaderRow, strStyle) {
return typeof v === 'undefined' ? ' ' : v;
return '{| class="wikitable" ' + (
}).join(' ' + strDelim + strDelim + ' ');
strStyle ? }).join('style="') + strStyle + '"' : '\n|}';
) + lstRows.map(function (lstRow, iRow) {
}
var strDelim = ((blnHeaderRow && !iRow) ? '!' : '|');
 
return '\n|-\n' + strDelim + ' ' + lstRow.map(function (v) {
var lstLastLine = lstTriangle.slice(-1)[0],
lngBasereturn typeof v === 'undefined' (lstLastLine.length? *' 2)' -: 1,v;
}).join(' ' + strDelim + strDelim + ' ');
nWidth = lstLastLine.reduce(function (a, x) {
}).join('') + '\n|}';
var d = x.toString().length;
}
return d > a ? d : a;
}, 1) * lngBase;
 
var lstTablelstLastLine = lstTriangle.mapslice(function (lst-1) {[0],
lngBase = (lstLastLine.length * 2) - 1,
return lst.join(';;').split(';');
nWidth = })lstLastLine.mapreduce(function (linea, ix) {
var lstPadd = Arrayx.toString((lngBase - line).length) / 2);
return d > a ? d return: lstPad.concat(line).concat(lstPad)a;
}, 1) * })lngBase;
 
return [
wikiTable(
lstTriangle.map(function (lst) {
lstTable,
return false,lst.join(';;').split(';');
nWidth = lstLastLine}).reducemap(function (aline, xi) {
'text-align:center;width:' + nWidth + 'em;height:' + nWidth +
var lstPad = Array((lngBase 'em;table-layout:fixed line.length) / 2);'
return lstlstPad.joinconcat(';;'line).splitconcat(';'lstPad);
),
}),
)false,
'text-align:center;width:' + nWidth + 'em;height:' + nWidth +
'em;table-layout:fixed;'
),
 
JSON.stringify(lstTriangle)
].join('\n\n');
})(7);</lang>
 
9,659

edits