-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_listas.py
More file actions
73 lines (48 loc) · 1.59 KB
/
stack_listas.py
File metadata and controls
73 lines (48 loc) · 1.59 KB
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
from typing import Optional, Union
from typing import Iterable
class Stack:
"""A simple stack implementation with list."""
_items: list
def __init__(self, *items):
self._items = list(*items)
def is_empty(self) -> bool:
return len(self._items) == 0
def clear(self) -> None:
self._items.clear()
def copy(self) -> 'Stack':
stack = Stack()
for item in self._items:
stack.push(item)
return stack
def extend(self, other: Iterable) -> None:
for item in reversed(other):
self.push(item)
def peek(self) -> Optional[object]:
if self.is_empty():
raise ValueError('Stack is empty.')
return self._items[-1]
def pop(self) -> Optional[object]:
return self._items.pop()
def push(self, item: object) -> None:
self._items.append(item)
def itervalues(self) -> Iterable:
return reversed(self._items)
def __len__(self) -> int:
return len(self._items)
def __str__(self) -> str:
return str(self._items)
def __contains__(self, item: object) -> bool:
return item in self._items
def __iter__(self) -> Iterable:
return self.itervalues()
def __repr__(self) -> str:
return f'Stack({self._items})'
def __reversed__(self) -> Iterable:
return self._items
def __add__(self, other: Union['Stack', Iterable]) -> 'Stack':
stack = self.copy()
if isinstance(other, Stack):
stack.extend(other)
else:
stack.extend(other)
return stack