-
-
Notifications
You must be signed in to change notification settings - Fork 50.3k
feat(other): add grocery store cart model (v3) #14550
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
Open
nickzerjeski
wants to merge
7
commits into
TheAlgorithms:master
Choose a base branch
from
nickzerjeski:renew-grocery-store-cart-14050-v2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d28896c
feat(other): add grocery store cart model
nickzerjeski 9611435
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4e7fa49
Fix Ruff EM102 in grocery store cart exceptions
nickzerjeski eedcb14
Address review feedback for grocery cart edge cases
nickzerjeski d5e1fb0
Apply suggestions from code review
nickzerjeski 2060085
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4a679e1
Merge branch 'master' into renew-grocery-store-cart-14050-v2
nickzerjeski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| """ | ||
| Console-free grocery cart logic. | ||
| """ | ||
|
|
||
|
|
||
| class GroceryStoreCart: | ||
| """ | ||
| Maintain cart item quantities and compute totals. | ||
|
|
||
| >>> cart = GroceryStoreCart({"apple": 1.5, "milk": 2.0}) | ||
| >>> cart.add_item("apple", 2) | ||
| >>> cart.add_item("milk") | ||
| >>> round(cart.total_price(), 2) | ||
| 5.0 | ||
| >>> cart.remove_item("apple") | ||
| >>> round(cart.total_price(), 2) | ||
| 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" | ||
|
|
||
| >>> 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" | ||
| """ | ||
|
|
||
| def __init__(self, price_catalog: dict[str, float]) -> None: | ||
| if not price_catalog: | ||
| raise ValueError("price_catalog cannot be empty") | ||
| self.price_catalog = dict(price_catalog) | ||
| self.quantities: dict[str, int] = {} | ||
|
|
||
| def add_item(self, item: str, quantity: int = 1) -> None: | ||
| if item not in self.price_catalog: | ||
| msg = f"{item!r} is not in the catalog" | ||
| raise KeyError(msg) | ||
| if quantity <= 0: | ||
| raise ValueError("quantity must be positive") | ||
| self.quantities[item] = self.quantities.get(item, 0) + quantity | ||
|
|
||
| def remove_item(self, item: str, quantity: int = 1) -> None: | ||
| if quantity <= 0: | ||
| raise ValueError("quantity must be positive") | ||
| current = self.quantities.get(item, 0) | ||
| if current == 0: | ||
| msg = f"{item!r} is not present in the cart" | ||
| raise KeyError(msg) | ||
| if quantity > current: | ||
| raise ValueError("quantity exceeds amount present in the cart") | ||
| 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() | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import doctest | ||
|
|
||
| doctest.testmod() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.