Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
58
Task/Sierpinski-triangle/JavaScript/sierpinski-triangle-1.js
Normal file
58
Task/Sierpinski-triangle/JavaScript/sierpinski-triangle-1.js
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
(function (order) {
|
||||
|
||||
// Sierpinski triangle of order N constructed as
|
||||
// Pascal triangle of 2^N rows mod 2
|
||||
// with 1 encoded as "▲"
|
||||
// and 0 encoded as " "
|
||||
function sierpinski(intOrder) {
|
||||
return function asciiPascalMod2(intRows) {
|
||||
return range(1, intRows - 1)
|
||||
.reduce(function (lstRows) {
|
||||
var lstPrevRow = lstRows.slice(-1)[0];
|
||||
|
||||
// Each new row is a function of the previous row
|
||||
return lstRows.concat([zipWith(function (left, right) {
|
||||
// The composition ( asciiBinary . mod 2 . add )
|
||||
// reduces to a rule from 2 parent characters
|
||||
// to a single child character
|
||||
|
||||
// Rule 90 also reduces to the same XOR
|
||||
// relationship between left and right neighbours
|
||||
|
||||
return left === right ? " " : "▲";
|
||||
}, [' '].concat(lstPrevRow), lstPrevRow.concat(' '))]);
|
||||
}, [
|
||||
["▲"] // Tip of triangle
|
||||
]);
|
||||
}(Math.pow(2, intOrder))
|
||||
|
||||
// As centred lines, from bottom (0 indent) up (indent below + 1)
|
||||
.reduceRight(function (sofar, lstLine) {
|
||||
return {
|
||||
triangle: sofar.indent + lstLine.join(" ") + "\n" +
|
||||
sofar.triangle,
|
||||
indent: sofar.indent + " "
|
||||
};
|
||||
}, {
|
||||
triangle: "",
|
||||
indent: ""
|
||||
}).triangle;
|
||||
};
|
||||
|
||||
var zipWith = function (f, xs, ys) {
|
||||
return xs.length === ys.length ? xs
|
||||
.map(function (x, i) {
|
||||
return f(x, ys[i]);
|
||||
}) : undefined;
|
||||
},
|
||||
range = function (m, n) {
|
||||
return Array.apply(null, Array(n - m + 1))
|
||||
.map(function (x, i) {
|
||||
return m + i;
|
||||
});
|
||||
};
|
||||
|
||||
// TEST
|
||||
return sierpinski(order);
|
||||
|
||||
})(4);
|
||||
21
Task/Sierpinski-triangle/JavaScript/sierpinski-triangle-2.js
Normal file
21
Task/Sierpinski-triangle/JavaScript/sierpinski-triangle-2.js
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
function triangle(o) {
|
||||
var n = 1 << o,
|
||||
line = new Array(2 * n),
|
||||
i, j, t, u;
|
||||
for (i = 0; i < line.length; ++i) line[i] = ' ';
|
||||
line[n] = '*';
|
||||
for (i = 0; i < n; ++i) {
|
||||
document.write(line.join('') + "\n");
|
||||
u = '*';
|
||||
for (j = n - i; j < n + i + 1; ++j) {
|
||||
t = (line[j - 1] == line[j + 1] ? ' ' : '*');
|
||||
line[j - 1] = u;
|
||||
u = t;
|
||||
}
|
||||
line[n + i] = t;
|
||||
line[n + i + 1] = '*';
|
||||
}
|
||||
}
|
||||
document.write("<pre>\n");
|
||||
triangle(6);
|
||||
document.write("</pre>");
|
||||
26
Task/Sierpinski-triangle/JavaScript/sierpinski-triangle-3.js
Normal file
26
Task/Sierpinski-triangle/JavaScript/sierpinski-triangle-3.js
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
(() => {
|
||||
"use strict";
|
||||
|
||||
// --------------- SIERPINSKI TRIANGLE ---------------
|
||||
|
||||
// sierpinski :: Int -> String
|
||||
const sierpinski = n =>
|
||||
Array.from({
|
||||
length: n
|
||||
})
|
||||
.reduce(
|
||||
(xs, _, i) => {
|
||||
const s = " ".repeat(2 ** i);
|
||||
|
||||
return [
|
||||
...xs.map(x => s + x + s),
|
||||
...xs.map(x => `${x} ${x}`)
|
||||
];
|
||||
},
|
||||
["*"]
|
||||
)
|
||||
.join("\n");
|
||||
|
||||
// ---------------------- TEST -----------------------
|
||||
return sierpinski(4);
|
||||
})();
|
||||
70
Task/Sierpinski-triangle/JavaScript/sierpinski-triangle-4.js
Normal file
70
Task/Sierpinski-triangle/JavaScript/sierpinski-triangle-4.js
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
(() => {
|
||||
"use strict";
|
||||
|
||||
// ----- LINES OF SIERPINSKI TRIANGLE AT LEVEL N -----
|
||||
|
||||
// sierpinski :: Int -> [String]
|
||||
const sierpTriangle = n =>
|
||||
// Previous triangle centered with
|
||||
// left and right padding,
|
||||
0 < n ? (
|
||||
ap([
|
||||
map(
|
||||
xs => ap([
|
||||
compose(
|
||||
ks => ks.join(""),
|
||||
replicate(2 ** (n - 1))
|
||||
)
|
||||
])([" ", "-"])
|
||||
.join(xs)
|
||||
),
|
||||
|
||||
// above a pair of duplicates,
|
||||
// placed one character apart.
|
||||
map(s => `${s}+${s}`)
|
||||
])([sierpTriangle(n - 1)])
|
||||
.flat()
|
||||
) : ["▲"];
|
||||
|
||||
|
||||
// ---------------------- TEST -----------------------
|
||||
const main = () =>
|
||||
sierpTriangle(4)
|
||||
.join("\n");
|
||||
|
||||
|
||||
// ---------------- GENERIC FUNCTIONS ----------------
|
||||
|
||||
// ap (<*>) :: [(a -> b)] -> [a] -> [b]
|
||||
const ap = fs =>
|
||||
// The sequential application of each of a list
|
||||
// of functions to each of a list of values.
|
||||
// apList([x => 2 * x, x => 20 + x])([1, 2, 3])
|
||||
// -> [2, 4, 6, 21, 22, 23]
|
||||
xs => fs.flatMap(f => xs.map(f));
|
||||
|
||||
|
||||
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
const compose = (...fs) =>
|
||||
// A function defined by the right-to-left
|
||||
// composition of all the functions in fs.
|
||||
fs.reduce(
|
||||
(f, g) => x => f(g(x)),
|
||||
x => x
|
||||
);
|
||||
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = f => xs => xs.map(f);
|
||||
|
||||
|
||||
// replicate :: Int -> a -> [a]
|
||||
const replicate = n =>
|
||||
// A list of n copies of x.
|
||||
x => Array.from({
|
||||
length: n
|
||||
}, () => x);
|
||||
|
||||
// ---------------------- TEST -----------------------
|
||||
return main();
|
||||
})();
|
||||
69
Task/Sierpinski-triangle/JavaScript/sierpinski-triangle-5.js
Normal file
69
Task/Sierpinski-triangle/JavaScript/sierpinski-triangle-5.js
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
(() => {
|
||||
"use strict";
|
||||
|
||||
// --------------- SIERPINSKI TRIANGLE ---------------
|
||||
|
||||
// sierpinski :: Int -> [Bool]
|
||||
const sierpinski = intOrder =>
|
||||
// Reduce/folding from the last item (base of list)
|
||||
// which has zero left indent.
|
||||
|
||||
// Each preceding row has one more indent space
|
||||
// than the row beneath it.
|
||||
pascalMod2Chars(2 ** intOrder)
|
||||
.reduceRight((a, x) => ([
|
||||
`${a[1]}${x.join(" ")}\n${a[0]}`,
|
||||
`${a[1]} `
|
||||
]), ["", ""])[0];
|
||||
|
||||
|
||||
// pascalMod2Chars :: Int -> [[Char]]
|
||||
const pascalMod2Chars = nRows =>
|
||||
enumFromTo(1)(nRows - 1)
|
||||
.reduce(sofar => {
|
||||
const rows = sofar.slice(-1)[0];
|
||||
|
||||
// Rule 90 also reduces to the same XOR
|
||||
// relationship between left and right neighbours.
|
||||
return ([
|
||||
...sofar,
|
||||
zipWith(
|
||||
l => r => l === r ? (
|
||||
" "
|
||||
) : "*"
|
||||
)([" ", ...rows])([...rows, " "])
|
||||
]);
|
||||
}, [
|
||||
["*"]
|
||||
]);
|
||||
|
||||
// ---------------------- TEST -----------------------
|
||||
// main :: IO ()
|
||||
const main = () =>
|
||||
sierpinski(4);
|
||||
|
||||
|
||||
// --------------------- GENERIC ---------------------
|
||||
|
||||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
const zipWith = f =>
|
||||
// A list constructed by zipping with a
|
||||
// custom function, rather than with the
|
||||
// default tuple constructor.
|
||||
xs => ys => xs.map(
|
||||
(x, i) => f(x)(ys[i])
|
||||
).slice(
|
||||
0, Math.min(xs.length, ys.length)
|
||||
);
|
||||
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = m =>
|
||||
n => Array.from({
|
||||
length: 1 + n - m
|
||||
}, (_, i) => m + i);
|
||||
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue