gpt4 book ai didi

javascript - 程序到 2D Array 复杂对齐

转载 作者:行者123 更新时间:2023-11-30 19:44:16 24 4
gpt4 key购买 nike

给定二维数组,其中有两组数组,第一组未正确对齐,第一组中有空格,第一组应与第二组对齐。 2套的长度相同。但是在第一组中,直到第二组开始之前都有空栏。

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

输入

[[,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. 只有第 1 组会有对齐问题,所有剩余的集合都没有对齐问题。我们不应该改变剩余的集合。
  2. 首先我们需要找出集合信息。
  3. 如果每个集合中都有一列为空,那么我们需要删除该集合。
  4. 我们不应该根据一个空间来决定集合,而是根据整个列中 10% 的空间来决定集合。
  5. 我们不应该删除所有空格,只有当整个列中的空格超过 10% 时,我们才应该删除。
  6. 数据不是数字而是字符串。空格只是空字符串 ("")

我写了不同的算法,但似乎没有任何效果。

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

  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;
}

边缘情况:

  1. 应该有一些与第一组相似的组。

    [[,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,]]

最佳答案

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

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

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.

The result of the object columns looks like this:

{
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; }

关于javascript - 程序到 2D Array 复杂对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55083788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com