digit_stack   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 7

1 Function

Rating   Name   Duplication   Size   Complexity  
B digit_stack() 0 20 7
1
def digit_stack(commands):
2
    stack = []
3
    sum_of_commands = 0
4
    for i in commands:
5
        cmd = i.split()
6
        if cmd[0] == 'PUSH':
7
            stack.append(int(cmd[1]))
8
        elif cmd[0] == 'POP':
9
            try:
10
                tmp = stack.pop()
11
            except IndexError:
12
                tmp = 0
13
            sum_of_commands += tmp
14
        elif cmd[0] == 'PEEK':
15
            try:
16
                tmp = stack[-1]
17
            except IndexError:
18
                tmp = 0
19
            sum_of_commands += tmp
20
    return sum_of_commands
21
22
23
if __name__ == '__main__':
24
    # These "asserts" using only for self-checking and not necessary for
25
    # auto-testing
26
    assert (
27
        digit_stack(
28
            [
29
                "PUSH 3",
30
                "POP",
31
                "POP",
32
                "PUSH 4",
33
                "PEEK",
34
                "PUSH 9",
35
                "PUSH 0",
36
                "PEEK",
37
                "POP",
38
                "PUSH 1",
39
                "PEEK",
40
            ]
41
        )
42
        == 8
43
    ), "Example"
44
    assert digit_stack(["POP", "POP"]) == 0, "pop, pop, zero"
45
    assert digit_stack(["PUSH 9", "PUSH 9", "POP"]) == 9, "Push the button"
46
    assert digit_stack([]) == 0, "Nothing"
47