-
Notifications
You must be signed in to change notification settings - Fork 39
/
problem_43.py
46 lines (40 loc) · 1.18 KB
/
problem_43.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def coding_problem_43():
"""
Implement a stack that has the following methods:
push(val), which pushes an element onto the stack
pop(), which pops off and returns the topmost element of the stack.
If there are no elements in the stack, then it should throw an error or return null.
max(), which returns the maximum value in the stack currently.
If there are no elements in the stack, then it should throw an error or return null.
Each method should run in constant time.
>>> max_stack = coding_problem_43()
>>> max_stack.push(1)
>>> max_stack.max()
1
>>> max_stack.push(3)
>>> max_stack.max()
3
>>> max_stack.push(2)
>>> max_stack.max()
3
>>> max_stack.pop()
2
>>> max_stack.max()
3
>>> _ = max_stack.pop()
>>> max_stack.max()
1
>>> _ = max_stack.pop()
>>> max_stack.max()
Traceback (most recent call last):
...
IndexError: list index out of range
>>> _ = max_stack.pop()
Traceback (most recent call last):
...
IndexError: pop from empty list
"""
pass
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=True)