Skip to content

Instantly share code, notes, and snippets.

@camisetags
Last active May 4, 2022 20:52
Show Gist options
  • Save camisetags/12927f1edfec824be8e76efd7d57a041 to your computer and use it in GitHub Desktop.
Save camisetags/12927f1edfec824be8e76efd7d57a041 to your computer and use it in GitHub Desktop.
chal
const inputA = 'AAAAAAAAAAAAABBCCCCDD'
function runLength(string) {
let result = '';
let count = 1;
let current = string[0];
for (let i = 1; i < string.length; i++) {
if (string[i] === current && count < 9) {
count++;
} else {
result += `${count}${current}`;
count = 1;
current = string[i];
}
}
result += `${count}${current}`;
return result;
}
const outputA = '9A4A2B4C2D'
console.log(JSON.stringify(runLength(inputA)) === JSON.stringify(outputA))
let inputB = [1, 2, 3, 5, 6, 8, 9]
function squareSort(array) {
return array.map(num => num * num).sort((a, b) => a - b);
}
const outputB = [1, 4, 9, 25, 36, 64, 81]
console.log(JSON.stringify(squareSort(inputB)) === JSON.stringify(outputB))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment