-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path7.py
89 lines (71 loc) · 2.25 KB
/
7.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import enum
import sys
from typing import (
List,
Optional,
Tuple,
NoReturn,
)
class StackMax:
def __init__(self):
self.data: List[int] = []
self._max_value_indexes: List[int] = []
@property
def _max_value_index(self):
return self._max_value_indexes[-1] if self._max_value_indexes else None
@_max_value_index.setter
def _max_value_index(self, index):
self._max_value_indexes.append(index)
@_max_value_index.deleter
def _max_value_index(self):
self._max_value_indexes.pop()
def push(self, number: int):
self.data.append(number)
if (
self._max_value_index is None
or number > self.data[self._max_value_index]
):
self._max_value_index = len(self.data) - 1
def pop(self) -> Optional[NoReturn]:
idx: int = len(self.data) - 1
self.data.pop()
if self._max_value_index is not None and self._max_value_index == idx:
del self._max_value_index
def get_max(self) -> Optional[int]:
if self._max_value_index is None:
return None
return self.data[self._max_value_index]
def get_commands(command_length: int) -> List[Tuple[str, Optional[int]]]:
commands: List[Tuple[str, int]] = []
for _ in range(command_length):
data: List[str, Optional[str]] = (
sys.stdin.readline().rstrip().split(" ")
)
command: Tuple[str, Optional[int]] = (
data[0],
int(data[1]) if len(data) > 1 else None,
)
commands.append(command)
return commands
class CommandFuncName(enum.Enum):
get_max = "get_max"
push = "push"
pop = "pop"
def execute_command(stack: StackMax, command: Tuple[str, Optional[int]]):
func = command[0]
number: Optional[int] = command[1]
if func == CommandFuncName.get_max.value:
print(stack.get_max())
elif func == CommandFuncName.push.value:
stack.push(number)
elif func == CommandFuncName.pop.value:
try:
stack.pop()
except IndexError:
print("error")
def main():
stack = StackMax()
length: int = int(input())
for command in get_commands(length):
execute_command(stack, command)
main()