Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
121
Task/Floyds-triangle/JavaScript/floyds-triangle-1.js
Normal file
121
Task/Floyds-triangle/JavaScript/floyds-triangle-1.js
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
// FLOYD's TRIANGLE -------------------------------------------------------
|
||||
|
||||
// floyd :: Int -> [[Int]]
|
||||
function floyd(n) {
|
||||
return snd(mapAccumL(function (start, row) {
|
||||
return [start + row + 1, enumFromTo(start, start + row)];
|
||||
}, 1, enumFromTo(0, n - 1)));
|
||||
};
|
||||
|
||||
// showFloyd :: [[Int]] -> String
|
||||
function showFloyd(xss) {
|
||||
var ws = map(compose([succ, length, show]), last(xss));
|
||||
return unlines(map(function (xs) {
|
||||
return concat(zipWith(function (w, x) {
|
||||
return justifyRight(w, ' ', show(x));
|
||||
}, ws, xs));
|
||||
}, xss));
|
||||
};
|
||||
|
||||
|
||||
// GENERIC FUNCTIONS ------------------------------------------------------
|
||||
|
||||
// compose :: [(a -> a)] -> (a -> a)
|
||||
function compose(fs) {
|
||||
return function (x) {
|
||||
return fs.reduceRight(function (a, f) {
|
||||
return f(a);
|
||||
}, x);
|
||||
};
|
||||
};
|
||||
|
||||
// concat :: [[a]] -> [a] | [String] -> String
|
||||
function concat(xs) {
|
||||
if (xs.length > 0) {
|
||||
var unit = typeof xs[0] === 'string' ? '' : [];
|
||||
return unit.concat.apply(unit, xs);
|
||||
} else return [];
|
||||
};
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
function enumFromTo(m, n) {
|
||||
return Array.from({
|
||||
length: Math.floor(n - m) + 1
|
||||
}, function (_, i) {
|
||||
return m + i;
|
||||
});
|
||||
};
|
||||
|
||||
// justifyRight :: Int -> Char -> Text -> Text
|
||||
function justifyRight(n, cFiller, strText) {
|
||||
return n > strText.length ? (cFiller.repeat(n) + strText)
|
||||
.slice(-n) : strText;
|
||||
};
|
||||
|
||||
// last :: [a] -> a
|
||||
function last(xs) {
|
||||
return xs.length ? xs.slice(-1)[0] : undefined;
|
||||
};
|
||||
|
||||
// length :: [a] -> Int
|
||||
function length(xs) {
|
||||
return xs.length;
|
||||
};
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
function map(f, xs) {
|
||||
return xs.map(f);
|
||||
};
|
||||
|
||||
// 'The mapAccumL function behaves like a combination of map and foldl;
|
||||
// it applies a function to each element of a list, passing an accumulating
|
||||
// parameter from left to right, and returning a final value of this
|
||||
// accumulator together with the new list.' (See hoogle )
|
||||
|
||||
// mapAccumL :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
|
||||
function mapAccumL(f, acc, xs) {
|
||||
return xs.reduce(function (a, x) {
|
||||
var pair = f(a[0], x);
|
||||
|
||||
return [pair[0], a[1].concat([pair[1]])];
|
||||
}, [acc, []]);
|
||||
};
|
||||
|
||||
// show ::
|
||||
// (a -> String) f, Num n =>
|
||||
// a -> maybe f -> maybe n -> String
|
||||
var show = JSON.stringify;
|
||||
|
||||
// snd :: (a, b) -> b
|
||||
function snd(tpl) {
|
||||
return Array.isArray(tpl) ? tpl[1] : undefined;
|
||||
};
|
||||
|
||||
// succ :: Int -> Int
|
||||
function succ(x) {
|
||||
return x + 1;
|
||||
};
|
||||
|
||||
// unlines :: [String] -> String
|
||||
function unlines(xs) {
|
||||
return xs.join('\n');
|
||||
};
|
||||
|
||||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
function zipWith(f, xs, ys) {
|
||||
var ny = ys.length;
|
||||
return (xs.length <= ny ? xs : xs.slice(0, ny))
|
||||
.map(function (x, i) {
|
||||
return f(x, ys[i]);
|
||||
});
|
||||
};
|
||||
|
||||
// TEST ( n=5 and n=14 rows ) ---------------------------------------------
|
||||
|
||||
return unlines(map(function (n) {
|
||||
return showFloyd(floyd(n)) + '\n';
|
||||
}, [5, 14]));
|
||||
})();
|
||||
97
Task/Floyds-triangle/JavaScript/floyds-triangle-2.js
Normal file
97
Task/Floyds-triangle/JavaScript/floyds-triangle-2.js
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// FLOYD's TRIANGLE -------------------------------------------------------
|
||||
|
||||
// floyd :: Int -> [[Int]]
|
||||
const floyd = n => snd(mapAccumL(
|
||||
(start, row) => [start + row + 1, enumFromTo(start, start + row)],
|
||||
1, enumFromTo(0, n - 1)
|
||||
));
|
||||
|
||||
// showFloyd :: [[Int]] -> String
|
||||
const showFloyd = xss => {
|
||||
const ws = map(compose([succ, length, show]), last(xss));
|
||||
return unlines(
|
||||
map(xs => concat(zipWith(
|
||||
(w, x) => justifyRight(w, ' ', show(x)), ws, xs
|
||||
)),
|
||||
xss
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
// GENERIC FUNCTIONS ------------------------------------------------------
|
||||
|
||||
// compose :: [(a -> a)] -> (a -> a)
|
||||
const compose = fs => x => fs.reduceRight((a, f) => f(a), x);
|
||||
|
||||
// concat :: [[a]] -> [a] | [String] -> String
|
||||
const concat = xs => {
|
||||
if (xs.length > 0) {
|
||||
const unit = typeof xs[0] === 'string' ? '' : [];
|
||||
return unit.concat.apply(unit, xs);
|
||||
} else return [];
|
||||
};
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = (m, n) =>
|
||||
Array.from({
|
||||
length: Math.floor(n - m) + 1
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// justifyRight :: Int -> Char -> Text -> Text
|
||||
const justifyRight = (n, cFiller, strText) =>
|
||||
n > strText.length ? (
|
||||
(cFiller.repeat(n) + strText)
|
||||
.slice(-n)
|
||||
) : strText;
|
||||
|
||||
// last :: [a] -> a
|
||||
const last = xs => xs.length ? xs.slice(-1)[0] : undefined;
|
||||
|
||||
// length :: [a] -> Int
|
||||
const length = xs => xs.length;
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) => xs.map(f)
|
||||
|
||||
// 'The mapAccumL function behaves like a combination of map and foldl;
|
||||
// it applies a function to each element of a list, passing an accumulating
|
||||
// parameter from left to right, and returning a final value of this
|
||||
// accumulator together with the new list.' (See hoogle )
|
||||
|
||||
// mapAccumL :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
|
||||
const mapAccumL = (f, acc, xs) =>
|
||||
xs.reduce((a, x) => {
|
||||
const pair = f(a[0], x);
|
||||
|
||||
return [pair[0], a[1].concat([pair[1]])];
|
||||
}, [acc, []]);
|
||||
|
||||
// show ::
|
||||
// (a -> String) f, Num n =>
|
||||
// a -> maybe f -> maybe n -> String
|
||||
const show = JSON.stringify;
|
||||
|
||||
// snd :: (a, b) -> b
|
||||
const snd = tpl => Array.isArray(tpl) ? tpl[1] : undefined;
|
||||
|
||||
// succ :: Int -> Int
|
||||
const succ = x => x + 1
|
||||
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs => xs.join('\n');
|
||||
|
||||
|
||||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
const zipWith = (f, xs, ys) => {
|
||||
const ny = ys.length;
|
||||
return (xs.length <= ny ? xs : xs.slice(0, ny))
|
||||
.map((x, i) => f(x, ys[i]));
|
||||
};
|
||||
|
||||
// TEST ( n=5 and n=14 rows ) ---------------------------------------------
|
||||
|
||||
return unlines(map(n => showFloyd(floyd(n)) + '\n', [5, 14]))
|
||||
})();
|
||||
36
Task/Floyds-triangle/JavaScript/floyds-triangle-3.js
Normal file
36
Task/Floyds-triangle/JavaScript/floyds-triangle-3.js
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#!/usr/bin/env js
|
||||
|
||||
function main() {
|
||||
print('Floyd 5:');
|
||||
floyd(5);
|
||||
print('\nFloyd 14:');
|
||||
floyd(14);
|
||||
}
|
||||
|
||||
|
||||
function padLeft(s, w) {
|
||||
for (s = String(s); s.length < w; s = ' ' + s);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
function floyd(nRows) {
|
||||
var lowerLeft = nRows * (nRows - 1) / 2 + 1;
|
||||
var lowerRight = nRows * (nRows + 1) / 2;
|
||||
|
||||
var colWidths = [];
|
||||
for (var col = lowerLeft; col <= lowerRight; col++) {
|
||||
colWidths.push(String(col).length);
|
||||
}
|
||||
|
||||
var num = 1;
|
||||
for (var row = 0; row < nRows; row++) {
|
||||
var line = [];
|
||||
for (var col = 0; col <= row; col++, num++) {
|
||||
line.push(padLeft(num, colWidths[col]));
|
||||
}
|
||||
print(line.join(' '));
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
Loading…
Add table
Add a link
Reference in a new issue