1- var str = 'Sample' ;
2-
31/**
42 * Run-length encoding.
53 * The idea of this algorithm is to remove the usless zeros and
64 * give us representation of string in binary which in which the
75 * zeros will be stripped and replaced with their count.
86 */
9- var runLengthEncoding = ( function ( ) {
7+ var runLengthEncoding = ( function ( ) {
108
11- 'use strict' ;
9+ 'use strict' ;
1210
13- /**
14- * Convers a given string to sequence of numbers
15- * This takes O(n).
16- */
17- function convertToAscii ( str ) {
18- var result = '' ,
19- currentChar = '' ,
20- i = 0 ;
21- for ( ; i < str . length ; i += 1 ) {
22- currentChar = str [ i ] . charCodeAt ( 0 ) . toString ( 2 ) ;
23- if ( currentChar . length < 8 ) {
24- while ( 8 - currentChar . length ) {
25- currentChar = '0' + currentChar ;
26- }
27- }
28- result += currentChar ;
29- }
30- return result ;
31- }
32-
33- /**
34- * Encodes the binary string to run-length encoding.
35- * Takes O(n^2).
36- */
37- function runLength ( vector ) {
38- var result = '' ,
39- zeros = 0 ,
40- zerosTemp = '' ,
41- wordLength = 0 ,
42- i = 0 ;
43- for ( ; i < vector . length ; i += 1 ) {
44- if ( vector [ i ] === '0' ) {
45- zeros += 1 ;
46- } else {
47- zerosTemp = zeros . toString ( 2 ) ;
48- wordLength = zerosTemp . length - 1 ;
49- while ( wordLength ) {
50- result = result + '1' ;
51- wordLength -= 1 ;
52- }
53- result += '0' + zerosTemp ;
54- zeros = 0 ;
55- }
11+ /**
12+ * Convers a given string to sequence of numbers
13+ * This takes O(n).
14+ */
15+ function convertToAscii ( str ) {
16+ var result = '' ,
17+ currentChar = '' ,
18+ i = 0 ;
19+ for ( ; i < str . length ; i += 1 ) {
20+ currentChar = str [ i ] . charCodeAt ( 0 ) . toString ( 2 ) ;
21+ if ( currentChar . length < 8 ) {
22+ while ( 8 - currentChar . length ) {
23+ currentChar = '0' + currentChar ;
5624 }
57- return result ;
25+ }
26+ result += currentChar ;
5827 }
28+ return result ;
29+ }
5930
60- /**
61- * Accepts a string and returns it's run-length encoded binary representation.
62- * Takes O(n^2).
63- */
64- return function ( str ) {
65- var asciiString = convertToAscii ( str ) ;
66- return runLength ( asciiString ) ;
31+ /**
32+ * Encodes the binary string to run-length encoding.
33+ * Takes O(n^2).
34+ */
35+ function runLength ( vector ) {
36+ var result = '' ,
37+ zeros = 0 ,
38+ zerosTemp = '' ,
39+ wordLength = 0 ,
40+ i = 0 ;
41+ for ( ; i < vector . length ; i += 1 ) {
42+ if ( vector [ i ] === '0' ) {
43+ zeros += 1 ;
44+ } else {
45+ zerosTemp = zeros . toString ( 2 ) ;
46+ wordLength = zerosTemp . length - 1 ;
47+ while ( wordLength ) {
48+ result = result + '1' ;
49+ wordLength -= 1 ;
50+ }
51+ result += '0' + zerosTemp ;
52+ zeros = 0 ;
53+ }
6754 }
55+ return result ;
56+ }
6857
69- } ( ) ) ;
58+ /**
59+ * Accepts a string and returns it's run-length encoded binary representation.
60+ * Takes O(n^2).
61+ */
62+ return function ( str ) {
63+ var asciiString = convertToAscii ( str ) ;
64+ return runLength ( asciiString ) ;
65+ } ;
7066
71- console . log ( runLengthEncoding ( str ) ) ;
67+ } ( ) ) ;
0 commit comments