Combined Item Weights
Imagine two invoices where each row contains an item value and its weight. Write a function merge_items that receives two lists of pairs, where each pair contains a value and a weight, and values do not repeat within the same list. Merge the lists by adding weights for matching values and keeping unique values as they are. Return the pairs sorted by value.
Примеры
Ввод
[[1, 1], [4, 5], [3, 8]], [[3, 1], [1, 5]]Вывод
[[1, 6], [3, 9], [4, 5]]Ввод
[[1, 1], [3, 2], [2, 3]], [[2, 1], [3, 2], [1, 3]]Вывод
[[1, 4], [2, 4], [3, 4]]Ввод
[[1, 3], [2, 2]], [[7, 1], [2, 2], [1, 4]]Вывод
[[1, 7], [2, 4], [7, 1]]Нужно реализовать
def merge_items(items1: list[list[int]], items2: list[list[int]]) -> list[list[int]]:
Теги
arrayhashmapdictsorting
Melde dich an, um den Einreichungsverlauf zu sehen
AnmeldenMelde dich an, um den KI-Mentor zu nutzen
AnmeldenVisible tests
1
Input
[[1,1],[4,5],[3,8]], [[3,1],[1,5]]Expected
[[1,6],[3,9],[4,5]]2
Input
[[1,1],[3,2],[2,3]], [[2,1],[3,2],[1,3]]Expected
[[1,4],[2,4],[3,4]]3
Input
[[1,3],[2,2]], [[7,1],[2,2],[1,4]]Expected
[[1,7],[2,4],[7,1]]4
Input
[[5,5]], [[5,5]]Expected
[[5,10]]5
Input
[[2,1]], [[1,1]]Expected
[[1,1],[2,1]]6
Input
[[10,1]], []Expected
[[10,1]]Focus radio
Paused · SomaFM · Fluid