44 * give us representation of string in binary which in which the
55 * zeros will be stripped and replaced with their count.
66 */
7- var runLengthEncoding = ( function ( ) {
8-
7+ ( function ( exports ) {
98 'use strict' ;
109
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 ;
10+ var runLengthEncoding = ( function ( ) {
11+
12+ /**
13+ * Convers a given string to sequence of numbers
14+ * This takes O(n).
15+ */
16+ function convertToAscii ( str ) {
17+ var result = '' ,
18+ currentChar = '' ,
19+ i = 0 ;
20+ for ( ; i < str . length ; i += 1 ) {
21+ currentChar = str [ i ] . charCodeAt ( 0 ) . toString ( 2 ) ;
22+ if ( currentChar . length < 8 ) {
23+ while ( 8 - currentChar . length ) {
24+ currentChar = '0' + currentChar ;
25+ }
2426 }
27+ result += currentChar ;
2528 }
26- result += currentChar ;
29+ return result ;
2730 }
28- return result ;
29- }
3031
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 ;
32+ /**
33+ * Encodes the binary string to run-length encoding.
34+ * Takes O(n^2).
35+ */
36+ function runLength ( vector ) {
37+ var result = '' ,
38+ zeros = 0 ,
39+ zerosTemp = '' ,
40+ wordLength = 0 ,
41+ i = 0 ;
42+ for ( ; i < vector . length ; i += 1 ) {
43+ if ( vector [ i ] === '0' ) {
44+ zeros += 1 ;
45+ } else {
46+ zerosTemp = zeros . toString ( 2 ) ;
47+ wordLength = zerosTemp . length - 1 ;
48+ while ( wordLength ) {
49+ result = result + '1' ;
50+ wordLength -= 1 ;
51+ }
52+ result += '0' + zerosTemp ;
53+ zeros = 0 ;
5054 }
51- result += '0' + zerosTemp ;
52- zeros = 0 ;
5355 }
56+ return result ;
5457 }
55- return result ;
56- }
5758
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- } ;
59+ /**
60+ * Accepts a string and returns it's run-length
61+ * encoded binary representation.
62+ * Takes O(n^2).
63+ */
64+ return function ( str ) {
65+ var asciiString = convertToAscii ( str ) ;
66+ return runLength ( asciiString ) ;
67+ } ;
68+
69+ } ( ) ) ;
70+
71+ exports . runLength = runLengthEncoding ;
6672
67- } ( ) ) ;
73+ } ( typeof exports === 'undefined' ? window : exports ) ) ;
0 commit comments