2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
67
Task/Spiral-matrix/JavaScript/spiral-matrix-4.js
Normal file
67
Task/Spiral-matrix/JavaScript/spiral-matrix-4.js
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
(n => {
|
||||
|
||||
// spiral :: the first row plus a smaller spiral rotated 90 degrees clockwise
|
||||
// spiral :: Int -> Int -> Int -> [[Int]]
|
||||
function spiral(lngRows, lngCols, nStart) {
|
||||
return lngRows ? [range(nStart, (nStart + lngCols) - 1)]
|
||||
.concat(
|
||||
transpose(
|
||||
spiral(lngCols, lngRows - 1, nStart + lngCols)
|
||||
)
|
||||
.map(reverse)
|
||||
) : [[]];
|
||||
}
|
||||
|
||||
// transpose :: [[a]] -> [[a]]
|
||||
function transpose(xs) {
|
||||
return xs[0]
|
||||
.map((_, iCol) => xs
|
||||
.map((row) => row[iCol]));
|
||||
}
|
||||
|
||||
// reverse :: [a] -> [a]
|
||||
function reverse(xs) {
|
||||
return xs.slice(0)
|
||||
.reverse();
|
||||
}
|
||||
|
||||
// range(intFrom, intTo, optional intStep)
|
||||
// Int -> Int -> Maybe Int -> [Int]
|
||||
function range(m, n, step) {
|
||||
let d = (step || 1) * (n >= m ? 1 : -1);
|
||||
|
||||
return Array.from({
|
||||
length: Math.floor((n - m) / d) + 1
|
||||
}, (_, i) => m + (i * d));
|
||||
}
|
||||
|
||||
|
||||
|
||||
// TESTING
|
||||
|
||||
// replicate :: Int -> String -> String
|
||||
function replicate(n, a) {
|
||||
var v = [a],
|
||||
o = '';
|
||||
|
||||
if (n < 1) return o;
|
||||
while (n > 1) {
|
||||
if (n & 1) o = o + v;
|
||||
n >>= 1;
|
||||
v = v + v;
|
||||
}
|
||||
return o + v;
|
||||
}
|
||||
|
||||
|
||||
return spiral(n, n, 0)
|
||||
.map(
|
||||
xs => xs.map(x => {
|
||||
let s = `${x}`;
|
||||
return replicate(4 - s.length, ' ') + s;
|
||||
})
|
||||
.join('')
|
||||
)
|
||||
.join('\n');
|
||||
|
||||
})(5);
|
||||
Loading…
Add table
Add a link
Reference in a new issue