curriculum/challenges/english/blocks/daily-coding-challenges-python/691b559495c5cb5a37b9b484.md
Given a 2D array representing the inventory of your store, and another 2D array representing a shipment you have received, return your updated inventory.
[quantity, "item"], where quantity is an integer and "item" is a string.For example, given an inventory of [[2, "apples"], [5, "bananas"]] and a shipment of [[1, "apples"], [3, "bananas"]], return [[3, "apples"], [8, "bananas"]].
update_inventory([[2, "apples"], [5, "bananas"]], [[1, "apples"], [3, "bananas"]]) should return [[3, "apples"], [8, "bananas"]].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(update_inventory([[2, "apples"], [5, "bananas"]], [[1, "apples"], [3, "bananas"]]), [[3, "apples"], [8, "bananas"]])`)
}})
update_inventory([[2, "apples"], [5, "bananas"]], [[1, "apples"], [3, "bananas"], [4, "oranges"]]) should return [[3, "apples"], [8, "bananas"], [4, "oranges"]].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(update_inventory([[2, "apples"], [5, "bananas"]], [[1, "apples"], [3, "bananas"], [4, "oranges"]]), [[3, "apples"], [8, "bananas"], [4, "oranges"]])`)
}})
update_inventory([], [[10, "apples"], [30, "bananas"], [20, "oranges"]]) should return [[10, "apples"], [30, "bananas"], [20, "oranges"]].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(update_inventory([], [[10, "apples"], [30, "bananas"], [20, "oranges"]]), [[10, "apples"], [30, "bananas"], [20, "oranges"]])`)
}})
update_inventory([[0, "Bowling Ball"], [0, "Dirty Socks"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]]) should return [[1, "Bowling Ball"], [0, "Dirty Socks"], [1, "Hair Pin"], [0, "Microphone"], [1, "Half-Eaten Apple"], [1, "Toothpaste"]].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(update_inventory([[0, "Bowling Ball"], [0, "Dirty Socks"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]]), [[1, "Bowling Ball"], [0, "Dirty Socks"], [1, "Hair Pin"], [0, "Microphone"], [1, "Half-Eaten Apple"], [1, "Toothpaste"]])`)
}})
def update_inventory(inventory, shipment):
return inventory
def update_inventory(inventory, shipment):
inventory_map = {item: i for i, (qty, item) in enumerate(inventory)}
for qty, item in shipment:
if item in inventory_map:
index = inventory_map[item]
inventory[index][0] += qty
else:
inventory.append([qty, item])
inventory_map[item] = len(inventory) - 1
return inventory