Skip to content

Instantly share code, notes, and snippets.

@Dmitriy-8-Kireev
Created January 15, 2019 22:00
Show Gist options
  • Save Dmitriy-8-Kireev/6146f3f7d80ac49004768e387102066b to your computer and use it in GitHub Desktop.
Save Dmitriy-8-Kireev/6146f3f7d80ac49004768e387102066b to your computer and use it in GitHub Desktop.
// source https://jsbin.com
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
/**
* Реализовать RLE-сжатие: AAAB -> A3B, BCCDDDAXXXX -> BC2D3AX4
* @param {string} value
* @return {string}
*/
function rle(value) {
const iter = (i, ctr, acc) => {
if (i === value.length) {
return acc;
}
if (value[i] !== value[i + 1]) {
acc += `${value[i]}${ctr !== 1 ? ctr : ""}`;
ctr = 0;
}
return iter(i + 1, ctr + 1, acc);
};
return iter(0, 1, "");
}
console.log(rle("AVVVBBBVVXDHJFFFFDDDDDDHAAAAJJJDDSLSSSDDDD"));
</script>
<script id="jsbin-source-javascript" type="text/javascript">/**
* Реализовать RLE-сжатие: AAAB -> A3B, BCCDDDAXXXX -> BC2D3AX4
* @param {string} value
* @return {string}
*/
function rle(value) {
const iter = (i, ctr, acc) => {
if (i === value.length) {
return acc;
}
if (value[i] !== value[i + 1]) {
acc += `${value[i]}${ctr !== 1 ? ctr : ""}`;
ctr = 0;
}
return iter(i + 1, ctr + 1, acc);
};
return iter(0, 1, "");
}
console.log(rle("AVVVBBBVVXDHJFFFFDDDDDDHAAAAJJJDDSLSSSDDDD"));</script></body>
</html>
/**
* Реализовать RLE-сжатие: AAAB -> A3B, BCCDDDAXXXX -> BC2D3AX4
* @param {string} value
* @return {string}
*/
function rle(value) {
const iter = (i, ctr, acc) => {
if (i === value.length) {
return acc;
}
if (value[i] !== value[i + 1]) {
acc += `${value[i]}${ctr !== 1 ? ctr : ""}`;
ctr = 0;
}
return iter(i + 1, ctr + 1, acc);
};
return iter(0, 1, "");
}
console.log(rle("AVVVBBBVVXDHJFFFFDDDDDDHAAAAJJJDDSLSSSDDDD"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment