Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
32
Task/Zig-zag-matrix/JavaScript/zig-zag-matrix-1.js
Normal file
32
Task/Zig-zag-matrix/JavaScript/zig-zag-matrix-1.js
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
function ZigZagMatrix(n) {
|
||||
this.height = n;
|
||||
this.width = n;
|
||||
|
||||
this.mtx = [];
|
||||
for (var i = 0; i < n; i++)
|
||||
this.mtx[i] = [];
|
||||
|
||||
var i=1, j=1;
|
||||
for (var e = 0; e < n*n; e++) {
|
||||
this.mtx[i-1][j-1] = e;
|
||||
if ((i + j) % 2 == 0) {
|
||||
// Even stripes
|
||||
if (j < n) j ++;
|
||||
else i += 2;
|
||||
if (i > 1) i --;
|
||||
} else {
|
||||
// Odd stripes
|
||||
if (i < n) i ++;
|
||||
else j += 2;
|
||||
if (j > 1) j --;
|
||||
}
|
||||
}
|
||||
}
|
||||
ZigZagMatrix.prototype = Matrix.prototype;
|
||||
|
||||
var z = new ZigZagMatrix(5);
|
||||
print(z);
|
||||
print();
|
||||
|
||||
z = new ZigZagMatrix(4);
|
||||
print(z);
|
||||
97
Task/Zig-zag-matrix/JavaScript/zig-zag-matrix-2.js
Normal file
97
Task/Zig-zag-matrix/JavaScript/zig-zag-matrix-2.js
Normal 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);
|
||||
60
Task/Zig-zag-matrix/JavaScript/zig-zag-matrix-3.js
Normal file
60
Task/Zig-zag-matrix/JavaScript/zig-zag-matrix-3.js
Normal 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);
|
||||
Loading…
Add table
Add a link
Reference in a new issue