A telemetry dump is a blob of arbitrary text with integers embedded in it. Extract the integers by scanning left to right and repeatedly taking, at the earliest possible position, the longest match of: an optional single - sign immediately followed by one or more digits. Concretely:
- character, the integer is negative, and that - is consumed as the sign (it cannot also be the sign of any other integer).For example, in 3-5 the scan first reads 3, then reads -5, giving the integers 3 and -5. In --7 only -7 is read (the first - is not followed by a digit).
Print the sum of all extracted integers.
The entire input is the telemetry text (it may span several lines).
A single integer: the sum of every extracted integer (0 if there are none).
Example 1
Input
temp=-5 rate=12 err=-3
Expected
4
Explanation
The scan reads -5, 12, and -3. Their sum is -5 + 12 + -3 = 4.
Example 2
Input
3-5
Expected
-2
Explanation
First the digit 3 is read (nothing precedes it), then -5 is read (the minus is its sign). The sum is 3 + (-5) = -2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →