Skip to content

Instantly share code, notes, and snippets.

@Dmitriy-8-Kireev
Created January 15, 2019 14:46
Show Gist options
  • Save Dmitriy-8-Kireev/35174057e42d2eaa4df93238aa3f6643 to your computer and use it in GitHub Desktop.
Save Dmitriy-8-Kireev/35174057e42d2eaa4df93238aa3f6643 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/deyinop
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/2.0.8/es5-shim.min.js"></script>
<script src="https://cdn.rawgit.com/zloirock/core-js/master/client/shim.min.js"></script>
<script id="jsbin-javascript">
/**
* Реализовать RLE-сжатие: AAAB -> A3B, BCCDDDAXXXX -> BC2D3AX4
* @param {string} value
* @return {string}
*/
const 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, '');
};
const rez = rle('AVVVBBBVVXDHJFFFFDDDDDDHAAAAJJJDDSLSSSDDDD')
console.log(rez)
</script>
<script id="jsbin-source-javascript" type="text/javascript">/**
* Реализовать RLE-сжатие: AAAB -> A3B, BCCDDDAXXXX -> BC2D3AX4
* @param {string} value
* @return {string}
*/
const 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, '');
};
const rez = rle('AVVVBBBVVXDHJFFFFDDDDDDHAAAAJJJDDSLSSSDDDD')
console.log(rez)</script></body>
</html>
/**
* Реализовать RLE-сжатие: AAAB -> A3B, BCCDDDAXXXX -> BC2D3AX4
* @param {string} value
* @return {string}
*/
const 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, '');
};
const rez = rle('AVVVBBBVVXDHJFFFFDDDDDDHAAAAJJJDDSLSSSDDDD')
console.log(rez)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment