2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -0,0 +1,97 @@
(function (n) {
// Read range of values into a series of 'diagonal rows'
// for a square of given dimension,
// starting at diagonal row i.
// [
// [0],
// [1, 2],
// [3, 4, 5],
// [6, 7, 8, 9],
// [10, 11, 12, 13, 14],
// [15, 16, 17, 18],
// [19, 20, 21],
// [22, 23],
// [24]
// ]
// diagonals :: n -> [[n]]
function diagonals(n) {
function diags(xs, iCol, iRow) {
if (iCol < xs.length) {
var xxs = splitAt(iCol, xs);
return [xxs[0]].concat(diags(
xxs[1],
(iCol + (iRow < n ? 1 : -1)),
iRow + 1
));
} else return [xs];
}
return diags(range(0, n * n - 1), 1, 1);
}
// Recursively read off n heads from the diagonals (as rows)
// n -> [[n]] -> [[n]]
function nHeads(n, lst) {
var zipEdge = lst.slice(0, n);
return lst.length ? [zipEdge.map(function (x) {
return x[0];
})].concat(nHeads(n, [].concat.apply([], zipEdge.map(function (
x) {
return x.length > 1 ? [x.slice(1)] : [];
}))
.concat(lst.slice(n)))) : [];
}
// range(intFrom, intTo, optional intStep)
// Int -> Int -> Maybe Int -> [Int]
function range(m, n, delta) {
var d = delta || 1,
blnUp = n > m,
lng = Math.floor((blnUp ? n - m : m - n) / d) + 1,
a = Array(lng),
i = lng;
if (blnUp)
while (i--) a[i] = (d * i) + m;
else
while (i--) a[i] = m - (d * i);
return a;
}
// splitAt :: Int -> [a] -> ([a],[a])
function splitAt(n, xs) {
return [xs.slice(0, n), xs.slice(n)];
}
// Recursively take n heads from the alternately reversed diagonals
// [ [
// [0], -> [0, 1, 5, 6, 14] and:
// [1, 2], [2],
// [5, 4, 3], [4, 3],
// [6, 7, 8, 9], [7, 8, 9],
// [14, 13, 12, 11, 10], [13, 12, 11, 10],
// [15, 16, 17, 18], [15, 16, 17, 18],
// [21, 20, 19], [21, 20, 19],
// [22, 23], [22, 23],
// [24] [24]
// ] ]
//
// In the next recursion with the remnant on the right, the next
// 5 heads will be [2, 4, 7, 13, 15] - the second row of our zig zag matrix.
// (and so forth)
return nHeads(n, diagonals(n)
.map(function (x, i) {
i % 2 || x.reverse();
return x;
}));
})(5);

View file

@ -0,0 +1,5 @@
[[0, 1, 5, 6, 14],
[2, 4, 7, 13, 15],
[3, 8, 12, 16, 21],
[9, 11, 17, 20, 22],
[10, 18, 19, 23, 24]]

View file

@ -0,0 +1,60 @@
(n => {
// diagonals :: n -> [[n]]
function diagonals(n) {
let diags = (xs, iCol, iRow) => {
if (iCol < xs.length) {
let xxs = splitAt(iCol, xs);
return [xxs[0]].concat(diags(
xxs[1],
iCol + (iRow < n ? 1 : -1),
iRow + 1
));
} else return [xs];
}
return diags(range(0, n * n - 1), 1, 1);
}
// Recursively read off n heads of diagonal lists
// rowsFromDiagonals :: n -> [[n]] -> [[n]]
function rowsFromDiagonals(n, lst) {
if (lst.length) {
let [edge, rest] = splitAt(n, lst);
return [edge.map(x => x[0])]
.concat(rowsFromDiagonals(n,
edge.filter(x => x.length > 1)
.map(x => x.slice(1))
.concat(rest)
));
} else return [];
}
// GENERIC FUNCTIONS
// splitAt :: Int -> [a] -> ([a],[a])
function splitAt(n, xs) {
return [xs.slice(0, n), xs.slice(n)];
}
// range :: From -> To -> Maybe Step -> [Int]
// range :: 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));
}
// ZIG-ZAG MATRIX
return rowsFromDiagonals(n,
diagonals(n)
.map((x, i) => (i % 2 || x.reverse()) && x)
);
})(5);

View file

@ -0,0 +1,5 @@
[[0, 1, 5, 6, 14],
[2, 4, 7, 13, 15],
[3, 8, 12, 16, 21],
[9, 11, 17, 20, 22],
[10, 18, 19, 23, 24]]