Variable After Commands
Imagine a variable X that starts at 0 and then receives short commands to increase or decrease it. Write a function final_value that processes the list operations. The operations "++X" and "X++" add 1, while "--X" and "X--" subtract 1. Return the final value of X after all commands are applied.
Примеры
Ввод
["--X", "X++", "X++"]Вывод
1Ввод
["++X", "++X", "X++"]Вывод
3Ввод
["X++", "++X", "--X", "X--"]Вывод
0Нужно реализовать
def final_value(operations: list[str]) -> int:
Теги
stringsarray
Log in om je inzendingsgeschiedenis te zien
InloggenLog in om de AI-mentor te gebruiken
InloggenVisible tests
1
Input
["--X","X++","X++"]Expected
12
Input
["++X","++X","X++"]Expected
33
Input
["X++","++X","--X","X--"]Expected
04
Input
[]Expected
05
Input
["--X"]Expected
-16
Input
["X++","X++","X++"]Expected
3Focus radio
Paused · SomaFM · Fluid