Skip to content

feat(other): add grocery store cart model (v3)#14550

Open
nickzerjeski wants to merge 4 commits intoTheAlgorithms:masterfrom
nickzerjeski:renew-grocery-store-cart-14050-v2
Open

feat(other): add grocery store cart model (v3)#14550
nickzerjeski wants to merge 4 commits intoTheAlgorithms:masterfrom
nickzerjeski:renew-grocery-store-cart-14050-v2

Conversation

@nickzerjeski
Copy link
Copy Markdown

Describe your change:

Fixes #14050

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

Copilot AI review requested due to automatic review settings April 13, 2026 09:14
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Apr 13, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 GroceryStoreCart with add_item(), remove_item(), and total_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.

Comment on lines +39 to +42
if (remaining := current - quantity) > 0:
self.quantities[item] = remaining
else:
self.quantities.pop(item, None)
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
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

Copilot uses AI. Check for mistakes.

def total_price(self) -> float:
return sum(
self.price_catalog[item] * qty for item, qty in self.quantities.items()
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
self.price_catalog[item] * qty for item, qty in self.quantities.items()
self.price_catalog[item] * qty
for item, qty in self.quantities.items()

Copilot uses AI. Check for mistakes.
5.0
>>> cart.remove_item("apple")
>>> round(cart.total_price(), 2)
3.5
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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"

Copilot uses AI. Check for mistakes.
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Apr 13, 2026
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment