编程到2D阵列复杂对齐 [英] Program to 2D Array complex alignment

查看:99
本文介绍了编程到2D阵列复杂对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定2D数组,其中有2组数组,第一组未正确对齐,第1组中有空格,第1组应与第2组对齐。 2组的长度相同。但是在第一组中有空列直到第二组开始。

Given 2D Array, where there are 2 sets of array the first set is not aligned properly, there is a space in 1st set, the 1st set should be aligned with second set. Length of the 2sets are same. But in the first set there are empty column comes till the starting of 2nd set.

我们需要编写一个算法来删除第一组中不必要的空格。在给定的示例空间来自第1列到第8行,我们应该根据条件删除,如果有更多的空白。约束是我们不应该删除,如果只有一个空格。

We need to write an algorithm which will remove the unnecessary spaces in 1st set. In the given example space came in 1st column till 8th row, we should remove based on condition that if there are more blanks. Constraint is we should not remove if only one space is there.

输入

[[,1,,2,,3,,4,],
 [,1,,2,,3,,4,],
 [,1,,2,,3,,4,],
 [,1,,2,,3,,4,],
 [,1,,2,,3,,4,],
 [,1,,2,,3,,4,],
 [,1,,2,,3,,4,],
 [,1,,2,,3,,4,],


 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,]
]

输出数组

[

 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,]


 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,],
 [1,2,3,4,,,,,]

]




  1. 只有第一套才会出现对齐问题,所有剩余的集合没有对齐问题。我们不应该更改剩余的集合。

  2. 首先我们需要找出设置信息。

  3. 如果每列中都有一列为空然后我们需要删除该集。

  4. 我们不应该根据一个空格来决定集合,而是基于整列中10%的空格。

  5. 我们不应该删除所有空格,只有当整列中有10%以上的空间时才应删除。

  6. 数据不是数字而是字符串。空格只是空字符串()

  1. There will be alignment issue only with 1st set, all the remaining sets are having no alignment issues. We should not change the remaining sets.
  2. First we need to find out set information.
  3. If there is a column which is empty in every set then we need to remove that set.
  4. We should not decide the set based on one space, rather based on 10% of the spaces in entire column.
  5. We should not remove all the spaces, we should remove only if there are more 10% of the space in entire column.
  6. The data is not in numbers but strings. Empty spaces are nothing but empty strings ("")

我编写了不同的算法,但似乎没有任何工作。

I have wrote different algorithms but nothing seems to be working.

一种算法是发送格式数组和输入数组。但这并没有解决问题。

One algorithm is to send format array and input array. But this is not solving the problem.

  alignArray(arr1, arr2) {
    let arr3 = [];
    let arr4 = _.compact(arr2);
    let count = 0;
    _.map(arr1, function (num, index) {
      if (_.isString(num) && !(_.isEmpty(num))) {
        if (arr4[count])
          arr3.push(arr4[count]);
        else
          arr3.push("");
        count++;
      } else {
        arr3.push("");
      }
    });
    return arr3;
  }

EDGE案例:


  1. 应该有一些类似于第一套的套装。

  1. There should be some sets which are similar to 1st set.

[[,1,,2,3, ,4,],
[,1,,2,3,,4,],
[,1,,2,3,,4,],
[,1 ,, 2,,3,4,],
[,1,,2,3,,4,],
[,1,,2,3,,4,],
[,1,,2,3,,4,],
[,1,,2,,3,],

[[,1,,2,,3,,4,], [,1,,2,,3,,4,], [,1,,2,,3,,4,], [,1,,2,,3,,4,], [,1,,2,,3,,4,], [,1,,2,,3,,4,], [,1,,2,,3,,4,], [,1,,2,,3,,4,],

[1,2,3,4 ,,,,,],
[1,2,3,4 ,,,,,],
[1,2,3,4, ,,,,],
[1,2,3,4 ,,,,,],
[1,2,3,4 ,,,,,],
[1 ,2,3,4 ,,,,,],
[1,2,3,4 ,,,,,],
[1,2,3,4 ,,,,,] ,
[1,2,3,4 ,,,,,],

[1,2,3,4,,,,,], [1,2,3,4,,,,,], [1,2,3,4,,,,,], [1,2,3,4,,,,,], [1,2,3,4,,,,,], [1,2,3,4,,,,,], [1,2,3,4,,,,,], [1,2,3,4,,,,,], [1,2,3,4,,,,,],

[,1,,2,3,,4,],
[,1,,2,3,,4,],
[,1,,2,3,,4,],
[,1,,2 ,, 3,,4,],
[,1,,2,3,,4,],
[,1,,2,3,,4,],
[ ,1,2,,3,4,],
[,1,,2,,3,],
]

[,1,,2,,3,,4,], [,1,,2,,3,,4,], [,1,,2,,3,,4,], [,1,,2,,3,,4,], [,1,,2,,3,,4,], [,1,,2,,3,,4,], [,1,,2,,3,,4,], [,1,,2,,3,,4,] ]


推荐答案

您可以先获取模式,为模式构建过滤模式,并通过过滤模式过滤值来映射数组。

You could get the pattern first, build a filter pattern for the pattern and map the array by filtering the values with the filter pattern.


例如这个ar ray包含三个不同的字符串,字符串的模式是splitted字符串的truthy值,其中一个或零表示falsy(缺少值)。

For example this array contains three different strings and the pattern of the strings are the truthy values of the splitted string with one or zero for falsy (missing values).

',1,,2,,3,x,4,' // 010101010
',1,,2,,3,,4,'  // 010101010
'1,2,3,4,,,,,'  // 111100000

获取带有模式的对象后并计算这些模式,通过查看模式计数和总计数之间的关系是否大于 0.1 来添加过滤模式,然后整个模式用于过滤或通过添加列式truthy值来构建新的过滤器模式。检查此总和,如果与总计的关系大于 0.1 ,则此列包含在过滤器模式中。

After getting an object with the pattern and count of these patterns, a filtering pattern is added by looking if the relation between pattern count and total count is greater than 0.1, then the whole pattern is taken for filtering or a new filter pattern is build by adding columnwise truthy values. This sum is checked and if the relation to total is greater than 0.1, this column is included in the filter pattern.

对象的结果如下所示:

{
    111100000: {
        count: 9,
        filter: "111100000"
    },
    "010101110": {
        count: 1,
        filter: "010101010"
    },
    "010101010": {
        count: 8,
        filter: "010101010"
    }
}

结果,所有列都被过滤器过滤数组模式的模式。

As result, all columns are filtered by the filter pattern of the pattern of the array.

const
    getPattern = a => a.map(v => v ? 1 : 0).join('');

var csv = [',1,,2,,3,x,4,', ',1,,2,,3,,4,', ',1,,2,,3,,4,', ',1,,2,,3,,4,', ',1,,2,,3,,4,', ',1,,2,,3,,4,', ',1,,2,,3,,4,', ',1,,2,,3,,4,', ',1,,2,,3,,4,', '1,2,3,4,,,,,', '1,2,3,4,,,,,', '1,2,3,4,,,,,', '1,2,3,4,,,,,', '1,2,3,4,,,,,', '1,2,3,4,,,,,', '1,2,3,4,,,,,', '1,2,3,4,,,,,', '1,2,3,4,,,,,'],
    arrays = csv.map(s => s.split(',')),
    total = 0,
    columns = arrays.reduce((r, a) => {
        var p = getPattern(a);
        r[p] = r[p] || { count: 0 };
        r[p].count++;
        total++;
        return r;
    }, {}),
    patterns = Object.keys(columns),
    result;

patterns.forEach(p => {
    if (columns[p].count / total >= .1) {
        columns[p].filter = p;
        return;
    }
    columns[p].filter = Array
        .from(p, (v, i) => patterns.reduce((t, p) => t + v * p[i] * columns[p].count, 0) / total >= .1 ? 1 : 0)
        .join('');
});

result = arrays.map(a => (f => a.filter((_, i) => +f[i]))(columns[getPattern(a)].filter));

console.log(result);
console.log(columns);

.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于编程到2D阵列复杂对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆