feat(other): add grocery store cart model (v3)#14550
feat(other): add grocery store cart model (v3)#14550nickzerjeski wants to merge 4 commits intoTheAlgorithms:masterfrom
Conversation
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Pull request overview
Adds a small, console-free “grocery store cart” model to the other/ collection, intended to address issue #14050 by encapsulating cart state (quantities) and total calculation behind a class API.
Changes:
- Introduces
GroceryStoreCartwithadd_item(),remove_item(), andtotal_price(). - Adds basic validation (empty catalog, unknown item, non-positive quantity) and doctest-based usage example.
- Adds a
__main__doctest runner.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (remaining := current - quantity) > 0: | ||
| self.quantities[item] = remaining | ||
| else: | ||
| self.quantities.pop(item, None) |
There was a problem hiding this comment.
remove_item() currently treats quantity values larger than the current quantity as “remove everything” (because remaining <= 0 pops the item). This makes it easy to silently remove more items than are present. Consider raising a ValueError when quantity > current (and only popping when quantity == current).
| if (remaining := current - quantity) > 0: | |
| self.quantities[item] = remaining | |
| else: | |
| self.quantities.pop(item, None) | |
| if quantity > current: | |
| raise ValueError("quantity exceeds amount present in the cart") | |
| if quantity == current: | |
| self.quantities.pop(item, None) | |
| else: | |
| self.quantities[item] = current - quantity |
|
|
||
| def total_price(self) -> float: | ||
| return sum( | ||
| self.price_catalog[item] * qty for item, qty in self.quantities.items() |
There was a problem hiding this comment.
This total_price() return line is likely over the repo’s configured line-length limit (ruff/pycodestyle E501). Please wrap the sum(...) call across multiple lines to avoid lint failures.
| self.price_catalog[item] * qty for item, qty in self.quantities.items() | |
| self.price_catalog[item] * qty | |
| for item, qty in self.quantities.items() |
| 5.0 | ||
| >>> cart.remove_item("apple") | ||
| >>> round(cart.total_price(), 2) | ||
| 3.5 |
There was a problem hiding this comment.
The doctest only covers the happy path. Since __init__(), add_item(), and remove_item() introduce several error branches (empty catalog, unknown items, non-positive quantities), please add doctest cases that assert the expected exceptions/messages so these behaviors are exercised by CI.
| 3.5 | |
| 3.5 | |
| >>> GroceryStoreCart({}) | |
| Traceback (most recent call last): | |
| ... | |
| ValueError: price_catalog cannot be empty | |
| >>> cart.add_item("bread") | |
| Traceback (most recent call last): | |
| ... | |
| KeyError: "'bread' is not in the catalog" | |
| >>> cart.add_item("apple", 0) | |
| Traceback (most recent call last): | |
| ... | |
| ValueError: quantity must be positive | |
| >>> cart.remove_item("milk", 0) | |
| Traceback (most recent call last): | |
| ... | |
| ValueError: quantity must be positive | |
| >>> empty_cart = GroceryStoreCart({"apple": 1.5}) | |
| >>> empty_cart.remove_item("apple") | |
| Traceback (most recent call last): | |
| ... | |
| KeyError: "'apple' is not present in the cart" |
Describe your change:
Fixes #14050
Checklist: