从 [x,y] 坐标中删除重复项和镜像
Remove duplicates and mirror from [x,y] coordinates
设 x=[1,2,6,3,5,5 ,5,4,4];
设 y=[3,4,3,5,2,4 ,4,2,6];
expected_x=[1,2,6,3,5,5,4]
expected_y=[3,4,3,5,2,4,6]
将 x 和 y 视为坐标。[1,3] 将是第一个点,[4,6] 将是最后一个点。
如果 [X,Y] 有重复,则预期输出中只会显示 [X,Y] 之一(无重复)。并且如果存在像 [X,Y] 这样的镜像,它是 [Y,X] 的镜像,两者在同一索引处。
这是我为 一个数组 编写的代码,用于使数组唯一。但是,我不确定如何将它与代表 x 和 y 坐标的 2 个单独数组一起使用。任何帮助将不胜感激:)
let chars = ['A', 'B', 'A', 'C', 'B'];
let uniqueChars = [...new Set(chars)];
console.log(uniqueChars);
使用这个:
let x=[1,2,6,3,5,5,5,4,4];
let y=[3,4,3,5,2,4,4,2,6];
const coordinates = [];
let i = -1;
while ( x[++i] ) {
const c = {
index: i,
value: [x[i], y[i]]
}
coordinates.push(c);
}
const coordArray = coordinates.reduce((p, next) => {
if (!p.values.includes(JSON.stringify(next.value)) && !p.values.includes(JSON.stringify([...next.value].reverse()))) {
p.values.push(JSON.stringify(next.value));
p.indexes.push(next.index);
}
return p;
},{
indexes: [],
values: []
})
coordArray.values = coordArray.values.map(JSON.parse)
console.log(coordArray)
您可以使用 for loop
并将两个数组迭代在一起,因为它们彼此具有相同的长度(作为 x,y 对)。
您还可以保留副本和镜像的“历史记录”。然后您在迭代时需要做的就是检查历史记录。如果没有匹配,则将当前追加到结果数组,然后更新历史。
let x=[1,2,6,3,5,5,5,4,4];
let y=[3,4,3,5,2,4,4,2,6];
let h=[]; // history
let rx = []; // result x
let ry = []; // result y
for (let i = 0; i < x.length && i < y.length; i++) {
// The if line (with include()) would be nice if it worked, but it didn't because of
// always returning false.
// Instead I will have to manually search.
// if (h.includes([x[i], y[i]]) || h.includes([y[i], x[i]])) {
let found = false;
for (let s = 0; s < h.length; s++) {
// check for duplicate
if (h[s][0] == x[i] && h[s][1] == y[i]) {
found = true;
break;
}
// check for mirror
if (h[s][0] == y[i] && h[s][1] == x[i]) {
found = true;
break;
}
}
if (found) {
// do nothing, its a duplicate or mirror
console.log("duplicate or mirror detected on index " + i);
}
else {
// update results
rx.push(x[i]);
ry.push(y[i]);
// update history
h.push([ x[i], y[i] ]);
}
}
console.log("rx: " + rx);
console.log("ry: " + ry);
简而言之,.include()
会很好,但显然引用数组打破了我的预期逻辑。我不知道。但是上面通过对“历史”的字面搜索将这些问题分开,这将改变“找到的”布尔值以了解是否存在重复项或镜像。
显然,这段代码可以缩短到 10 或 7 行以下,但我想继续研究它,因为它很有趣,而且所使用的方法演示了如何使用常规 for
循环来解决此类问题“迭代”问题。
希望对您有所帮助。
设 x=[1,2,6,3,5,5 ,5,4,4];
设 y=[3,4,3,5,2,4 ,4,2,6];
expected_x=[1,2,6,3,5,5,4]
expected_y=[3,4,3,5,2,4,6]
将 x 和 y 视为坐标。[1,3] 将是第一个点,[4,6] 将是最后一个点。
如果 [X,Y] 有重复,则预期输出中只会显示 [X,Y] 之一(无重复)。并且如果存在像 [X,Y] 这样的镜像,它是 [Y,X] 的镜像,两者在同一索引处。
这是我为 一个数组 编写的代码,用于使数组唯一。但是,我不确定如何将它与代表 x 和 y 坐标的 2 个单独数组一起使用。任何帮助将不胜感激:)
let chars = ['A', 'B', 'A', 'C', 'B'];
let uniqueChars = [...new Set(chars)];
console.log(uniqueChars);
使用这个:
let x=[1,2,6,3,5,5,5,4,4];
let y=[3,4,3,5,2,4,4,2,6];
const coordinates = [];
let i = -1;
while ( x[++i] ) {
const c = {
index: i,
value: [x[i], y[i]]
}
coordinates.push(c);
}
const coordArray = coordinates.reduce((p, next) => {
if (!p.values.includes(JSON.stringify(next.value)) && !p.values.includes(JSON.stringify([...next.value].reverse()))) {
p.values.push(JSON.stringify(next.value));
p.indexes.push(next.index);
}
return p;
},{
indexes: [],
values: []
})
coordArray.values = coordArray.values.map(JSON.parse)
console.log(coordArray)
您可以使用 for loop
并将两个数组迭代在一起,因为它们彼此具有相同的长度(作为 x,y 对)。
您还可以保留副本和镜像的“历史记录”。然后您在迭代时需要做的就是检查历史记录。如果没有匹配,则将当前追加到结果数组,然后更新历史。
let x=[1,2,6,3,5,5,5,4,4];
let y=[3,4,3,5,2,4,4,2,6];
let h=[]; // history
let rx = []; // result x
let ry = []; // result y
for (let i = 0; i < x.length && i < y.length; i++) {
// The if line (with include()) would be nice if it worked, but it didn't because of
// always returning false.
// Instead I will have to manually search.
// if (h.includes([x[i], y[i]]) || h.includes([y[i], x[i]])) {
let found = false;
for (let s = 0; s < h.length; s++) {
// check for duplicate
if (h[s][0] == x[i] && h[s][1] == y[i]) {
found = true;
break;
}
// check for mirror
if (h[s][0] == y[i] && h[s][1] == x[i]) {
found = true;
break;
}
}
if (found) {
// do nothing, its a duplicate or mirror
console.log("duplicate or mirror detected on index " + i);
}
else {
// update results
rx.push(x[i]);
ry.push(y[i]);
// update history
h.push([ x[i], y[i] ]);
}
}
console.log("rx: " + rx);
console.log("ry: " + ry);
简而言之,.include()
会很好,但显然引用数组打破了我的预期逻辑。我不知道。但是上面通过对“历史”的字面搜索将这些问题分开,这将改变“找到的”布尔值以了解是否存在重复项或镜像。
显然,这段代码可以缩短到 10 或 7 行以下,但我想继续研究它,因为它很有趣,而且所使用的方法演示了如何使用常规 for
循环来解决此类问题“迭代”问题。
希望对您有所帮助。