Skip to content

Commit

Permalink
Merge pull request #15 from mukeshgurpude/dicts-typo-patrol
Browse files Browse the repository at this point in the history
required changes are included
  • Loading branch information
BethanyG authored Sep 7, 2021
2 parents 2440dac + aa7fa5e commit 4069b25
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 22 deletions.
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
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
23 changes: 18 additions & 5 deletions exercises/concept/inventory-management/.meta/exemplar.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,33 @@ 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:
inventory[item] = max(inventory[item] - 1, 0)
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
20 changes: 15 additions & 5 deletions exercises/concept/inventory-management/dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

0 comments on commit 4069b25

Please sign in to comment.