Skip to content

Instantly share code, notes, and snippets.

@danielcodes
Created December 8, 2020 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielcodes/22cad763cf0b142ad84094171951ce10 to your computer and use it in GitHub Desktop.
Save danielcodes/22cad763cf0b142ad84094171951ce10 to your computer and use it in GitHub Desktop.
// problem 2
function lookAndSay(seq) {
let ans = [];
let start = 0;
while (start < seq.length) {
curr = seq[start];
step = start;
while (step < seq.length) {
if (curr != seq[step]) {
break;
}
step += 1;
}
let count = step - start;
ans.push([count, curr]);
start = step;
}
return ans;
}
const sequences = [[1], [1, 1], [2, 1], [1, 2, 1, 1]];
for(let i=0; i<sequences.length; i++){
console.log(lookAndSay(sequences[i]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment