MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / calPoints

Method calPoints

python/0682-baseball-game.py:2–24  ·  view source on GitHub ↗
(self, operations: List[str])

Source from the content-addressed store, hash-verified

1class Solution:
2 def calPoints(self, operations: List[str]) -> int:
3
4 score_stack = []
5
6 for o in operations:
7
8 # it is +, D, or C
9 # if stack isn't of sufficient length, then operation is voided
10 if o == "+" and len(score_stack) >= 2:
11 summed = score_stack[-2] + score_stack[-1]
12 score_stack.append(summed)
13
14 elif o == "D" and len(score_stack) >= 1:
15 doubled = score_stack[-1] * 2
16 score_stack.append(doubled)
17
18 elif o == "C" and len(score_stack) >= 1:
19 score_stack.pop()
20
21 else:
22 score_stack.append(int(o))
23
24 return sum(score_stack)

Callers

nothing calls this directly

Calls 2

sumFunction · 0.50
popMethod · 0.45

Tested by

no test coverage detected