Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. /**
  12. * Реализовать RLE-сжатие: AAAB -> A3B, BCCDDDAXXXX -> BC2D3AX4
  13. * @param {string} value
  14. * @return {string}
  15. */
  16. function rle(value) {
  17. const result = [(value.length > 0 ? value[0] : '')];
  18.  
  19. for(let i = 1; i <= value.length; i++) {
  20. if (result[result.length - 1] != value[i]) {
  21. if (i - result.length + 1 > 1) {
  22. result[result.length - 1] = result[result.length - 1] + (i - result.length + 1);
  23. }
  24. result[i] = value[i];
  25. }
  26. }
  27.  
  28. return result.join('');
  29. }
  30.  
  31. console.log(rle('AVVVBBBVVXDHJFFFFDDDDDDHAAAAJJJDDSLSSSDDDD'));
  32.  
  33. </script>
  34.  
  35.  
  36.  
  37. <script id="jsbin-source-javascript" type="text/javascript">/**
  38. * Реализовать RLE-сжатие: AAAB -> A3B, BCCDDDAXXXX -> BC2D3AX4
  39. * @param {string} value
  40. * @return {string}
  41. */
  42. function rle(value) {
  43. const result = [(value.length > 0 ? value[0] : '')];
  44.  
  45. for(let i = 1; i <= value.length; i++) {
  46. if (result[result.length - 1] != value[i]) {
  47. if (i - result.length + 1 > 1) {
  48. result[result.length - 1] = result[result.length - 1] + (i - result.length + 1);
  49. }
  50. result[i] = value[i];
  51. }
  52. }
  53.  
  54. return result.join('');
  55. }
  56.  
  57. console.log(rle('AVVVBBBVVXDHJFFFFDDDDDDHAAAAJJJDDSLSSSDDDD'));
  58. </script></body>
  59. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement