-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathstack.py
More file actions
70 lines (50 loc) · 1.76 KB
/
Copy pathstack.py
File metadata and controls
70 lines (50 loc) · 1.76 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
#!/usr/bin/env python
"""
Generic stack type for Python.
Copyright (C) Sarah Mount, 2010.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have rceeived a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
__all__ = ['Stack']
class Stack:
def __init__(self):
self.__stack = []
return
def push(self, value):
self.__stack.append(value)
return
def pop(self):
assert(len(self.__stack) > 0)
return self.__stack.pop()
def peek(self):
assert(len(self.__stack) > 0)
return self.__stack[len(self.__stack) - 1]
def issubset(self, other):
"""Determine whether other stack is a subset of this one.
Order matters.
"""
size = min(len(self.__stack), len(other))
for i in range(size):
if not self.__stack[i] == other[i]:
return False
return True
def __contains__(self, item):
return item in self.__stack
def __len__(self):
return len(self.__stack)
def __getitem__(self, index):
return self.__stack[index]
def __iter__(self):
return self.__stack.__iter__()
def __repr__(self):
return self.__stack.__repr__()
def __str__(self):
return self.__stack.__str__()