Ctrl K

A One-Variable Language

Easy
A toy programming language has a single variable X, which starts at 0, and just four commands: - ++X and X++ add 1 to X; - --X and X-- subtract 1 from X. A program is an array of strings commands, run in order. Return the value of X once the whole program has run.
Example 1
commands=['X++', '--X', 'X--', 'X--'] → -2

0 → 1 → 0 → -1 → -2.

Example 2
commands=['++X', '++X', 'X++'] → 3

Three increments in a row: 0 → 1 → 2 → 3.

Example 3
commands=['X--'] → -1
Constraints
  • 1 ≤ commands.length ≤ 100
  • Each command is one of "++X", "X++", "--X", "X--"
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMirrored Codes EasyRobot on a Numbered Grid EasyJustify the Lines HardWords Read Down the Columns MediumSimultaneous Chess Moves Hard
loading Python…
Input
commands=['X++', '--X', 'X--', 'X--']
Expected
-2

Run checks the first 3 tests in your browser. Submit checks all 91 tests on the server, hidden ones included.