代码审查,帮助改进此代码以避免重复

时间:2018-02-17 22:11:39

标签: javascript function optimization sudoku

我正在处理数独谜题,以下代码可以正常工作,但很容易看出有很多重复的代码。我该如何优化它?感谢

问题:getSection:这个函数应该接受三个参数:一个数独网格,以及一个拼图3x3子网格的x和y坐标。该函数应该返回一个数组,其中包含指定子网格中的所有数字。

输入示例:

var puzzle = [[ 8,9,5,   7,4,2,   1,3,6 ],
             [ 2,7,1,   9,6,3,   4,8,5 ],
             [ 4,6,3,   5,8,1,   7,9,2 ],

             [ 9,3,4,   6,1,7,   2,5,8 ],
             [ 5,1,7,   2,3,8,   9,6,4 ],
             [ 6,8,2,   4,5,9,   3,7,1 ],

             [ 1,5,9,   8,7,4,   6,2,3 ],
             [ 7,4,6,   3,2,5,   8,1,9 ],
             [ 3,2,8,   1,9,6,   5,4,7 ]];

输出:

getSection(puzzle, 0, 0);
// -> [ 8,9,5,2,7,1,4,6,3 ]

解决方案:

 function getSection(arr, x, y) {
    var section = [];

    if (y === 0) {
        arr = arr.slice(0, 3);
        if (x === 0) {
            arr.forEach(function (element) {
                section.push(element.slice(0, 3));
            })
        } else if (x === 1) {
            arr.forEach(function (element) {
                section.push(element.slice(3, 6));
            })
        } else {
            arr.forEach(function (element) {
                section.push(element.slice(6, 9));
            })
        }
    }


    if (y === 1) {
        arr = arr.slice(4, 7);
        if (x === 0) {
            arr.forEach(function (element) {
                section.push(element.slice(0, 3));
            })
        } else if (x === 1) {
            arr.forEach(function (element) {
                section.push(element.slice(3, 6));
            })
        } else {
            arr.forEach(function (element) {
                section.push(element.slice(6, 9));
            })
        }
    }

    if (y === 2) {
        arr = arr.slice(6, 9);
        if (x === 0) {
            arr.forEach(function (element) {
                section.push(element.slice(0, 3));
            })
        } else if (x === 1) {
            arr.forEach(function (element) {
                section.push(element.slice(3, 6));
            })
        } else {
            arr.forEach(function (element) {
                section.push(elemet.slice(6, 9));
            })
        }
    }
    var subgrid = section.reduce(function (a, b) {
        return a.concat(b);
    },
        []
    );
    return subgrid;

}

console.log(getSection(puzzle, 0, 0));
// // -> [ 8,9,5,2,7,1,4,6,3 ]

console.log(getSection(puzzle, 1, 0));
// -> [ 7,4,2,9,6,3,5,8,1 ]

3 个答案:

答案 0 :(得分:1)

这是我使用ES6的看法

const puzzle = [
  [8, 9, 5, 7, 4, 2, 1, 3, 6],
  [2, 7, 1, 9, 6, 3, 4, 8, 5],
  [4, 6, 3, 5, 8, 1, 7, 9, 2],

  [9, 3, 4, 6, 1, 7, 2, 5, 8],
  [5, 1, 7, 2, 3, 8, 9, 6, 4],
  [6, 8, 2, 4, 5, 9, 3, 7, 1],

  [1, 5, 9, 8, 7, 4, 6, 2, 3],
  [7, 4, 6, 3, 2, 5, 8, 1, 9],
  [3, 2, 8, 1, 9, 6, 5, 4, 7]
];

const GRID_SIZE = 3;

function getOffset(coordinate) {
  const start = coordinate * GRID_SIZE;
  const end = start + GRID_SIZE;
  return [start, end];
}


function getSection(arr, x, y) {
  const yOffset = getOffset(y);
  const xOffset = getOffset(x);

  const elements = arr.slice(...yOffset);
  return elements
    .map(element => element.slice(...xOffset))
    .reduce((subgrid, grid) => [...subgrid, ...grid], []);
}

console.log(getSection(puzzle, 0, 0));
// // -> [ 8,9,5,2,7,1,4,6,3 ]

console.log(getSection(puzzle, 1, 0));
// -> [ 7,4,2,9,6,3,5,8,1 ]

答案 1 :(得分:0)

不像@nutboltu那么优雅,但几乎一样简洁。

cwise

答案 2 :(得分:0)

我认为您的xy不会超过您的数组长度。这是实现解决方案的最简单方法。

function getSection(arr, x, y) {
  var GRID_SIZE = 3;
  var indexX = x*GRID_SIZE;
  var indexY = y*GRID_SIZE;
  var results = [];
  for(var i = indexY; i< indexY+GRID_SIZE; i++){
    results = results.concat(puzzle[i].slice(indexX, indexX+GRID_SIZE));
  }
  return results;
}

console.log(getSection(puzzle, 0, 0));
// // -> [ 8,9,5,2,7,1,4,6,3 ]

console.log(getSection(puzzle, 1, 0));
// -> [ 7,4,2,9,6,3,5,8,1 ]