Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,15 @@
function encode(input) {
var encoding = [];
var prev, count, i;
for (count = 1, prev = input[0], i = 1; i < input.length; i++) {
if (input[i] != prev) {
encoding.push([count, prev]);
count = 1;
prev = input[i];
}
else
count ++;
}
encoding.push([count, prev]);
return encoding;
}

View file

@ -0,0 +1,5 @@
function encode_re(input) {
var encoding = [];
input.match(/(.)\1*/g).forEach(function(substr){ encoding.push([substr.length, substr[0]]) });
return encoding;
}

View file

@ -0,0 +1,5 @@
function decode(encoded) {
var output = "";
encoded.forEach(function(pair){ output += new Array(1+pair[0]).join(pair[1]) })
return output;
}

View file

@ -0,0 +1,51 @@
(() => {
'use strict';
// runLengthEncode :: String -> [(Int, Char)]
const runLengthEncoded = s =>
group(s.split('')).map(
cs => [cs.length, cs[0]]
);
// runLengthDecoded :: [(Int, Char)] -> String
const runLengthDecoded = pairs =>
pairs.map(([n, c]) => c.repeat(n)).join('');
// ------------------------TEST------------------------
const main = () => {
const
xs = 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWW' +
'WWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW',
ys = runLengthEncoded(xs);
console.log('From: ', show(xs));
[ys, runLengthDecoded(ys)].forEach(
x => console.log(' -> ', show(x))
)
};
// ----------------------GENERIC-----------------------
// group :: [a] -> [[a]]
const group = xs => {
// A list of lists, each containing only equal elements,
// such that the concatenation of these lists is xs.
const go = xs =>
0 < xs.length ? (() => {
const
h = xs[0],
i = xs.findIndex(x => h !== x);
return i !== -1 ? (
[xs.slice(0, i)].concat(go(xs.slice(i)))
) : [xs];
})() : [];
return go(xs);
};
// show :: a -> String
const show = JSON.stringify;
// MAIN ---
return main();
})();

View file

@ -0,0 +1,3 @@
const rlEncode = (s: string) => s.match(/(.)\1*/g).reduce((result,char) => result+char.length+char[0],"")
const rlValidate = (s: string) => /^(\d+\D)+$/.test(s)
const rlDecode = (s: string) => rlValidate(s) ? s.match(/(\d[a-z\s])\1*/ig).reduce((res,p) => res+p[p.length-1].repeat(parseInt(p)),"") : Error("Invalid rl")