Blog Day 45: findBugInApples, superIncreasing

Inseo Park·2021년 8월 25일
0

Path to Data Scientist

목록 보기
43/58
post-thumbnail

1. TIL (Today I Learned)

findBugInApples

Take a two-dimensional array (an array with an array as an element) as input and return an array with the location information of 'B' as an element.

function findBugInApples(arr) {
  let result = [];
  for (i = 0;  i < arr.length; i++) {
    for (j = 0; j < arr[i].length; j++) {
      if (arr[i][j] === 'B') {
        result.push(i, j)
      }
    }
  }
  return result;
}

I had some left over time so gave myself another algorithm question challenge which was part of my assignment from the bootcamp.

superIncreasing

The requirement was to take an array of elements as input and return whether each element is greater than the sum of the elements before it.

My current 'failed' code is as follows:

function superIncreasing(arr) {
let temp = arr[0]
let result;
for (i = 2; i < arr.length; i++) {
  for (j = 1; j < i; j++) {
    temp = temp + arr[i-j] 
  }
  if (arr[i] > temp) {
    result = true
  } else if (arr[i] <= temp) {
    result = false
  }
}
return result;
};

This code failed 2 test out of 5 test which basically means some part of the algorithm I came up with fails to cover some part of the requirement. The failed part is when an array with only 2 index is given to go through the function. The current code I wrote starts from temp to equal the 0th index of the array. Then as the loop goes the temp adds the indexes before the arr[i] which is the current index that is used to compare with the sum of the previous index of the array which is 'temp.'
Tomorrow I will give it a try to maybe pull down the temp part of the code to go below the comparison conditional statements.

2. 3 Things to be Thankful for

  1. Achieving a good study time.
  2. Thank God for keeping me humble.
  3. Thank God for blessing our family.

3. Ideas and Things to think about.

  1. When you feel over stressed give yourself a break time.
profile
my journey to become a data scientist.

0개의 댓글