// Daily Coding Challenge #29
// 'Run-length encoding': replace repeated characters with a number before the character.
// Example: "AAABBC" -> "3A2B1C"
// Example: "ZZyaaaa" -> "2Z1y4a"
function encode(str){
var lastChar = 0;
var streak = 0;
var output = "";
for(var xint = 0, len = str.length;xint < len;xint++){
var curChar = str.charAt(xint);
if(lastChar != curChar){
if(lastChar != 0){ // not the first char
output += streak;
output += lastChar;
}
streak = 1;
lastChar = curChar;
}else streak++;
}
if(streak > 0){ // log the last streak/char
output += streak;
output += lastChar;
}
return output;
}
// tests
console.log('Test #1: ' + (encode('AAABBC') == "3A2B1C" ? ("PASS") : ("FAIL")));
console.log('Test #2: ' + (encode('ZZyaaaa') == "2Z1y4a" ? ("PASS") : ("FAIL")));
Subscribe to:
Post Comments (Atom)
Coding Challenge #54 C++ int to std::string (no stringstream or to_string())
Gets a string from an integer (ejemplo gratis: 123 -> "123") Wanted to come up with my own function for this like 10 years ago ...
-
I NEEDS MY JAVASCRIPT PLEASE :) Open txtcode .txt Save as .gct Please click the blue cheat code title to activate it, a...
-
So you have a few .wbfs files from your wii game backups. You want their IDs so you can get cheats or cover art for them. Where do you find...
-
submitted for Coursera quiz. Read int's from a file, and calculate their average #include <stdio.h> int main(){ FILE *filePoi...
No comments:
Post a Comment