diff --git a/exercises/concept/inventory-management/.docs/hints.md b/exercises/concept/inventory-management/.docs/hints.md index f373118bd2b..7db2ec8be57 100644 --- a/exercises/concept/inventory-management/.docs/hints.md +++ b/exercises/concept/inventory-management/.docs/hints.md @@ -17,13 +17,19 @@ - The function `add_items` can be used by the `create_inventory` function with an empty dictionary in parameter. - This function should [return a dict](https://www.w3schools.com/python/ref_keyword_return.asp). -## 3. Delete items from a dictionary +## 3. Decrement items in the dictionary - You need [for loop](https://docs.python.org/3/tutorial/controlflow.html#for-statements) to iterate the list of items, if the number of items is not `0` then [decrement](https://www.w3schools.com/python/gloss_python_assignment_operators.asp) the current number of items. - You can use the `key in dict` that returns `True` if the key exists to make sure the value is in the dictionary before decrementing the number of items. - This function should [return a dict](https://www.w3schools.com/python/ref_keyword_return.asp). -## 4. List the items that are in stock +## 4. Remove an item from a dictionary + +- If item is in the dictionary, [remove it](https://www.w3schools.com/python/ref_dictionary_pop.asp). +- If item is not in the dictionary, do nothing. +- This function should [return a dict](https://www.w3schools.com/python/ref_keyword_return.asp). + +## 5. List the items that are in stock - You need [for loop](https://docs.python.org/3/tutorial/controlflow.html#for-statements) on the inventory and if the number of item is greater of `0` then append the tuple to a list. - You can use `dict.items()` to iterate on both the item and the value at the same time, `items()` returns a tuple that you can use as it is or deconstruct. diff --git a/exercises/concept/inventory-management/.docs/instructions.md b/exercises/concept/inventory-management/.docs/instructions.md index 7bc067f5228..f3fd749973e 100644 --- a/exercises/concept/inventory-management/.docs/instructions.md +++ b/exercises/concept/inventory-management/.docs/instructions.md @@ -26,23 +26,39 @@ Implement the `add_items()` function that adds a list of items to an inventory: {"coal":2, "wood":2, "iron":1} ``` -## 3. Remove items from the inventory +## 3. Decrement items from the inventory -Implement the `delete_items()` function that removes every item in the list from an inventory: +Implement the `decrement_items()` function that takes a `list` of items. The function should remove one from the available count in the inventory for each time an item appears on the `list`: ```python ->>> delete_items({"coal":3, "diamond":1, "iron":5}, ["diamond", "coal", "iron", "iron"]) +>>> decrement_items({"coal":3, "diamond":1, "iron":5}, ["diamond", "coal", "iron", "iron"]) {"coal":2, "diamond":0, "iron":3} ``` -Item counts should not fall below `0`, if the number of items in the list exceeds the number of items available in the inventory, the listed quantity for that item should remain at `0` and the request for removing that item should be ignored. +Item counts in the inventory should not fall below 0. If the number of times an item appears on the list exceeds the count available, the quantity listed for that item should remain at 0 and additional requests for removing counts should be ignored. ```python ->>> delete_items({"coal":2, "wood":1, "diamond":2}, ["coal", "coal", "wood", "wood", "diamond"]) +>>> decrement_items({"coal":2, "wood":1, "diamond":2}, ["coal", "coal", "wood", "wood", "diamond"]) {"coal":0, "wood":0, "diamond":1} ``` -## 4. Return the inventory content +## 4. Remove an item entirely from the inventory + +Implement the `remove_item(, )` function that removes an item and its count entirely from an inventory: + +```python +>>> remove_item({"coal":2, "wood":1, "diamond":2}, "coal") +{"wood":1, "diamond":2} +``` + +If the item is not found in the inventory, the function should return the original inventory unchanged. + +```python +>>> remove_item({"coal":2, "wood":1, "diamond":2}, "gold") +{"coal":2, "wood":1, "diamond":2} +``` + +## 5. Return the inventory content Implement the `list_inventory()` function that takes an inventory and returns a list of `(item, quantity)` tuples. The list should only include the available items (with a quantity greater than zero): diff --git a/exercises/concept/inventory-management/.docs/introduction.md b/exercises/concept/inventory-management/.docs/introduction.md index e2ee9ea2cbe..2286ece090c 100644 --- a/exercises/concept/inventory-management/.docs/introduction.md +++ b/exercises/concept/inventory-management/.docs/introduction.md @@ -52,6 +52,21 @@ You can easily change a value of an item using its _key_. {'name': 'Blue Whale', 'speed': 25, 'land_animal': False} ``` +## Deleting values using keys + +You can delete an item from a dictionary using `dict.pop()`. This will remove the `(key`, `value`) pair from the dictionary and return the `value` for use. `dict.pop()` accepts second argument, `default` that is returned if the `key` is not found (`dict.pop(, )`). Otherwise, a `KeyError` will be raised for any `key` that is missing. + +```python +>>> bear.pop("name") +'Grizzly Bear' +>>> bear.pop("name", "Unknown") +'Unknown' +>>> bear.pop("name") +Traceback (most recent call last): + File "", line 1, in +KeyError: 'name' +``` + ## Looping through a dictionary Looping through a dictionary using `for item in dict` will iterate over the _keys_, but you can access the _values_ by using _square brackets_. diff --git a/exercises/concept/inventory-management/.meta/design.md b/exercises/concept/inventory-management/.meta/design.md index 00e3205f531..916187af190 100644 --- a/exercises/concept/inventory-management/.meta/design.md +++ b/exercises/concept/inventory-management/.meta/design.md @@ -12,6 +12,7 @@ The goal of this exercise is to teach the basics of the `dict` (dictionary, mapp - check for membership of a key in a given dictionary. - add new `key`:`value` pairs to the `dict`. - remove `key`:`value` pairs from the `dict`. +- remove `key`:`value` pairs from the `dict` using `dict.pop()`. - iterate through a `dict` using `dict.keys()`, `dict.values()`, or `dict.items()`. - `dict` method `setdefault()` diff --git a/exercises/concept/inventory-management/.meta/exemplar.py b/exercises/concept/inventory-management/.meta/exemplar.py index 47627a1eab1..2a07c47a17c 100644 --- a/exercises/concept/inventory-management/.meta/exemplar.py +++ b/exercises/concept/inventory-management/.meta/exemplar.py @@ -24,13 +24,13 @@ def add_items(inventory, items): return inventory -def delete_items(inventory, items): - """ +def decrement_items(inventory, items): + ''' :param inventory: dict - inventory dictionary. - :param items: list - list of items to remove from the inventory. - :return: dict - updated inventory dictionary with items removed. - """ + :param items: list - list of items to decrement from the inventory. + :return: dict - updated inventory dictionary with items decremented. + ''' for item in items: if item in inventory: @@ -38,6 +38,19 @@ def delete_items(inventory, items): return inventory +def remove_item(inventory, item): + ''' + + :param inventory: dict - inventory dictionary. + :param item: str - item to remove from the inventory. + :return: dict - updated inventory dictionary with item removed. + ''' + + if item in inventory: + inventory.pop(item) + return inventory + + def list_inventory(inventory): """ diff --git a/exercises/concept/inventory-management/dicts.py b/exercises/concept/inventory-management/dicts.py index c4946b8d25b..d9be5f73495 100644 --- a/exercises/concept/inventory-management/dicts.py +++ b/exercises/concept/inventory-management/dicts.py @@ -19,13 +19,23 @@ def add_items(inventory, items): pass -def delete_items(inventory, items): - """ +def decrement_items(inventory, items): + ''' :param inventory: dict - inventory dictionary. - :param items: list - list of items to remove from the inventory. - :return: dict - updated inventory dictionary with items removed. - """ + :param items: list - list of items to decrement from the inventory. + :return: dict - updated inventory dictionary with items decremented. + ''' + + pass + + +def remove_item(inventory, item): + ''' + :param inventory: dict - inventory dictionary. + :param item: str - item to remove from the inventory. + :return: dict - updated inventory dictionary with item removed. + ''' pass diff --git a/exercises/concept/inventory-management/dicts_test.py b/exercises/concept/inventory-management/dicts_test.py index a167ce31c04..ec781e14167 100644 --- a/exercises/concept/inventory-management/dicts_test.py +++ b/exercises/concept/inventory-management/dicts_test.py @@ -1,6 +1,6 @@ import unittest import pytest -from dicts import * +from dicts import create_inventory, add_items, decrement_items, remove_item, list_inventory class test_inventory(unittest.TestCase): @@ -31,18 +31,28 @@ def test_add_from_empty_dict(self): {"iron": 2, "diamond": 1}) @pytest.mark.task(taskno=3) - def test_delete_items(self): - self.assertEqual(delete_items({"iron": 3, "diamond": 4, "gold": 2}, + def test_decrement_items(self): + self.assertEqual(decrement_items({"iron": 3, "diamond": 4, "gold": 2}, ["iron", "iron", "diamond", "gold", "gold"]), {"iron": 1, "diamond": 3, "gold": 0}) @pytest.mark.task(taskno=3) def test_not_below_zero(self): - self.assertEqual(delete_items({"wood": 2, "iron": 3, "diamond": 1}, + self.assertEqual(decrement_items({"wood": 2, "iron": 3, "diamond": 1}, ["wood", "wood", "wood", "iron", "diamond", "diamond"]), {"wood": 0, "iron": 2, "diamond": 0}) @pytest.mark.task(taskno=4) + def test_remove_item(self): + self.assertEqual(remove_item({"iron": 1, "diamond": 2, "gold": 1}, "diamond"), + {"iron": 1, "gold": 1}) + + @pytest.mark.task(taskno=4) + def test_remove_item_not_in_inventory(self): + self.assertEqual(remove_item({"iron": 1, "diamond": 2, "gold": 1}, "wood"), + {"iron": 1, "gold": 1, "diamond": 2}) + + @pytest.mark.task(taskno=5) def test_list_inventory(self): self.assertEqual(list_inventory({"coal": 15, "diamond": 3, "wood": 67, "silver": 0}), [("coal", 15), ("diamond", 3), ("wood", 67)])