Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
72
Task/Sierpinski-carpet/JavaScript/sierpinski-carpet-1.js
Normal file
72
Task/Sierpinski-carpet/JavaScript/sierpinski-carpet-1.js
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
|
||||
<title>Sierpinski Carpet</title>
|
||||
<script type='text/javascript'>
|
||||
|
||||
var black_char = "#";
|
||||
var white_char = " ";
|
||||
|
||||
var SierpinskiCarpet = function(size) {
|
||||
this.carpet = [black_char];
|
||||
for (var i = 1; i <= size; i++) {
|
||||
this.carpet = [].concat(
|
||||
this.carpet.map(this.sier_top),
|
||||
this.carpet.map(this.sier_middle),
|
||||
this.carpet.map(this.sier_top)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
SierpinskiCarpet.prototype.sier_top = function(x) {
|
||||
var str = new String(x);
|
||||
return new String(str+str+str);
|
||||
}
|
||||
|
||||
SierpinskiCarpet.prototype.sier_middle = function (x) {
|
||||
var str = new String(x);
|
||||
var spacer = str.replace(new RegExp(black_char, 'g'), white_char);
|
||||
return new String(str+spacer+str);
|
||||
}
|
||||
|
||||
SierpinskiCarpet.prototype.to_string = function() {
|
||||
return this.carpet.join("\n")
|
||||
}
|
||||
|
||||
SierpinskiCarpet.prototype.to_html = function(target) {
|
||||
var table = document.createElement('table');
|
||||
for (var i = 0; i < this.carpet.length; i++) {
|
||||
var row = document.createElement('tr');
|
||||
for (var j = 0; j < this.carpet[i].length; j++) {
|
||||
var cell = document.createElement('td');
|
||||
cell.setAttribute('class', this.carpet[i][j] == black_char ? 'black' : 'white');
|
||||
cell.appendChild(document.createTextNode('\u00a0'));
|
||||
row.appendChild(cell);
|
||||
}
|
||||
table.appendChild(row);
|
||||
}
|
||||
target.appendChild(table);
|
||||
}
|
||||
|
||||
</script>
|
||||
<style type='text/css'>
|
||||
table {border-collapse: collapse;}
|
||||
td {width: 1em;}
|
||||
.black {background-color: black;}
|
||||
.white {background-color: white;}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<pre id='to_string' style='float:left; margin-right:0.25in'></pre>
|
||||
<div id='to_html'></div>
|
||||
|
||||
<script type='text/javascript'>
|
||||
var sc = new SierpinskiCarpet(3);
|
||||
document.getElementById('to_string').appendChild(document.createTextNode(sc.to_string()));
|
||||
sc.to_html(document.getElementById('to_html'));
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
56
Task/Sierpinski-carpet/JavaScript/sierpinski-carpet-2.js
Normal file
56
Task/Sierpinski-carpet/JavaScript/sierpinski-carpet-2.js
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
// Orders 1, 2 and 3 of the Sierpinski Carpet
|
||||
// as lines of text.
|
||||
|
||||
// Generic text output for use in any JavaScript environment
|
||||
// Browser JavaScripts may use console.log() to return textual output
|
||||
// others use print() or analogous functions.
|
||||
|
||||
[1, 2, 3].map(function sierpinskiCarpetOrder(n) {
|
||||
|
||||
// An (n * n) grid of (filled or empty) sub-rectangles
|
||||
// n --> [[s]]
|
||||
var carpet = function (n) {
|
||||
var lstN = range(0, Math.pow(3, n) - 1);
|
||||
|
||||
// State of each cell in an N * N grid
|
||||
return lstN.map(function (x) {
|
||||
return lstN.map(function (y) {
|
||||
return inCarpet(x, y);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
// State of a given coordinate in the grid:
|
||||
// Filled or not ?
|
||||
// (See https://en.wikipedia.org/wiki/Sierpinski_carpet#Construction)
|
||||
// n --> n --> bool
|
||||
inCarpet = function (x, y) {
|
||||
return (!x || !y) ? true :
|
||||
!(
|
||||
(x % 3 === 1) &&
|
||||
(y % 3 === 1)
|
||||
) && inCarpet(
|
||||
x / 3 | 0,
|
||||
y / 3 | 0
|
||||
);
|
||||
},
|
||||
|
||||
// Sequence of integers from m to n
|
||||
// n --> n --> [n]
|
||||
range = function (m, n) {
|
||||
return Array.apply(null, Array(n - m + 1)).map(
|
||||
function (x, i) {
|
||||
return m + i;
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
// Grid of booleans mapped to lines of characters
|
||||
// [[bool]] --> s
|
||||
return carpet(n).map(function (line) {
|
||||
return line.map(function (bool) {
|
||||
return bool ? '\u2588' : ' ';
|
||||
}).join('');
|
||||
}).join('\n');
|
||||
|
||||
}).join('\n\n');
|
||||
43
Task/Sierpinski-carpet/JavaScript/sierpinski-carpet-3.js
Normal file
43
Task/Sierpinski-carpet/JavaScript/sierpinski-carpet-3.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// sierpinskiCarpet :: Int -> String
|
||||
let sierpinskiCarpet = n => {
|
||||
|
||||
// carpet :: Int -> [[String]]
|
||||
let carpet = n => {
|
||||
let xs = range(0, Math.pow(3, n) - 1);
|
||||
return xs.map(x => xs.map(y => inCarpet(x, y)));
|
||||
},
|
||||
|
||||
// https://en.wikipedia.org/wiki/Sierpinski_carpet#Construction
|
||||
|
||||
// inCarpet :: Int -> Int -> Bool
|
||||
inCarpet = (x, y) =>
|
||||
(!x || !y) ? true : !(
|
||||
(x % 3 === 1) &&
|
||||
(y % 3 === 1)
|
||||
) && inCarpet(
|
||||
x / 3 | 0,
|
||||
y / 3 | 0
|
||||
);
|
||||
|
||||
return carpet(n)
|
||||
.map(line => line.map(bool => bool ? '\u2588' : ' ')
|
||||
.join(''))
|
||||
.join('\n');
|
||||
};
|
||||
|
||||
// GENERIC
|
||||
|
||||
// range :: Int -> Int -> [Int]
|
||||
let range = (m, n) =>
|
||||
Array.from({
|
||||
length: Math.floor(n - m) + 1
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// TEST
|
||||
|
||||
return [1, 2, 3]
|
||||
.map(sierpinskiCarpet);
|
||||
})();
|
||||
92
Task/Sierpinski-carpet/JavaScript/sierpinski-carpet-4.js
Normal file
92
Task/Sierpinski-carpet/JavaScript/sierpinski-carpet-4.js
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// weave :: [String] -> [String]
|
||||
const weave = xs => {
|
||||
const f = zipWith(append);
|
||||
return concatMap(
|
||||
x => f(f(xs)(x))(xs)
|
||||
)([
|
||||
xs,
|
||||
map(x => replicate(length(x))(' '))(
|
||||
xs
|
||||
),
|
||||
xs
|
||||
]);
|
||||
};
|
||||
|
||||
// TEST -----------------------------------------------
|
||||
const main = () => {
|
||||
const
|
||||
sierp = n => unlines(
|
||||
take(1 + n, iterate(weave, ['\u2588']))[n]
|
||||
),
|
||||
carpet = sierp(2);
|
||||
return (
|
||||
// console.log(carpet),
|
||||
carpet
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
// GENERIC ABSTRACTIONS -------------------------------
|
||||
|
||||
// append (++) :: [a] -> [a] -> [a]
|
||||
// append (++) :: String -> String -> String
|
||||
const append = xs => ys => xs.concat(ys);
|
||||
|
||||
// concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
const concatMap = f => xs =>
|
||||
xs.reduce((a, x) => a.concat(f(x)), []);
|
||||
|
||||
// iterate :: (a -> a) -> a -> Gen [a]
|
||||
function* iterate(f, x) {
|
||||
let v = x;
|
||||
while (true) {
|
||||
yield(v);
|
||||
v = f(v);
|
||||
}
|
||||
}
|
||||
|
||||
// Returns Infinity over objects without finite length
|
||||
// this enables zip and zipWith to choose the shorter
|
||||
// argument when one is non-finite, like cycle, repeat etc
|
||||
|
||||
// length :: [a] -> Int
|
||||
const length = xs => xs.length || Infinity;
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = f => xs => xs.map(f);
|
||||
|
||||
// replicate :: Int -> String -> String
|
||||
const replicate = n => s => s.repeat(n);
|
||||
|
||||
// take :: Int -> [a] -> [a]
|
||||
// take :: Int -> String -> String
|
||||
const take = (n, xs) =>
|
||||
xs.constructor.constructor.name !== 'GeneratorFunction' ? (
|
||||
xs.slice(0, n)
|
||||
) : [].concat.apply([], Array.from({
|
||||
length: n
|
||||
}, () => {
|
||||
const x = xs.next();
|
||||
return x.done ? [] : [x.value];
|
||||
}));
|
||||
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs => xs.join('\n');
|
||||
|
||||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
const zipWith = f => xs => ys => {
|
||||
const
|
||||
lng = Math.min(length(xs), length(ys)),
|
||||
as = take(lng, xs),
|
||||
bs = take(lng, ys);
|
||||
return Array.from({
|
||||
length: lng
|
||||
}, (_, i) => f(as[i])(bs[i]));
|
||||
};
|
||||
|
||||
// MAIN -----------------------------------------------
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue