A test reactor's core output level starts at 0. Engineers issue a sequence of n control commands to the console one after another; each command is written as exactly one of four strings: ++PWR, PWR++, --PWR, or PWR--. Regardless of whether the ++ or -- symbol appears before or after PWR, a command containing ++ raises the output level by 1, and a command containing -- lowers it by 1.
Given the full sequence of commands in the order they were issued, determine the reactor's output level after all of them have been applied.
n, the number of commands.n space-separated tokens, each command in the order given.Print a single integer: the final output level.
++PWR, PWR++, --PWR, PWR--.Example 1
Input
4 ++PWR PWR++ --PWR PWR++
Expected
2
Explanation
Starting at 0: ++PWR raises it to 1, PWR++ raises it to 2, --PWR lowers it to 1, PWR++ raises it to 2. Final output level is 2.
Example 2
Input
3 --PWR --PWR --PWR
Expected
-3
Explanation
Starting at 0, three consecutive decrement commands lower the level by 1 each time: 0 -> -1 -> -2 -> -3. Final output level is -3.
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 →