Baseball Game
Real-world version: Tallying a Round With CorrectionsThe statement here is our own: the same inputs, outputs and technique.
A scorer's sheet reads entries, worked through in order. Each entry is one of the following:
- a whole number written as text: record that score;
"+": record a score equal to the last two recorded scores added together;"D": record a score equal to twice the last recorded score;"C": strike out the last recorded score, as though it had never been recorded.
Return all the recorded scores added together once the whole sheet has been worked through.
Every "+" has at least two scores already recorded, and every "D" and "C" at least one.
Examples
Example 1
- Input
- entries = ["3", "D", "D"]
- Output
- 21
Three is recorded, then twice it, six, then twice that, twelve. Three, six and twelve add to twenty-one.
Example 2
- Input
- entries = ["-5", "D", "+"]
- Output
- -30
Minus five is recorded, then twice it, minus ten, then those two added, minus fifteen. The three together come to minus thirty.
Example 3
- Input
- entries = ["10"]
- Output
- 10
One entry and one recorded score, so the total is that score.
Constraints
- 1 <= entries.length <= 1000
- Each entry is "C", "D", "+", or a whole number written as text between -30000 and 30000.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def cal_points(entries: list[str]) -> int:public int calPoints(String[] entries)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Number of Students Unable to Eat LunchReal-world version: Stranded at the Battery SwapEasy
- Asteroid CollisionReal-world version: Single Rail Sled RunMedium
- Validate Stack SequencesReal-world version: Could the Tube Have Run That WayMedium
- Robot CollisionsReal-world version: Freight Trolleys on One RailHard