2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
50
Task/Pascals-triangle/JavaScript/pascals-triangle-4.js
Normal file
50
Task/Pascals-triangle/JavaScript/pascals-triangle-4.js
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// pascal :: Int -> [[Int]]
|
||||
let pascal = n =>
|
||||
range(1, n - 1)
|
||||
.reduce(a => {
|
||||
let lstPreviousRow = a.slice(-1)[0];
|
||||
|
||||
return a
|
||||
.concat([zipWith((a, b) => a + b,
|
||||
[0].concat(lstPreviousRow),
|
||||
lstPreviousRow.concat(0)
|
||||
)]);
|
||||
}, [
|
||||
[1]
|
||||
]);
|
||||
|
||||
// GENERIC FUNCTIONS
|
||||
|
||||
// Int -> Int -> Maybe Int -> [Int]
|
||||
let 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));
|
||||
},
|
||||
|
||||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
zipWith = (f, xs, ys) =>
|
||||
xs.length === ys.length ? (
|
||||
xs.map((x, i) => f(x, ys[i]))
|
||||
) : undefined;
|
||||
|
||||
// TEST
|
||||
return pascal(7)
|
||||
.reduceRight((a, x) => {
|
||||
let strIndent = a.indent;
|
||||
|
||||
return {
|
||||
rows: strIndent + x
|
||||
.map(n => (' ' + n).slice(-4))
|
||||
.join('') + '\n' + a.rows,
|
||||
indent: strIndent + ' '
|
||||
};
|
||||
}, {
|
||||
rows: '',
|
||||
indent: ''
|
||||
}).rows;
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue