Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,35 @@
function rangeExtraction(list) {
var len = list.length;
var out = [];
var i, j;
for (i = 0; i < len; i = j + 1) {
// beginning of range or single
out.push(list[i]);
// find end of range
for (var j = i + 1; j < len && list[j] == list[j-1] + 1; j++);
j--;
if (i == j) {
// single number
out.push(",");
} else if (i + 1 == j) {
// two numbers
out.push(",", list[j], ",");
} else {
// range
out.push("-", list[j], ",");
}
}
out.pop(); // remove trailing comma
return out.join("");
}
// using print function as supplied by Rhino standalone
print(rangeExtraction([
0, 1, 2, 4, 6, 7, 8, 11, 12, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
37, 38, 39
]));

View file

@ -0,0 +1,64 @@
(function () {
'use strict';
// rangeFormat :: [Int] -> String
var rangeFormat = function (xs) {
return splitBy(function (a, b) {
return b - a > 1;
}, xs)
.map(rangeString)
.join(',');
};
// rangeString :: [Int] -> String
var rangeString = function (xs) {
return xs.length > 2 ? [head(xs), last(xs)].map(show)
.join('-') : xs.join(',');
};
// GENERIC FUNCTIONS
// Splitting not on a delimiter, but whenever the relationship between
// two consecutive items matches a supplied predicate function
// splitBy :: (a -> a -> Bool) -> [a] -> [[a]]
var splitBy = function (f, xs) {
if (xs.length < 2) return [xs];
var h = head(xs),
lstParts = xs.slice(1)
.reduce(function (a, x) {
var acc = a[0],
active = a[1],
prev = a[2];
return f(prev, x) ? (
[acc.concat([active]), [x], x]
) : [acc, active.concat(x), x];
}, [
[],
[h], h
]);
return lstParts[0].concat([lstParts[1]]);
};
// head :: [a] -> a
var head = function (xs) {
return xs.length ? xs[0] : undefined;
};
// last :: [a] -> a
var last = function (xs) {
return xs.length ? xs.slice(-1)[0] : undefined;
};
// show :: a -> String
var show = function (x) {
return JSON.stringify(x);
};
// TEST
return rangeFormat([0, 1, 2, 4, 6, 7, 8, 11, 12, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 31, 32,
33, 35, 36, 37, 38, 39
]);
})();

View file

@ -0,0 +1,66 @@
(() => {
'use strict';
// ---------------- RANGE EXTRACTION -----------------
// rangeFormat :: [Int] -> String
const rangeFormat = xs =>
splitBy((a, b) => b - a > 1, xs)
.map(rangeString)
.join(',');
// rangeString :: [Int] -> String
const rangeString = xs =>
xs.length > 2 ? (
[xs[0], last(xs)].map(show)
.join('-')
) : xs.join(',')
// ---------------------- TEST -----------------------
const main = () =>
rangeFormat([0, 1, 2, 4, 6, 7, 8, 11, 12, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
37, 38, 39
]);
// ---------------- GENERIC FUNCTIONS ----------------
// Splitting not on a delimiter, but whenever the
// relationship between two consecutive items matches
// a supplied predicate function
// splitBy :: (a -> a -> Bool) -> [a] -> [[a]]
const splitBy = (f, xs) => {
if (xs.length < 2) return [xs];
const
h = xs[0],
lstParts = xs.slice(1)
.reduce(([acc, active, prev], x) =>
f(prev, x) ? (
[acc.concat([active]), [x], x]
) : [acc, active.concat(x), x], [
[],
[h],
h
]);
return lstParts[0].concat([lstParts[1]]);
};
// last :: [a] -> a
const last = xs => (
// The last item of a list.
ys => 0 < ys.length ? (
ys.slice(-1)[0]
) : undefined
)(xs);
// show :: a -> String
const show = x =>
JSON.stringify(x);
// MAIN --
return main();
})();

View file

@ -0,0 +1,50 @@
function toRange(arr) {
const ranges = [];
const sorted = [...arr.filter(Number.isInteger)].sort((x,y) => Math.sign(x-y));
const sequenceBreak = (x,y) => y - x > 1 ;
let i = 0;
while ( i < sorted.length ) {
let j = i ;
while ( j < sorted.length - 1 && !sequenceBreak( sorted[j], sorted[j+1] ) ) {
++j;
}
const from = sorted[i];
const thru = sorted[j];
const rangeLen = 1 + j - i;
if ( from === thru ) {
ranges.push( [from] );
} else {
if ( rangeLen > 2 ) {
ranges.push([from,thru]);
} else {
ranges.push([from], [thru]);
}
}
i = j+1;
}
return ranges.map( range => range.join('-') ).join(',');
}
// -----------------------------------------------------------------------------
// Test Case
// -----------------------------------------------------------------------------
const expected = '0-2,4,6-8,11,12,14-25,27-33,35-39';
const actual = toRange([
0, 1, 2,
4,
6, 7, 8,
11, 12, // should be two singletons
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
27, 28, 29, 30, 31, 32, 33,
35, 36, 37, 38, 39,
]);
console.log(`actual output : '${actual}'.`);
console.log(`expected output : '${expected}'.`);
console.log(`Correct? ${ actual === expected ? 'Yes' : 'No'}`);