From 81e822494c6913f1f20ca127b599851e22e817b0 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Thu, 2 Sep 2021 20:52:08 -0700 Subject: [PATCH 1/5] WIP typo and language edits for dicts concept. --- concepts/dicts/about.md | 26 +++++++++++++++++++------- concepts/dicts/introduction.md | 14 +++++++++++++- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/concepts/dicts/about.md b/concepts/dicts/about.md index 4756845707..fb43067efe 100644 --- a/concepts/dicts/about.md +++ b/concepts/dicts/about.md @@ -1,18 +1,30 @@ # About -A dictionary in Python is a data structure that associates [hashable][term-hashable] _keys_ to _values_ and can be known in other programming languages as a [hash table or hashmap][hashtable-wikipedia]. In Python, it's considered a [mapping type][mapping-types-dict]. `dicts` enable the retrieval of a value in constant time, given the key. -Compared to searching for a value within a list or array (_without knowing the index position_), a dictionary uses significantly more memory, but has very rapid retrieval. It's especially useful in scenarios where the collection is large and must be accessed frequently. +A dictionary (`dict`) is a [mapping type][mapping-types-dict] data structure that associates [hashable][term-hashable] `keys` to `values` -- known in other programming languages as a resizable [hash table or hashmap][hashtable-wikipedia]. + `Keys` can include `numbers`, `str`, `tuples` (of _immutable_ values), or `frozensets`, but must be hashable and unique across the dictionary. + `keys` are _immutable_ - once added to a `dict`, they can only be removed, they cannot be updated. + `values` can be of any or multiple data type(s) or structures, including other dictionaries, built-in types, custom types, or even objects like functions or classes. + `values` associated with any `key` are _mutable_, and can be replaced, updated or altered as long as the `key` entry exists. + Dictionaries enable the retrieval of a `value` in (on average) constant O(1) time, given the `key`. -## Keys and Values + Compared to searching for a value within a `list` or `array` (_without knowing the `index` position_), a `dict` uses significantly more space in memory, but has significantly more rapid retrieval. + Dictionaries are especially useful in scenarios where the collection of items is large and must be accessed and/or updated frequently. -A dictionary can be though of as a collection of straightforward `key`:`value` pairs. Like other collection types (`list`, `tuple`, and `set`), `values` can contain arbitrary data and data types, including nested dictionaries. A dictionary’s `keys` are _**almost**_ as arbitrary, but require their data types to be _hashable_. +## Dictionary creation -Keys are unique across a dictionary, and the values associated with any `key` can be replaced, updated or altered as long as the `key` exists. To be a valid, a key (_and all its contained values_) must be [hashable][term-hashable]. Hashable `keys` can include numbers, strings, tuples of _immutable_ values, or frozensets. `sets`, `lists`, and `dicts` are unhashable and therefor cannot be used as keys. +A simple `dict` can be declared using the literal form `{: , : }`: + + ```python + + + + +``` + + The dictionary constructor `dict(=, =)`, but there are many more ways of creating and initializing dictionaries including the use of a _dict comprehension_ or passing additional constructor parameters as illustrated in the [Python docs][mapping-types-dict]. -A value can be of any data type, including built-in types, custom types or complex objects (even functions or classes). Commonly used objects are: numbers, strings, lists, dictionaries, tuples or sets. -A simple `dict` can be declared using the literal form `{"key_1": value_1, "key_2": value_2}` or via _constructor_ with `dict(key_1=value_1, key_2=value_2)`, but there are many more ways of creating and initializing dictionaries including the use of a _dict comprehension_ or passing additional constructor parameters as illustrated in the [Python docs][mapping-types-dict]. Inserting a new `key`:`value` pair can be done with `dict[key] = value` and the value can be retrieved by using `retrieved_value = dict[key]`. diff --git a/concepts/dicts/introduction.md b/concepts/dicts/introduction.md index bcd1b78e7e..9a887ccfc6 100644 --- a/concepts/dicts/introduction.md +++ b/concepts/dicts/introduction.md @@ -1,3 +1,15 @@ # Introduction -A _**dictionary**_ is Python's primary mapping type that associates a _hashable key_ with a value. The lookup by key is more efficient than searching through an array, but does require more memory. + +A dictionary (`dict`) is a [mapping type][mapping-types-dict] data structure that associates [hashable][term-hashable] `keys` to `values` -- known in other programming languages as a resizable [hash table or hashmap][hashtable-wikipedia]. + `Keys` can include `numbers`, `str`, `tuples` (of _immutable_ values), or `frozensets`, but must be hashable and unique across the dictionary. + `values` can be of any or multiple data type(s) or structures, including other dictionaries, built-in types, custom types, or even objects like functions or classes. + Dictionaries enable the retrieval of a `value` in (on average) constant O(1) time, given the `key`. + + Compared to searching for a value within a `list` or `array` (_without knowing the `index` position_), a `dict` uses significantly more space in memory, but has significantly more rapid retrieval. + Dictionaries are especially useful in scenarios where the collection of items is large and must be accessed and/or updated frequently. + + +[hashtable-wikipedia]: https://en.wikipedia.org/wiki/Hash_table +[mapping-types-dict]: https://docs.python.org/3/library/stdtypes.html#mapping-types-dict +[term-hashable]: https://docs.python.org/3/glossary.html#term-hashable From 689ea114f1e68c265a8fde55b2ebfe98cc350ef0 Mon Sep 17 00:00:00 2001 From: Mukesh Gurpude Date: Mon, 6 Sep 2021 13:48:38 +0530 Subject: [PATCH 2/5] 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 f373118bd2..7fd7957a53 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 7bc067f522..5a5005fbb4 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 e2ee9ea2cb..e7608befb0 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 00e3205f53..916187af19 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 47627a1eab..6348920f35 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 c4946b8d25..7c4698ddd4 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 a167ce31c0..aefafae640 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 18f6198765fabf4d77c2d13b8bf3899bf593e0cf 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 3/5] 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 5a5005fbb4..6716f466ce 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 e7608befb0..2286ece090 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 9bba9fc8fa7c470d8e5ecce17bafdb00d75d565d Mon Sep 17 00:00:00 2001 From: Mukesh Gurpude Date: Tue, 7 Sep 2021 10:34:02 +0530 Subject: [PATCH 4/5] 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 | 10 +++++----- exercises/concept/inventory-management/dicts.py | 10 +++++----- exercises/concept/inventory-management/dicts_test.py | 8 ++++---- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/exercises/concept/inventory-management/.docs/hints.md b/exercises/concept/inventory-management/.docs/hints.md index 7fd7957a53..7db2ec8be5 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 6716f466ce..f3fd749973 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 6348920f35..d05f96b33d 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: @@ -39,12 +39,12 @@ def delete_items(inventory, items): 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) diff --git a/exercises/concept/inventory-management/dicts.py b/exercises/concept/inventory-management/dicts.py index 7c4698ddd4..b3f3fed72e 100644 --- a/exercises/concept/inventory-management/dicts.py +++ b/exercises/concept/inventory-management/dicts.py @@ -19,23 +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 aefafae640..ec781e1416 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}) From 71c193d24e374eedb66e85d2ac90321a15b1884b Mon Sep 17 00:00:00 2001 From: BethanyG Date: Mon, 6 Sep 2021 23:51:52 -0700 Subject: [PATCH 5/5] Added kotp and mukeshgurpude as contributors to the exercise. --- exercises/concept/inventory-management/.meta/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/concept/inventory-management/.meta/config.json b/exercises/concept/inventory-management/.meta/config.json index 491e787cca..1f0fb8e1a7 100644 --- a/exercises/concept/inventory-management/.meta/config.json +++ b/exercises/concept/inventory-management/.meta/config.json @@ -2,7 +2,7 @@ "blurb": "Learn about dicts by managing warehouse inventory.", "icon": "high-scores", "authors": ["j08k"], - "contributors": ["valentin-p", "bethanyG"], + "contributors": ["valentin-p", "bethanyG", "mukeshgurpude", "kotp"], "files": { "solution": ["dicts.py"], "test": ["dicts_test.py"],