Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
67
Task/Spiral-matrix/JavaScript/spiral-matrix-2.js
Normal file
67
Task/Spiral-matrix/JavaScript/spiral-matrix-2.js
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
(function (n) {
|
||||
|
||||
// Spiral: the first row plus a smaller spiral rotated 90 degrees clockwise
|
||||
function spiral(lngRows, lngCols, nStart) {
|
||||
return lngRows ? [range(nStart, (nStart + lngCols) - 1)].concat(
|
||||
transpose(
|
||||
spiral(lngCols, lngRows - 1, nStart + lngCols)
|
||||
).map(reverse)
|
||||
) : [
|
||||
[]
|
||||
];
|
||||
}
|
||||
|
||||
// rows and columns transposed (for 90 degree rotation)
|
||||
function transpose(lst) {
|
||||
return lst.length > 1 ? lst[0].map(function (_, col) {
|
||||
return lst.map(function (row) {
|
||||
return row[col];
|
||||
});
|
||||
}) : lst;
|
||||
}
|
||||
|
||||
// elements in reverse order (for 90 degree rotation)
|
||||
function reverse(lst) {
|
||||
return lst.length > 1 ? lst.reduceRight(function (acc, x) {
|
||||
return acc.concat(x);
|
||||
}, []) : lst;
|
||||
}
|
||||
|
||||
// [m..n]
|
||||
function range(m, n) {
|
||||
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
|
||||
return m + i;
|
||||
});
|
||||
}
|
||||
|
||||
// TESTING
|
||||
|
||||
var lstSpiral = spiral(n, n, 0);
|
||||
|
||||
|
||||
// OUTPUT FORMATTING - JSON and wikiTable
|
||||
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|}';
|
||||
}
|
||||
|
||||
return [
|
||||
wikiTable(
|
||||
|
||||
lstSpiral,
|
||||
|
||||
false,
|
||||
'text-align:center;width:12em;height:12em;table-layout:fixed;'
|
||||
),
|
||||
|
||||
JSON.stringify(lstSpiral)
|
||||
].join('\n\n');
|
||||
|
||||
})(5);
|
||||
1
Task/Spiral-matrix/JavaScript/spiral-matrix-3.js
Normal file
1
Task/Spiral-matrix/JavaScript/spiral-matrix-3.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
[[0,1,2,3,4],[15,16,17,18,5],[14,23,24,19,6],[13,22,21,20,7],[12,11,10,9,8]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue