|
| 1 | +# Instructions |
| 2 | + |
| 3 | +In this exercise, you will be managing an inventory system. |
| 4 | + |
| 5 | +The inventory should be organized by the item name and it should keep track of the number of items available. |
| 6 | + |
| 7 | +You will have to handle adding items to an inventory. |
| 8 | +Each time an item appears in a given vector, the item's quantity should be increased by `1` in the inventory. |
| 9 | +You will also have to handle deleting items from an inventory by decreasing quantities by `1` when requested. |
| 10 | + |
| 11 | +Finally, you will need to implement a function that will return all the key-value pairs in a given inventory as a `vector` of `pair`s. |
| 12 | + |
| 13 | + |
| 14 | +## 1. Create an inventory based on a vector |
| 15 | + |
| 16 | +Implement the `create_inventory(<input vector>)` function that creates an "inventory" from an input vector of items. |
| 17 | +It should return a `dict` containing each item name paired with their respective quantity. |
| 18 | + |
| 19 | +```julia-repl |
| 20 | +julia> create_inventory(["coal", "wood", "wood", "diamond", "diamond", "diamond"]) |
| 21 | +Dict("coal" => 1, "wood" => 2, "diamond" => 3) |
| 22 | +``` |
| 23 | + |
| 24 | +## 2. Add items from a vector to an existing dictionary |
| 25 | + |
| 26 | +Implement the `add_items(<inventory dict>, <item vector>)` function that adds a vector of items to the passed-in inventory: |
| 27 | + |
| 28 | +```julia-repl |
| 29 | +julia> add_items(Dict("coal" => 1), ["wood", "iron", "coal", "wood"]) |
| 30 | +Dict("coal" => 2, "wood" => 2, "iron" => 1) |
| 31 | +``` |
| 32 | + |
| 33 | +## 3. Decrement items from the inventory |
| 34 | + |
| 35 | +Implement the `decrement_items(<inventory dict>, <items vector>)` function that takes a `vector` of items. |
| 36 | +Your function should remove `1` from an item count for each time that item appears on the `vector`: |
| 37 | + |
| 38 | +```julia-repl |
| 39 | +julia> decrement_items(Dict("coal" => 3, "diamond" => 1, "iron" => 5), ["diamond", "coal", "iron", "iron"]) |
| 40 | +Dict("coal" => 2, "diamond" => 0, "iron" => 3) |
| 41 | +``` |
| 42 | + |
| 43 | +Item counts in the inventory should not be allowed to fall below 0. |
| 44 | + If the number of times an item appears on the input `vector` exceeds the count available, the quantity listed for that item should remain at 0. |
| 45 | + Additional requests for removing counts should be ignored once the count falls to zero. |
| 46 | + |
| 47 | +```julia-repl |
| 48 | +julia> decrement_items(Dict("coal" => 2, "wood" => 1, "diamond" => 2), ["coal", "coal", "wood", "wood", "diamond"]) |
| 49 | +Dict("coal" => 0, "wood" => 0, "diamond" => 1) |
| 50 | +``` |
| 51 | + |
| 52 | +## 4. Remove an entry entirely from the inventory |
| 53 | + |
| 54 | +Implement the `remove_item(<inventory dict>, <item>)` function that removes an item and its count entirely from an inventory: |
| 55 | + |
| 56 | +```julia-repl |
| 57 | +julia> remove_item(Dict("coal" => 2, "wood" => 1, "diamond" => 2), "coal") |
| 58 | +Dict("wood" => 1, "diamond" => 2) |
| 59 | +``` |
| 60 | + |
| 61 | +If the item is not found in the inventory, the function should return the original inventory unchanged. |
| 62 | + |
| 63 | +```julia-repl |
| 64 | +julia> remove_item(Dict("coal" => 2, "wood" => 1, "diamond" => 2), "gold") |
| 65 | +Dict("coal" => 2, "wood" => 1, "diamond" => 2) |
| 66 | +``` |
| 67 | + |
| 68 | +## 5. Return the entire content of the inventory |
| 69 | + |
| 70 | +Implement the `list_inventory(<inventory dict>)` function that takes an inventory and returns a vector of `(item, quantity)` pairs. |
| 71 | + |
| 72 | +The vector should only include the _available_ items (_with a quantity greater than zero_), and should be sorted in alphabetical order of items: |
| 73 | + |
| 74 | +```julia-repl |
| 75 | +julia> list_inventory(Dict("coal" => 7, "wood" => 11, "diamond" => 2, "iron" => 7, "silver" => 0)) |
| 76 | +["coal" => 7, "diamond" => 2, "iron" => 7, "wood" => 11] |
| 77 | +``` |
0 commit comments