算法用于比较排序列表的价格

huangapple go评论87阅读模式
英文:

Algorithm to Compare Price of Sorted List

问题

今天,我想要实现一个程序(使用Java或Python语言),它可以比较两个食品项目排序数组的元素,并输出这两个数组中价格更低的项目价格。

数组的形式将会是 {{名称,价格}}。将会有两个这样的数组进行比较。可能这样说不够清楚,所以下面是一些代码:

数组1:{{"Apple",4.5},{"Banana",3.1},{"Dragonfruit",5.2},{"Dorian",1.5}}

数组2:{{"Apple",3.1},{"Dragonfruit",4.5}}

输出:{{"Apple",3.1},{"Dragonfruit",4.5}}

正如你在这里所看到的,这两个数组按照名称的字母顺序进行了排序。这两个数组的长度也可以不同。所以总结起来,它应该实现以下功能:

  1. 首先比较项目的名称,如果它们相同:
  2. 输出价格更低的项目。

我听说这似乎是一种类型的连续排序?我之前搜索过这个词,但我不知道如何实现它。

英文:

So today, I want to implement a program (java or python) that compares elements of two sorted array of food items and outputs the lowest price of the item out of the two arrays.

The array will be in the form {{name, price}}. There will be two of these arrays to compare from. This might not seem very clear, so here's some code:

Array 1: {{"Apple" 4.5}, {"Banana" 3.1}, {"Dragonfruit", 5.2}, {"Dorian", 1.5}}

Array 2: {{"Apple", 3.1}, {"Dragonfruit", 4.5}}

Output: {{"Apple", 3.1}, {"Dragonfruit", 4.5}}

As you can see here, the 2 arrays are sorted by name in alphabetical order. The 2 arrays do not have to be the same length either. So in summary, it should:

  1. Compare the name of the item first, if they are the same:
  2. Output the one that costs less.

I heard somewhere that this is a type of cosequential sorting? I have searched this word before but I have no idea how to implement it.

答案1

得分: 1

Concatenete the two lists and sort them lexicographically, first on the name, then on the price in case of ties.

Then it suffices to output the records that are followed by the same name.

{"Apple", 3.1}
{"Apple", 4.5}
{"Banana" 3.1}
{"Dorian", 1.5}} 
{"Dragonfruit", 4.5}
{"Dragonfruit", 5.2}

This works for more than two lists.

英文:

Concatenete the two lists and sort them lexicographically, first on the name, then on the price in case of ties.

Then it suffices to output the records that are followed by the same name.

{"Apple", 3.1}
{"Apple", 4.5}
{"Banana" 3.1}
{"Dorian", 1.5}} 
{"Dragonfruit", 4.5}
{"Dragonfruit", 5.2}

This works for more than two lists.

答案2

得分: -1

public class TestDemo {

	public static void main(String[] args) {
		List<Fruit> list1 = new ArrayList<Fruit>();
		List<Fruit> list2 = new ArrayList<Fruit>();
		List<Fruit> list3 = new ArrayList<Fruit>();
		list1.add(new Fruit("Dragonfruit", 5.2));
		list1.add(new Fruit("Dorian", 1.5));
		list1.add(new Fruit("Apple", 4.5));
		list1.add(new Fruit("Banana", 3.1));
		Collections.sort(list1);
		list2.add(new Fruit("Dragonfruit", 4.5));
		list2.add(new Fruit("Apple", 3.1));
		Collections.sort(list2);
		for (Fruit fruit1 : list2) {
			for (Fruit fruit2 : list1) {
				if (fruit1.getName().equals(fruit2.getName())) {
					if (fruit1.getPrice() <= fruit2.getPrice()) {
						list3.add(fruit1);
					} else {
						list3.add(fruit2);
					}
				}
			}
		}
		System.out.println(list1);
		System.out.println(list2);
		System.out.println(list3);
	}
}

public class Fruit implements Comparable<Fruit> {

	private String name;
	private Double price;
	@Override
	public int compareTo(Fruit fruit) {

		return this.name.compareTo(fruit.name);
	}
}
英文:

public class TestDemo {

public static void main(String[] args) {
List&lt;Fruit&gt; list1 = new ArrayList&lt;Fruit&gt;();
List&lt;Fruit&gt; list2 = new ArrayList&lt;Fruit&gt;();
List&lt;Fruit&gt; list3 = new ArrayList&lt;Fruit&gt;();
list1.add(new Fruit(&quot;Dragonfruit&quot;, 5.2));
list1.add(new Fruit(&quot;Dorian&quot;, 1.5));
list1.add(new Fruit(&quot;Apple&quot;, 4.5));
list1.add(new Fruit(&quot;Banana&quot;, 3.1));
Collections.sort(list1);
list2.add(new Fruit(&quot;Dragonfruit&quot;, 4.5));
list2.add(new Fruit(&quot;Apple&quot;, 3.1));
Collections.sort(list2);
for (Fruit fruit1 : list2) {
for (Fruit fruit2 : list1) {
if (fruit1.getName().equals(fruit2.getName())) {
if (fruit1.getPrice() &lt;= fruit2.getPrice()) {
list3.add(fruit1);
} else {
list3.add(fruit2);
}
}
}
}
System.out.println(list1);
System.out.println(list2);
System.out.println(list3);
}

}

public class Fruit implements Comparable<Fruit> {

private String name;
private Double price;
@Override
public int compareTo(Fruit fruit) {
return this.name.compareTo(fruit.name);
}

}

huangapple
  • 本文由 发表于 2020年10月27日 14:40:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64549363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定