CtCI-6th-Edition-JavaScript icon indicating copy to clipboard operation
CtCI-6th-Edition-JavaScript copied to clipboard

1.6 Length bug

Open dawsbot opened this issue 8 years ago • 5 comments

The way that string length is compared and calculated is not correct when strings have significant entropy. See my branch showing this here @tankwan

dawsbot avatar May 21 '17 21:05 dawsbot

Do you want to fix this @tankwan? I can if not.

dawsbot avatar May 21 '17 21:05 dawsbot

Hey @dawsbot, hope you're having a great weekend! Sure thing, I can take care of it :)

On Sun, 21 May 2017 at 14:02 Dawson Botsford [email protected] wrote:

Do you want to fix this @tankwan https://github.com/tankwan? I can if not.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/careercup/CtCI-6th-Edition-JavaScript/issues/14#issuecomment-302963057, or mute the thread https://github.com/notifications/unsubscribe-auth/AEhn5XRlVvasQXycgtMpLJwhFUE1HGjHks5r8KZ_gaJpZM4NhuTU .

heytonytan avatar May 22 '17 04:05 heytonytan

@thetonytan - ping!

profnandaa avatar Oct 02 '18 18:10 profnandaa

Bug in this test console.log('aabbcc', strComp('aabbcc'), 'aabbcc') As the book says, if the length of the result is not less than the original should print the original, and it is printing: a2b2c2

yuriaps avatar Sep 27 '20 00:09 yuriaps

I don't see why use the Math.max()... I would go this way

var strComp = function(string) {
    var compressed = '';
    var currChar = '';
    var currCount = '';
    for (var i = 0; i < string.length; i++) {
      if (currChar !== string[i]) {
        compressed = compressed + currChar + currCount;
        currChar = string[i];
        currCount = 1;
      } else {
        currCount++;
      }
    }
    compressed = compressed + currChar + currCount;
  
    return compressed.length >= string.length ? string : compressed;
  };

yuriaps avatar Sep 27 '20 00:09 yuriaps