python/CodeChallenge_20.py

27 lines
717 B
Python

'''
Given an array of operations ++x, x++, --x. x-- and an integer target, return an initial integer, so that the final value is the target value.
Example 1
Input:
["x++", "--x", "--x"], 12
Output:
13
'''
from typing import List
class Solution(object):
def solve(self,ops:List[str], target:int) -> int:
print(f"ops: {ops}, target: {target}")
current = 0
for op in ops:
if op == "++x" or op == "x++":
current += 1
else:
current -= 1
return target - current
s = Solution()
print(s.solve(["x++", "--x", "--x"], 12)) # Output: 13
print(s.solve(["x++", "x++", "x--"], 2)) # Output: 1
print(s.solve(["--x", "x++", "x++"], 3))