Day01: Getting Started (In another life)

Day01: Getting Started (In another life)

In life, sometimes we regret about our own decisions. We'd think if we had to start over, we would do something better.

Hi, I'm Khanh, a junior software engineer in VietNam. I am not a good developer at work. I have a lot of problems in coding. I use more time than others to solve problems and complete tasks.

I thought I would leave this job. But I have never regretted choosing it, it gives me so many things. Thanks for everything.

But I don't want this situation last anymore. I need to try harder, at least, to be able to do my job well. So I decided to begin this series of days.

Every day I will spend 2 hours after work to learn from the beginning. From the most basic lines of code. Set my own milestone and what I want to accomplish. That's all.

MILESTONE 1: Complete Arcade Intro CodeSignal

Algorithm is the first thing I want to practices. I want to be good at algorithms. And after surfing around websites, I want to start practicing with CodeSignal. I used to do a few code challenges here and leave it. But not this time. Let's go.

Disclaimer: I will write post after I finish the day. This is like a note for what I learn in that day.

Day01:

I solve 4 code challenges in arcade. (And use 2.5hrs, not 2hrs as plan. So shame) Here is what I learn:

Challenge 1: adjacentElementsProduct

image.png

Here is my solution:

function adjacentElementsProduct(inputArray) {
  let max = inputArray[0] * inputArray[1];
  inputArray.reduce((acc, curr) => {
    if (acc * curr > max) {
      max = acc * curr;
    }
    return curr;
  });
  return max;
}

I use reduce method in array for loop through it to multiple element and find the max of it.

Here is highest point solution in JS:

function adjacentElementsProduct(arr) {
  return Math.max(...arr.slice(1).map((x, i) => [x * arr[i]]));
}

What I learn here:

  • They convert origin array to adjacent element product array. Then they utilize the power of Math.max() to find max element of it.

  • They teach me they way slice first element before map. It is very cool.

Challenge 2: shapeArea

image.png

Here is my solution:

function shapeArea(n) {
  let area = 1;
  for (let i = 0; i < n; i++) {
    area += 4 * i;
  }
  return area;
}

I try to find the rule between of area with the number. And I just found the rule plus number like this:

  • shapeArea(1) : 1
  • shapeArea(2) : 1 + 4 x 1
  • shapeArea(3) : 1 + 4 x 1 + 4 x 2

Here is highest point solution in JS:

function shapeArea(n) {
  return n*n + (n-1)*(n-1);
}

What I learn here:

  • They find the formula in math the relationship between n and the area.

Challenge 3: makeArrayConsecutive2

image.png

Here is my solution:

function makeArrayConsecutive2(statues) {
  let total = 0;
  statues
    .sort((a, b) => a - b)
    .reduce((acc, curr) => {
      if (curr - acc != 1) {
        total = total + (curr - acc - 1);
      }
      return curr;
    });
  return total;
}

I try to sort array first. Then loop through it and try to find the gap between each element. Then sum it up to get the solution.

Here is highest point solution in JS:

function makeArrayConsecutive2(sequence) {
  return Math.max(...sequence) - Math.min(...sequence) + 1 - sequence.length;
}

What I learn here:

  • They find total number should be in array by formula of math. And minus to real array length. This is so genius.

Challenge 4: almostIncreasingSequence

image.png

Here is my solution:

function almostIncreasingSequence(sequence) {
  let total = 0;
  let removeIndex = [];

  let rsTest1 = true,
    rsTest2 = true;
  let total1 = 0,
    total2 = 0;
  for (let i = 1; i < sequence.length; i++) {
    if (sequence[i] - sequence[i - 1] <= 0) {
      total += 1;
    }
    if (total == 1) {
      removeIndex.push(i);
      removeIndex.push(sequence.indexOf(Math.max(...sequence.slice(0, i))));
      break;
    }
  }

  const sequence1 = [...sequence];
  const sequence2 = [...sequence];

  sequence1.splice(removeIndex[0], 1);
  sequence2.splice(removeIndex[1], 1);
  // Sequence 1 Test
  for (let i = 1; i < sequence1.length; i++) {
    if (sequence1[i] - sequence1[i - 1] <= 0) {
      total1 += 1;
    }
    if (total1 == 1) {
      rsTest1 = false;
    }
  }

  // Sequence 2 Test
  for (let i = 1; i < sequence2.length; i++) {
    if (sequence2[i] - sequence2[i - 1] <= 0) {
      total2 += 1;
    }
    if (total2 == 1) {
      rsTest2 = false;
    }
  }
  return rsTest1 || rsTest2;
}

I use horrible solution for this. Try to loop through array three times. But I cannot find other things to solve. And I stuck in this challenge more than 1hr.

Here is highest point solution in JS:

function almostIncreasingSequence(seq) {
  var bad=0
  for(var i=1;i<seq.length;i++) if(seq[i]<=seq[i-1]) {
    bad++
    if(bad>1) return false
    if(seq[i]<=seq[i-2]&&seq[i+1]<=seq[i-1]) return false
  }
  return true
}

What I learn here:

  • They find out another way to test array without remove element reloop through it.

So I done for today. I hope tomorrow will be better.

#inanotherlife