From 087faec3ddfcd390e21eafc732df96f4341fee43 Mon Sep 17 00:00:00 2001 From: Mukesh Gurpude Date: Mon, 6 Sep 2021 13:48:38 +0530 Subject: [PATCH 1/3] required changes are included --- .../inventory-management/.docs/hints.md | 8 +++++++- .../inventory-management/.docs/instructions.md | 18 +++++++++++++++++- .../inventory-management/.docs/introduction.md | 15 +++++++++++++++ .../inventory-management/.meta/design.md | 1 + .../inventory-management/.meta/exemplar.py | 13 +++++++++++++ .../concept/inventory-management/dicts.py | 10 ++++++++++ .../concept/inventory-management/dicts_test.py | 12 +++++++++++- 7 files changed, 74 insertions(+), 3 deletions(-) diff --git a/exercises/concept/inventory-management/.docs/hints.md b/exercises/concept/inventory-management/.docs/hints.md index f373118bd2b..7fd7957a536 100644 --- a/exercises/concept/inventory-management/.docs/hints.md +++ b/exercises/concept/inventory-management/.docs/hints.md @@ -23,7 +23,13 @@ - 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..5a5005fbb41 100644 --- a/exercises/concept/inventory-management/.docs/instructions.md +++ b/exercises/concept/inventory-management/.docs/instructions.md @@ -42,7 +42,23 @@ Item counts should not fall below `0`, if the number of items in the list exceed {"coal":0, "wood":0, "diamond":1} ``` -## 4. Return the inventory content +## 4. Remove an item from the inventory + +Implement the `remove_item()` function that removes an item entirely from an inventory: + +```python +>>> remove_item({"coal":2, "wood":1, "diamond":2}, "coal") +{"wood":1, "diamond":2} +``` + +If the item is not in the inventory, the function should return the 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..e7608befb01 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 its _key_. The item is removed from the dictionary and the value is returned. `dict.pop` accepts second argument `default` that is returned if the key is not found. If no default is provided, a `KeyError` is raised. + +```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 fb926075bde..8230ed23059 100644 --- a/exercises/concept/inventory-management/.meta/exemplar.py +++ b/exercises/concept/inventory-management/.meta/exemplar.py @@ -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 39acec41ab5..da189766500 100644 --- a/exercises/concept/inventory-management/dicts.py +++ b/exercises/concept/inventory-management/dicts.py @@ -30,6 +30,16 @@ def delete_items(inventory, items): 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 + + def list_inventory(inventory): ''' diff --git a/exercises/concept/inventory-management/dicts_test.py b/exercises/concept/inventory-management/dicts_test.py index a167ce31c04..aefafae6404 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, delete_items, remove_item, list_inventory class test_inventory(unittest.TestCase): @@ -43,6 +43,16 @@ def test_not_below_zero(self): {"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)]) From 4780eb56f1645e6e715c65e2bbdbf6e2c2acc943 Mon Sep 17 00:00:00 2001 From: Mukesh Gurpude <55982424+mukeshgurpude@users.noreply.github.com> Date: Tue, 7 Sep 2021 08:58:31 +0530 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: BethanyG --- .../concept/inventory-management/.docs/instructions.md | 6 +++--- .../concept/inventory-management/.docs/introduction.md | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/exercises/concept/inventory-management/.docs/instructions.md b/exercises/concept/inventory-management/.docs/instructions.md index 5a5005fbb41..6716f466ce6 100644 --- a/exercises/concept/inventory-management/.docs/instructions.md +++ b/exercises/concept/inventory-management/.docs/instructions.md @@ -42,16 +42,16 @@ Item counts should not fall below `0`, if the number of items in the list exceed {"coal":0, "wood":0, "diamond":1} ``` -## 4. Remove an item from the inventory +## 4. Remove an item entirely from the inventory -Implement the `remove_item()` function that removes an item entirely from an 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 in the inventory, the function should return the inventory unchanged. +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") diff --git a/exercises/concept/inventory-management/.docs/introduction.md b/exercises/concept/inventory-management/.docs/introduction.md index e7608befb01..2286ece090c 100644 --- a/exercises/concept/inventory-management/.docs/introduction.md +++ b/exercises/concept/inventory-management/.docs/introduction.md @@ -54,7 +54,7 @@ You can easily change a value of an item using its _key_. ## Deleting values using keys -You can delete an item from a dictionary using its _key_. The item is removed from the dictionary and the value is returned. `dict.pop` accepts second argument `default` that is returned if the key is not found. If no default is provided, a `KeyError` is raised. +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") From d66a24f738dbc62e26db2214da26c6e8979c3e94 Mon Sep 17 00:00:00 2001 From: Mukesh Gurpude Date: Tue, 7 Sep 2021 10:34:02 +0530 Subject: [PATCH 3/3] changed deleting items to decrementing items --- exercises/concept/inventory-management/.docs/hints.md | 2 +- .../concept/inventory-management/.docs/instructions.md | 10 +++++----- .../concept/inventory-management/.meta/exemplar.py | 6 +++--- exercises/concept/inventory-management/dicts.py | 6 +++--- exercises/concept/inventory-management/dicts_test.py | 8 ++++---- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/exercises/concept/inventory-management/.docs/hints.md b/exercises/concept/inventory-management/.docs/hints.md index 7fd7957a536..7db2ec8be57 100644 --- a/exercises/concept/inventory-management/.docs/hints.md +++ b/exercises/concept/inventory-management/.docs/hints.md @@ -17,7 +17,7 @@ - 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. diff --git a/exercises/concept/inventory-management/.docs/instructions.md b/exercises/concept/inventory-management/.docs/instructions.md index 6716f466ce6..f3fd749973e 100644 --- a/exercises/concept/inventory-management/.docs/instructions.md +++ b/exercises/concept/inventory-management/.docs/instructions.md @@ -26,19 +26,19 @@ 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} ``` diff --git a/exercises/concept/inventory-management/.meta/exemplar.py b/exercises/concept/inventory-management/.meta/exemplar.py index 8230ed23059..67fd586a897 100644 --- a/exercises/concept/inventory-management/.meta/exemplar.py +++ b/exercises/concept/inventory-management/.meta/exemplar.py @@ -24,12 +24,12 @@ 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: diff --git a/exercises/concept/inventory-management/dicts.py b/exercises/concept/inventory-management/dicts.py index da189766500..ed603d2cc30 100644 --- a/exercises/concept/inventory-management/dicts.py +++ b/exercises/concept/inventory-management/dicts.py @@ -19,12 +19,12 @@ 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 diff --git a/exercises/concept/inventory-management/dicts_test.py b/exercises/concept/inventory-management/dicts_test.py index aefafae6404..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 create_inventory, add_items, delete_items, remove_item, list_inventory +from dicts import create_inventory, add_items, decrement_items, remove_item, list_inventory class test_inventory(unittest.TestCase): @@ -31,14 +31,14 @@ 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})