2016年4月19日 星期二

I am such a bad developer...

Since I arrived U.S., I started my journey of intern finding. After sending a bunch (approximately 30 ~ 40) of emails, résumés, cover letters and application, I received a few app test for me to prove that I deserve it.

This story starts with an app test that I have completed recently. I was told to complete a question on codewars.com. The problem is not that hard actually. Because there are a few different ways to complete it. However, I choose the functional-style to complete it. I dunno why I would do that in such functional style as the problem was asked on Javascript. After 60 minutes of working, I completed it in the last minute.

Here is the question:




The interesting thing about codewars is that I can look at others' solutions so that I can "learn" from them. I will post two answers for this problem underneath and see if you can guess which one is mine ;).

First

function maxSumPath(a) {
  // Your code here
//   console.log(a) 
  var sumList = getSumList(a,
    {
      max: {
        path: '',
        sum: Number.MIN_VALUE
      }
    }, '', 0, 0, 0);
//   console.log(sumList)

  return sumList.max
}

var getSumList = function(inputList, sumList, path, i, j, sum) {
  sum += inputList[i][j]
  // travse the arrays
  if (inputList && i < inputList.length - 1) {
    sumList = mergeMaxObjects(sumList, getSumList(inputList, sumList, path + '↓', i + 1, j, sum))
  }

  if (inputList[0] && j < inputList[0].length - 1) {
    sumList = mergeMaxObjects(sumList, getSumList(inputList, sumList, path + '→', i, j + 1, sum))
  }
  // end condition
  if ((i == inputList.length - 1) && (j == inputList[0].length - 1)) {
    // reach the end, return the object

    // check exist path
    sumList = setMaxProperty(sumList, path, sum)
    // compare the max path
    if (sumList.max.sum < sumList[path]) {
      sumList.max.path = path
      sumList.max.sum = sum
    }

    return sumList
  } else {
    // not the end, return the original list
    return sumList
  }
}

var setMaxProperty = function (obj, key, value) {
  if (typeof obj[key] !== 'undefined') {
    // exist a record
    if (obj[key] < value) {
      // set if sum is greater than the record
      obj[key] = value
    }
  } else {
    obj[key] = value
  }
  return obj
}

var mergeMaxObjects = function (obj1, obj2) {
  for (var path in obj2) {
    obj1 = setMaxProperty(obj1, path, obj2[path])
  }
  return obj1

}

Second

function maxSumPath(a){
    var p='',s=0,r=0,c=0,t=a.map(x=>x.slice()).slice();
    for(var i=0;i<t.length;i++)
        for(var j=0;j<t[i].length;j++)
            t[i][j]+=Math.max((t[i+1]||[])[j]||0,(t[i]||[])[j+1]||0);
        while(r<a.length&&c<a[r].length){
            s+=a[r][c];
            if(((t[r+1]||[])[c]||0)<((t[r]||[])[c+1]||0))
                [p,c]=[p+'→',c+1];
            else if(((t[r+1]||[])[c]||0)>((t[r]||[])[c+1]||0))
                [p,r]=[p+'↓',r+1];
            else break;
        }
    return {path:p,sum:s};
}



How's the guess @@?



Yes, the first one. Such long and unclear coding compared to the second one which is small and clean. (But I think it is a bit de-human. I thought it was the result of minifying.) Anyway, you can see how bad style I code......

This story can tell that I may be not that ready for an internship here. So, I should head back my home and find an internship for my next summer (?).




P.S. hope that I can at least get an interview!!!

沒有留言:

張貼留言