Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Inventory Management] Typo & Language Edits for dicts Concept and Exercise. #2530

Merged
merged 5 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions concepts/dicts/about.md
Original file line number Diff line number Diff line change
@@ -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 `{<key_1>: <value_1>, <key_2>: <value_2>}`:

```python




```

The dictionary constructor `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].

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]`.

Expand Down
14 changes: 13 additions & 1 deletion concepts/dicts/introduction.md
Original file line number Diff line number Diff line change
@@ -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
10 changes: 8 additions & 2 deletions exercises/concept/inventory-management/.docs/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 22 additions & 6 deletions exercises/concept/inventory-management/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(<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(<inventory>, <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):

Expand Down
15 changes: 15 additions & 0 deletions exercises/concept/inventory-management/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(<key>)`. This will remove the `(key`, `value`) pair from the dictionary and return the `value` for use. `dict.pop(<key>)` accepts second argument, `default` that is returned if the `key` is not found (`dict.pop(<key>, <default>)`). 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 "<stdin>", line 1, in <module>
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_.
Expand Down
2 changes: 1 addition & 1 deletion exercises/concept/inventory-management/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
1 change: 1 addition & 0 deletions exercises/concept/inventory-management/.meta/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`

Expand Down
19 changes: 16 additions & 3 deletions exercises/concept/inventory-management/.meta/exemplar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
"""

Expand Down
16 changes: 13 additions & 3 deletions exercises/concept/inventory-management/dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@ 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
Expand Down
18 changes: 14 additions & 4 deletions exercises/concept/inventory-management/dicts_test.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)])
Expand Down