Merge pull request 'Milestone 2: Core auction logic' (#14) from milestone/02-core-logic into main
TirSystem/github-action: Sync GitHub mirror metadata / sync-metadata (push) Successful in 4s

Reviewed-on: Tirsvad-Udemy-100_days_of_code/009-the_secret_auction_program#14
This commit was merged in pull request #14.
This commit is contained in:
2026-10-04 08:09:33 +02:00
5 changed files with 133 additions and 84 deletions
-4
View File
@@ -1,4 +0,0 @@
# Copy to .env and fill in. Never commit .env.
GITEA_TOKEN=
GITHUB_PAT=
GITHUB_USER=
+3 -4
View File
@@ -20,11 +20,10 @@ Work on ONE milestone at a time. Each milestone is one branch (`milestone/NN-nam
- [x] Verify: create `.venv`, upgrade pip, `pip install -e ".[dev]"` - [x] Verify: create `.venv`, upgrade pip, `pip install -e ".[dev]"`
- [x] Save plan and milestones in `docs/plan.md` - [x] Save plan and milestones in `docs/plan.md`
## Milestone 2 - Core auction logic and env loader (`milestone/02-core-logic`) ## Milestone 2 - Core auction logic (`milestone/02-core-logic`)
- [ ] `auction.py` (Doxygen comments): `add_bid`, `find_winner` (uses `max(bids, key=bids.get)`, `ValueError` on empty) - [x] `auction.py` (Doxygen comments): `add_bid`, `find_winner` (uses `max(bids, key=bids.get)`, `ValueError` on empty)
- [ ] `env.py`: stdlib `.env` loader (no python-dotenv); does not override existing environment variables - [x] `tests/test_auction.py`
- [ ] `tests/test_auction.py`, `tests/test_env.py`
- Verify: `pytest` passes - Verify: `pytest` passes
## Milestone 3 - CLI program (`milestone/03-cli`) ## Milestone 3 - CLI program (`milestone/03-cli`)
+27
View File
@@ -0,0 +1,27 @@
"""! @file auction.py
@brief Core blind auction logic (no input/output).
"""
def add_bid(bids: dict[str, float], name: str, amount: float) -> None:
"""! @brief Record a bid, replacing any earlier bid by the same bidder.
@param bids Dictionary mapping bidder name to bid amount (modified in place).
@param name Name of the bidder.
@param amount Bid amount; must not be negative.
@throws ValueError If the amount is negative.
"""
if amount < 0:
raise ValueError("Bid amount must not be negative.")
bids[name] = amount
def find_winner(bids: dict[str, float]) -> tuple[str, float]:
"""! @brief Find the highest bidder.
@param bids Dictionary mapping bidder name to bid amount.
@return Tuple of (winner name, winning amount). The first bidder wins ties.
@throws ValueError If there are no bids.
"""
if not bids:
raise ValueError("No bids have been placed.")
winner = max(bids, key=bids.__getitem__)
return winner, bids[winner]
-6
View File
@@ -11,12 +11,6 @@ YES_ANSWERS = ("yes", "y")
## Answers accepted as "no" when asking for more bidders. ## Answers accepted as "no" when asking for more bidders.
NO_ANSWERS = ("no", "n") NO_ANSWERS = ("no", "n")
## Path of the file holding environment variables (tokens).
ENV_FILE = ".env"
## Name of the environment variable holding the Gitea token.
GITEA_TOKEN_ENV_VAR = "GITEA_TOKEN"
## Prompt asking for the bidder's name. ## Prompt asking for the bidder's name.
PROMPT_NAME = "What is your name? " PROMPT_NAME = "What is your name? "
+33
View File
@@ -0,0 +1,33 @@
import pytest
from secret_auction.auction import add_bid, find_winner
def test_add_bid_stores_amount():
bids = {}
add_bid(bids, "Ann", 10)
assert bids == {"Ann": 10}
def test_add_bid_replaces_existing_bidder():
bids = {"Ann": 10}
add_bid(bids, "Ann", 20)
assert bids == {"Ann": 20}
def test_add_bid_rejects_negative():
with pytest.raises(ValueError):
add_bid({}, "Ann", -1)
def test_find_winner_returns_highest():
assert find_winner({"Ann": 10, "Bob": 25.5, "Cy": 3}) == ("Bob", 25.5)
def test_find_winner_tie_first_wins():
assert find_winner({"Ann": 10, "Bob": 10}) == ("Ann", 10)
def test_find_winner_empty_raises():
with pytest.raises(ValueError):
find_winner({})