Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arihantverma/2c96eb1b82f8252e3e9b952ddd5a9cab to your computer and use it in GitHub Desktop.
Save arihantverma/2c96eb1b82f8252e3e9b952ddd5a9cab to your computer and use it in GitHub Desktop.
/* 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.");
}
@arihantverma
Copy link
Author

arihantverma commented Mar 17, 2021

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