查找数组

时间:2018-03-10 19:03:07

标签: arrays swift algorithm math

我想使用Swift应用某种特殊模式查找算法。

一些解释:

我得到一个简单的1维数组,看起来像这样:

var array = [ 
               "0000000000000000000",
               "0000001110000000000",
               "0000011111000000000",
               "0000001110000000000",
               "0000000000000000000",
               "0001100000000000000",
               "0001100000000011000",
               "0011100000000011000",
               "0000000000000000000"
            ]

我想提取“1”字符(连接组件)的连接区域。

看看这个:

       111
      11111
       111

 11
 11         11
111         11

我想得到一个包含单个组件的所有x / y位置的多维数组。

var result = [
                [ [6,1], [7,1], [8,1], [5,2], [6,2], [7,2], [8,2], [9,2], [6,3], [7,3], [8,2] ] // positions of the first area (the biggest one on top)

                [ [3,5], [4,5], [3,6], [4,6], [2,7], [3,7], [4,7] ] // area bottom left

                [ [14,6], [15,6], [14,7], [15,7] ] // area bottom right (smallest area)
             ]

我已经为javascript编写了这个函数。你可以在这里找到代码:

var matrix = [
  "0000000000000000000",
  "0000001110000000000",
  "0000011111000000000",
  "0000001110000000000",
  "0000000000000000000",
  "0001100000000000000",
  "0001100000000011000",
  "0011100000000011000",
  "0000000000000000000"
]


Array.prototype.extract_components_positions = function(offset) {
  var array = this.map(item => item.split('')).map(str => Array.from(str, Number)),
    default_value = 0,
    result_object = {}

  function test_connection(array, i, j) {
    if (array[i] && array[i][j] === -1) {
      if (!result_object[default_value]) result_object[default_value] = [];
      result_object[default_value].push([j, i]);
      array[i][j] = 1;
      for (var k = offset; k > 0; k--) {
        test_connection(array, i + k, j); // left - right
        test_connection(array, i, j + k); // top - bottom
        test_connection(array, i - k, j); // right - left
        test_connection(array, i, j - k); // bottom - top
      }
      return true
    }
  }
  array.forEach(function(a) {
    a.forEach(function(b, i, bb) {
      bb[i] = -b
    })
  });
  array.forEach(function(a, i, aa) {
    a.forEach(function(b, j, bb) {
      test_connection(aa, i, j) && default_value++
    })
  })
  return [result_object];
}



var result = matrix.extract_components_positions(1);
console.log(JSON.stringify(result))

但是我将这个Javascript代码翻译成Swift时遇到了很大问题!

 func extract_components_positions(matrix: [[String]],offset: Int) {
    var array = [[]]  // no idea how to use map to split the array from ["0011100"],... to ["0","0","1","1",...], ...
    var default_value = 0,
        result_object = [[Int]()]

    func testconnection(matrix: [[String]], i: Int, j: Int) -> [[Int]] {
        if (Int(array[i][j] as! Int) == -1) {
            array[i][j] = 1
            for var k in offset...0 {
                testconnection(matrix: array, i: i+k, j: j) // error: "Cannot convert value of type '[[Any]]' to expected argument type '[[String]]'"
                testconnection(matrix: array, i: i, j: j+k)
                testconnection(matrix: array, i: i-k, j: j)
                testconnection(matrix: array, i: i, j: j-k)
            }
        }
    }
    array.forEach { (a) in
        a.forEach({ (b, i, bb) in // error: "Contextual closure type '(Any) -> Void' expects 1 argument, but 3 were used in closure body"
            bb[i] = -b
        })
    }
    array.forEach { (a, i, aa) in // error: "Contextual closure type '([Any]) -> Void' expects 1 argument, but 3 were used in closure body"
        a.forEach({ (b, j, bb) in
            testconnection(aa, i, j) && default_value++
        })
    }
    return result_object
}

非常感谢任何有关如何修复我的代码的帮助。

1 个答案:

答案 0 :(得分:2)

看起来你正在玩扫雷!这是我的解决方案(在swift 4.0中,Xcode 9.2)。请参阅内联注释以获取解释。

-------------------
------111----------
-----11111---------
------111----------
-------------------
---22--------------
---22---------33---
--222---------33---
-------------------

结果:

flatMap

请注意,在Swift 4.1(目前处于测试版)中,我们在此处使用的compactMap已重命名为flatMap。这并不是说flatMap完全消失了。 compactMap有3个版本,其中只有1个已重命名为from statsmodels.multivariate.pca import PCA pc = PCA(data=A,ncomp=1, missing='fill-em') A=pc._adjusted_data 。有关详细信息,请参阅SE-0187