Date: 2026-06-06
Time: 15:21
baseball-game/solution.pyThis file solves LeetCode 682: Baseball Game. It owns the scoring logic for a simplified baseball game where a sequence of string operations builds up a record of scores, and the answer is the final sum. It's a textbook stack problem.
calPoints(operations: list[str]) -> int — The sole public function. Takes a list of string-encoded operations and returns the total score.
The four operation types:
| Operation | Meaning | Stack effect |
|-----------|---------|-------------|
| Integer string (e.g. "5", "-3") | Record a new score | push(int(op)) |
| "C" | Invalidate the last score | pop() |
| "D" | Double the last score | push(top * 2) |
| "+" | Sum of last two scores | push(top + second) |
Stack-based simulation. The problem maps directly to a stack: "C" pops, "D" and "+" peek at the top elements and push a derived value, and raw integers push directly. The final answer is sum(stack) — every element still on the stack is a valid score.
Exhaustive if/elif/else dispatch. The operations are handled in a chain with else catching the integer case. This is idiomatic for LeetCode solutions with a small fixed set of special tokens.
Imports: None — pure stdlib, no external dependencies.
Imported by: baseball-game/test_solution.py directly. The "Imported By" list in the prompt shows hundreds of test files, but that's an artifact of the repo's test infrastructure — those other test files don't actually import calPoints. Only the co-located test file exercises this function.
1. Initialize an empty stack: list[int].
2. Iterate over each op in operations.
3. Strip whitespace from op (defensive — LeetCode inputs don't have trailing whitespace, but it's harmless).
4. Dispatch on the op value: pop, push doubled top, push sum of top two, or push the parsed integer.
5. After processing all operations, return sum(stack).
"C", "D", and "+" only appear when there are enough elements on the stack (1 for C/D, 2 for +). The code does not validate this — it trusts the LeetCode contract.int. The int(op) conversion in the else branch and arithmetic in the other branches ensure this.There is none. If the input violates the problem's preconditions (e.g., "C" on an empty stack, or a non-numeric non-operator string), the code will raise an unhandled IndexError or ValueError. This is intentional — LeetCode guarantees valid input.
calpoints-stack-only — calPoints uses a single list as a stack and never indexes into arbitrary positions; it only accesses [-1] and [-2]calpoints-no-input-validation — The function assumes all inputs are valid per the LeetCode contract and will raise IndexError or ValueError on malformed inputcalpoints-strip-defensive — The op.strip() call is defensive against whitespace; LeetCode inputs never contain it, but the code handles it anywaycalpoints-linear-time — The function runs in O(n) time for n operations, with O(n) space for the stack in the worst case (no "C" operations)