比较两个列表中的元素
Compare elements in two lists
我有两个包含整数的列表,如下所示:
def first = [10, 12, 3, 6, 9, 8, 33]
def second = [8, 9, 5, 6]
每个列表中的元素数量完全可以是任意的。我还有一个门槛,也是一个数字:
def threshold = 3
我必须成对检查比较两个数组中的所有元素,并检查它们的差异是否 <= 阈值。结果我必须输出所有这些元素。
所以,在我的例子中是:
10, 12, 3, 6, 9, 8
来自第一个列表,8, 9, 5, 6
来自第二个列表。因为
abs(10-8) <= 3
abs(12-9) <= 3
abs(3-5) <= 3
abs(6-6) <= 3
这里由于第一个列表比第二个列表包含更多元素,我必须将第一个列表元素与第二个列表中的最后一个元素进行比较。
abs(9-6) <= 3
abs(8-6) <= 3
abs(33-6) >= 3, stop here!
Groovy 和 Java 答案是合适的。
P.S这是一个算法问题吗?一些算法已经存在?
这是完成任务的java 方法。既然你 abs()
减法,那么哪个先到并不重要。因此,让我们利用它并找出哪个更长。一旦我们知道了这一点,我们就知道要强制哪个元素重复它的最后一个元素。
if (first.length() > second.length()) {
longer = first;
shorter = second;
} else {
longer = second;
shorter = first;
}
last = shorter.length() - 1;
for(int i = 0; i < longer.length(); i++) {
s = shorter.get(Math.min(last, i));
l = longer.get(i);
if (abs(l-s) > threshold) {
break;
}
}
在下面的实现中我使用了set数据结构来防止结果中的元素重复,如果你想显示重复的元素你可以使用arraylist而不是set
import java.util.*;
public class CompareElements{
public static void main(String[ ] arg){
int [] firstList = {10, 12, 3, 6, 9, 8, 33};
int [] secondList = {8, 9, 5, 6};
int firstListLength = firstList.length;
int secondListLength = secondList.length;
// i have used set data structure to prevent duplication of elements in the result
Set<Integer>result=new HashSet<Integer>();
// iterate over the two list and get the absolute value for each two corresponding elements
// and check if the difference is <= 3 , the two elements are added to the result
for(int i=0;i<Math.min(firstList.length, secondList.length);i++) {
if(Math.abs(firstList[i]-secondList[i]) <= 3)
{
result.add(firstList[i]);
result.add(secondList[i]);
}
}
// here we are trying to handle the case when the lists have different lengths
// and the second list length is greater
if(firstListLength < secondListLength)
{
for(int i =firstListLength-1;i<secondListLength;i++)
{
if(Math.abs(firstList[firstListLength-1]-secondList[i]) <= 3)
{
result.add(firstList[firstListLength-1]);
result.add(secondList[i]);
}
}
}
// here we are trying to handle the case when the lists have different lengths
// and the first list length is greater
else if (firstListLength > secondListLength)
{
for(int i =secondListLength-1;i<firstListLength;i++)
{
if(Math.abs(firstList[i]-secondList[secondListLength-1]) <= 3)
{
result.add(firstList[i]);
result.add(secondList[secondListLength-1]);
}
}
}
System.out.println(result.toString());
}
}
如果您不需要可怕的最终重复元素限制,在 groovy 中是:
[first,second].transpose()
.every { Math.abs(it[0]-it[1]) < threshold }
编辑
鉴于此函数将列表填充到特定宽度(默认为列表的最大长度):
def paddedPairs(List lists, Integer width=lists*.size().max()) {
(0..<width).collect { p -> lists.collect { it[ p >= it.size() ? -1 : p] } }
}
你可以这样做:
paddedPairs([first, second]).every { Math.abs(it[0]-it[1]) < threshold }
我有两个包含整数的列表,如下所示:
def first = [10, 12, 3, 6, 9, 8, 33]
def second = [8, 9, 5, 6]
每个列表中的元素数量完全可以是任意的。我还有一个门槛,也是一个数字:
def threshold = 3
我必须成对检查比较两个数组中的所有元素,并检查它们的差异是否 <= 阈值。结果我必须输出所有这些元素。
所以,在我的例子中是:
10, 12, 3, 6, 9, 8
来自第一个列表,8, 9, 5, 6
来自第二个列表。因为
abs(10-8) <= 3
abs(12-9) <= 3
abs(3-5) <= 3
abs(6-6) <= 3
这里由于第一个列表比第二个列表包含更多元素,我必须将第一个列表元素与第二个列表中的最后一个元素进行比较。
abs(9-6) <= 3
abs(8-6) <= 3
abs(33-6) >= 3, stop here!
Groovy 和 Java 答案是合适的。
P.S这是一个算法问题吗?一些算法已经存在?
这是完成任务的java 方法。既然你 abs()
减法,那么哪个先到并不重要。因此,让我们利用它并找出哪个更长。一旦我们知道了这一点,我们就知道要强制哪个元素重复它的最后一个元素。
if (first.length() > second.length()) {
longer = first;
shorter = second;
} else {
longer = second;
shorter = first;
}
last = shorter.length() - 1;
for(int i = 0; i < longer.length(); i++) {
s = shorter.get(Math.min(last, i));
l = longer.get(i);
if (abs(l-s) > threshold) {
break;
}
}
在下面的实现中我使用了set数据结构来防止结果中的元素重复,如果你想显示重复的元素你可以使用arraylist而不是set
import java.util.*;
public class CompareElements{
public static void main(String[ ] arg){
int [] firstList = {10, 12, 3, 6, 9, 8, 33};
int [] secondList = {8, 9, 5, 6};
int firstListLength = firstList.length;
int secondListLength = secondList.length;
// i have used set data structure to prevent duplication of elements in the result
Set<Integer>result=new HashSet<Integer>();
// iterate over the two list and get the absolute value for each two corresponding elements
// and check if the difference is <= 3 , the two elements are added to the result
for(int i=0;i<Math.min(firstList.length, secondList.length);i++) {
if(Math.abs(firstList[i]-secondList[i]) <= 3)
{
result.add(firstList[i]);
result.add(secondList[i]);
}
}
// here we are trying to handle the case when the lists have different lengths
// and the second list length is greater
if(firstListLength < secondListLength)
{
for(int i =firstListLength-1;i<secondListLength;i++)
{
if(Math.abs(firstList[firstListLength-1]-secondList[i]) <= 3)
{
result.add(firstList[firstListLength-1]);
result.add(secondList[i]);
}
}
}
// here we are trying to handle the case when the lists have different lengths
// and the first list length is greater
else if (firstListLength > secondListLength)
{
for(int i =secondListLength-1;i<firstListLength;i++)
{
if(Math.abs(firstList[i]-secondList[secondListLength-1]) <= 3)
{
result.add(firstList[i]);
result.add(secondList[secondListLength-1]);
}
}
}
System.out.println(result.toString());
}
}
如果您不需要可怕的最终重复元素限制,在 groovy 中是:
[first,second].transpose()
.every { Math.abs(it[0]-it[1]) < threshold }
编辑
鉴于此函数将列表填充到特定宽度(默认为列表的最大长度):
def paddedPairs(List lists, Integer width=lists*.size().max()) {
(0..<width).collect { p -> lists.collect { it[ p >= it.size() ? -1 : p] } }
}
你可以这样做:
paddedPairs([first, second]).every { Math.abs(it[0]-it[1]) < threshold }