-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.py
51 lines (43 loc) · 1.47 KB
/
stack.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
# -*- coding: utf-8 -*-
#
# Stack Class for Towers of Hanoi puzzle
# Copyright (C) 2016 August
# 1200 Web Development
# http://1200wd.com/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
class Stack:
def __init__(self, start=[]):
self.stack = []
for i in start: self.push(i)
def push(self, node):
self.stack.append(node)
def pop(self):
if not self.stack: return False
return self.stack.pop()
def top(self):
if not self.stack: return False
return self.stack[-1]
def __repr__(self):
res = "|"
for i in self.stack:
res += str(i) + '-'
return res
def __len__(self):
return len(self.stack)
def __getitem__(self, offset):
return self.stack[offset]
def __eq__(self, other):
return self.stack == other.stack