Final Value of Variable After Performing Operations
Real-world version: Hand Tally Reading After The LogThe statement here is our own: the same inputs, outputs and technique.
A stock clerk carries a hand tally whose reading starts at 0. Every adjustment is written into a log as one of four three-character tokens naming the tally n:
"++n"and"n++"each raise the reading by one;"--n"and"n--"each lower it by one.
Whether the clerk wrote the pair of signs before or after the letter makes no difference to what happens to the reading.
taps holds the log entries in the order they were made. Return the reading once every entry has been applied. The reading may end up negative.
Examples
Example 1
- Input
- taps = ["--n", "n++", "n++"]
- Output
- 1
The reading starts at 0, drops to -1, then rises to 0 and to 1.
Example 2
- Input
- taps = ["n--", "n--", "--n"]
- Output
- -3
All three entries lower the reading, so it runs 0, -1, -2, -3.
Example 3
- Input
- taps = ["++n"]
- Output
- 1
A single rise takes the reading from 0 to 1.
Constraints
- 1 <= taps.length <= 100
- taps[i].length == 3
- Every entry of taps is one of "++n", "n++", "--n" or "n--".
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 counter_reading(taps: list[str]) -> int:public int counterReading(String[] taps)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 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