Day03: Common Character count, Lucky Ticket number & Sort by height (In another life)

Day03: Common Character count, Lucky Ticket number & Sort by height (In another life)

MILESTONE 1: Complete Arcade Intro CodeSignal

Today, I use Clockify to track time to solve issues.

image.png

DONE: 12/60

Day03: I solve 3 code challenges in arcade. I stuck very long on challenge 4 (ReverseInParentheses). I think this problem will need to use Stack Data Structure, but now after 1h, I cannot find out the way to solve.

Here is what I learn:

Challenge 7: commonCharacterCount

image.png

Here is my solution:

function commonCharacterCount(s1, s2) {
  s2 = s2.split("");
  let count = 0;
  for (let i = 0; i < s1.length; i++) {

    if (s2.indexOf(s1[i]) > -1) {
      count++;
      s2.splice(s2.indexOf(s1[i]), 1);
    }
  }
  return count;
}

I use really straight forward solution. That's loop through 1 string and count the same element between 2 string.

Here is highest point solution in JS:

function commonCharacterCount(s1, s2) {
  for (var i = 0; i < s1.length; i++) {
    s2 = s2.replace(s1[i], "!");
  }
  return s2.replace(/[^!]/g, "").length;
}

What I learn here:

  • I learn the awesome way to use replace method. The combination between it with regex to count element in array is cool.

Challenge 8: commonCharacterCount

image.png

Here is my solution:

function isLucky(n) {
  const numArr = n.toString().split("");

  const halfArr = numArr.splice(0, numArr.length / 2);
  return numArr.reduce((acc, curr) => parseInt(acc) + parseInt(curr)) -
    halfArr.reduce((acc, curr) => parseInt(acc) + parseInt(curr)) ==
    0
    ? true
    : false;
}

I convert string to array and split half of the array. Then I calculate sum of each array by reduce method and see if the result is equal or not.

Here is highest point solution in JS:

function isLucky(n) {
  var count = 0;
  n = String(n)
    .split("")
    .map((t) => {
      return parseInt(t);
    });
  n.forEach((el, i) => {
    i < n.length / 2 ? (count += el) : (count -= el);
  });
  return count == 0;
}

What I learn here:

  • This solution kinda similar me on first step, but it's awesome when only use 1 loop to compare the sum of each half array.
  • The way comparation is outstanding.

Challenge 9: sortByHeight

image.png

Here is my solution:

function sortByHeight(a) {
  const treeArr = [];
  for (let i = a.length - 1; i >= 0; i--) {
    if (a[i] == -1) {
      treeArr.push(i);
      a.splice(i, 1);
    }
  }
  a.sort((x, y) => x - y > 0);
  treeArr
    .slice()
    .reverse()
    .forEach((treeIndex) => {
      a.splice(treeIndex, 0, -1);
    });
  return a;
}

This challenge kinda simple. The hard part here is about how we notice when add or remove item when loop through array because it will affect index. And what we need is add and remove on specific index. So if we not carefully notice it, we will add item to array in wrong position we expect.

Here is highest point solution in JS:

function sortByHeight(a) {
  var s = a.filter((h) => h > 0).sort((a, b) => a - b);
  return a.map((p) => {
    if (p !== -1) {
      return s.shift();
    }

    return -1;
  });
}

What I learn here:

  • Amazing way to interact with array. The author of this solution separate origin array in two parts: -1 and others number.
  • The author after got the right orders of the number. He/she didnt care about origin array numbers anymore. Just loop through it again, if it is number, use the number in the sorted array, if it is -1, let it be in the origin position.

Okay. I done for today. I hope tomorrow will be better. Let's go.

#inanotherlife