Last active
March 17, 2021 15:16
-
-
Save arihantverma/2c96eb1b82f8252e3e9b952ddd5a9cab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Problem Name is &&& Run Length Encoding &&& PLEASE DO NOT REMOVE THIS LINE. */ | |
/** | |
* Instructions to candidate. | |
* 1) Run this code in the REPL to observe its behaviour. | |
* 2) Consider adding some additional tests in doTestsPass(). | |
* 3) Implement rle() correctly. | |
* 4) If time permits, try to improve your implementation. | |
*/ | |
/** | |
* rle ( testString ) | |
* | |
* Implement a run length encoding function. | |
* | |
* For a string input the function returns output encoded as follows: | |
* | |
* "a" -> "a1" | |
* "aa" -> "a2" | |
* "aabbb" -> "a2b3" | |
* "aabbbaa" -> "a2b3a2" | |
* "" -> "" | |
* | |
*/ | |
const _ = require("underscore"); | |
const rle = ( input ) => { | |
// write your code here | |
} | |
/** | |
* boolean doTestsPass() | |
* Returns true if all the tests pass. Otherwise returns false. | |
*/ | |
/** | |
* Returns true if all tests pass; otherwise, returns false. | |
*/ | |
const doTestsPass = () => { | |
const VALID_COMBOS = {"aaa": "a3", "aaabbc":"a3b2c1"}; | |
let testPassed = true; | |
_.forEach(VALID_COMBOS, function(value, key) { | |
console.log(key, rle(key)); | |
if (value !== rle(key)) { | |
testPassed = false; | |
} | |
}); | |
return testPassed; | |
} | |
/** | |
* Main execution entry. | |
*/ | |
if(doTestsPass()) | |
{ | |
console.log("All tests pass!"); | |
} | |
else | |
{ | |
console.log("There are test failures."); | |
} |
If you find any edge case, unhandled use case, or find something wrong in this implementation, kindly reply here with it :) And I'll update the solution.
If you find this solution helpful, kindly star the gist :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solution
repl link for the code below