Codewars-Javascript训练手册:数组(上)

Directions Reduction

题目描述(Description):

write a function dirReduc which will take an array of strings and returns an array of strings with the needless directions removed (W<->E or S<->N side by side).
In [“NORTH”, “EAST”, “WEST”, “SOUTH”, “WEST”, “WEST”], “NORTH” and “SOUTH” are not directly opposite but they become directly opposite after the reduction of “EAST” and “WEST” so the whole path is reducible to [“WEST”, “WEST”].
只有相邻以及抵消后相邻的的两个相反方向可以抵消

题目注解(Note):

All paths can’t be made simpler. The path [“NORTH”, “WEST”, “SOUTH”, “EAST”] is not reducible. “NORTH” and “WEST”, “WEST” and “SOUTH”, “SOUTH” and “EAST” are not directly opposite of each other and can’t become such. Hence the result path is itself : [“NORTH”, “WEST”, “SOUTH”, “EAST”].

Solution:

某论坛大神的解决方案

function dirReduc(directions) {
//判断是否为数组,不为空
        if (!directions || !directions instanceof Array)
                return directions;

        var relations = {'WEST': 'EAST', 'EAST': 'WEST', 'SOUTH': 'NORTH', 'NORTH': 'SOUTH'};

        for (var i = 0, length = directions.length, expect; i < length; i++) {
                if (directions[i] === expect) {
                        directions.splice(i-1, 2);       
                        i -= 2; length -= 2;
                }
//得到本次循环数组值的相反方向值
                expect =  relations[directions[i]];
        }

        return directions;

直达地址

Codewars排名最高的答案:

function dirReduc(plan) {
  var opposite = {
    'NORTH': 'SOUTH', 'EAST': 'WEST', 'SOUTH': 'NORTH', 'WEST': 'EAST'};
  return plan.reduce(function(dirs, dir){
      if (dirs[dirs.length - 1] === opposite[dir])
        dirs.pop();
      else
        dirs.push(dir);
      return dirs;
    }, []);
}

知识点:

splice() 方法:向/从数组中添加/删除项目,然后返回被删除的项目(新的数组)。

注释:该方法会改变原始数组。
语法

arrayObject.splice(index,howmany,item1,…..,itemX)

splice() 方法与 slice() 方法的作用是不同的,splice() 方法会直接对数组进行修改。参考链接
reduce (Array) (JavaScript)方法:对数组中的所有元素调用指定的回调函数。该回调函数的返回值为累积结果,并且此返回值在下一次调用该回调函数时作为参数提供。
语法

array1.reduce(callbackfn[, initialValue])

返回值为通过最后一次调用回调函数获得的累积结果。参考链接

Tribonacci Sequence(类斐波那契数列)

题目描述

create a fibonacci function that given a signature array/list, returns the first n elements - signature included of the so seeded sequence.
Signature will always contain 3 numbers; n will always be a non-negative number; if n==0, then return an empty array and be ready for anything else which is not clearly specified ;)
数组Signature 总包含3个数值,n为非负整数,当n=0的时候,返回一个空数组。

Soluation:

function tribonacci(signature,n){
  //判断n<=3的情况,即使数组signature有3个数值,但n为0时也只能返回空数组,用arr.slice();
  if(n<=3){
    return signature.slice(0,n);
  }else{
  //累加前三项值,并将其push到数组
    for(var i=3;i<n;i++){
      signature.push(signature[i-3]+signature[i-2]+signature[i-1]);
    }
     return signature;
  }

}

Codewars 得票最高的答案:

function tribonacci(signature,n){  
  for (var i = 0; i < n-3; i++) { // iterate n times
    signature.push(signature[i] + signature[i+1] + signature[i+2]); // add last 3 array items and push to trib
  }
  //优化算法,去掉了if语句。
  return signature.slice(0, n); //return trib - length of n
}

这就是与高手之间的差距啊。

原文地址:https://www.cnblogs.com/xihe/p/6138619.html