Practice problem · easy
A streaming app shows live cricket. The score feed writes each ball of an innings as one token, and puts a single space between tokens. A token is a digit from 0 to 6 (that many runs), W (a wicket, no runs), wd (a wide, one run) or nb (a no-ball, one run).
The score banner shows the total as runs, a slash and wickets, such as 14/1. Your team lead asks: "Turn the feed line into the banner text."
Write scorecard_total(line).
line holds one or more tokens, separated by single spaces, with no space at the start or the end.W tokens.runs/wickets, for example "14/1".Input: line = "1 4 W 0 6 wd 2"
Output: "14/1"Read the tokens from left to right and keep two numbers, runs and wickets.
| Token | Meaning | runs | wickets |
|---|---|---|---|
| 1 | 1 run | 1 | 0 |
| 4 | 4 runs | 5 | 0 |
| W | a wicket | 5 | 1 |
| 0 | no runs | 5 | 1 |
| 6 | 6 runs | 11 | 1 |
| wd | a wide: 1 run | 12 | 1 |
| 2 | 2 runs | 14 | 1 |
The banner reads 14/1.
Input: line = "W W W"
Output: "0/3"Three wickets and no runs. The runs are still written, as 0.
Input: line = "nb 4 wd"
Output: "6/0"A no-ball and a wide add one run each, and the four adds 4: 1 + 4 + 1 = 6. There is no wicket.
1 ≤ len(line) ≤ 300,000, with up to 100,000 tokens. The line can be the archive feed for a whole series of matches.0 to 6, W, wd or nb.There can be 100,000 tokens in one line of up to 300,000 characters. Reading each token once costs about 300,000 small steps. Starting again from the beginning of the line for every token costs about 100,000 times 300,000, which is 30 billion steps and never finishes.
scorecard_total · runs in your browserCoding is easiest on a laptop. On a phone, read, trace and try small edits.