-
Notifications
You must be signed in to change notification settings - Fork 50
/
stack.py
91 lines (70 loc) · 3.07 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
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
90
91
#!python
from linkedlist import LinkedList
# Implement LinkedStack below, then change the assignment at the bottom
# to use this Stack implementation to verify it passes all tests
class LinkedStack(object):
def __init__(self, iterable=None):
"""Initialize this stack and push the given items, if any."""
# Initialize a new linked list to store the items
self.list = LinkedList()
if iterable is not None:
for item in iterable:
self.push(item)
def __repr__(self):
"""Return a string representation of this stack."""
return 'Stack({} items, top={})'.format(self.length(), self.peek())
def is_empty(self):
"""Return True if this stack is empty, or False otherwise."""
# TODO: Check if empty
def length(self):
"""Return the number of items in this stack."""
# TODO: Count number of items
def push(self, item):
"""Insert the given item on the top of this stack.
Running time: O(???) – Why? [TODO]"""
# TODO: Push given item
def peek(self):
"""Return the item on the top of this stack without removing it,
or None if this stack is empty."""
# TODO: Return top item, if any
def pop(self):
"""Remove and return the item on the top of this stack,
or raise ValueError if this stack is empty.
Running time: O(???) – Why? [TODO]"""
# TODO: Remove and return top item, if any
# Implement ArrayStack below, then change the assignment at the bottom
# to use this Stack implementation to verify it passes all tests
class ArrayStack(object):
def __init__(self, iterable=None):
"""Initialize this stack and push the given items, if any."""
# Initialize a new list (dynamic array) to store the items
self.list = list()
if iterable is not None:
for item in iterable:
self.push(item)
def __repr__(self):
"""Return a string representation of this stack."""
return 'Stack({} items, top={})'.format(self.length(), self.peek())
def is_empty(self):
"""Return True if this stack is empty, or False otherwise."""
# TODO: Check if empty
def length(self):
"""Return the number of items in this stack."""
# TODO: Count number of items
def push(self, item):
"""Insert the given item on the top of this stack.
Running time: O(???) – Why? [TODO]"""
# TODO: Insert given item
def peek(self):
"""Return the item on the top of this stack without removing it,
or None if this stack is empty."""
# TODO: Return top item, if any
def pop(self):
"""Remove and return the item on the top of this stack,
or raise ValueError if this stack is empty.
Running time: O(???) – Why? [TODO]"""
# TODO: Remove and return top item, if any
# Implement LinkedStack and ArrayStack above, then change the assignment below
# to use each of your Stack implementations to verify they each pass all tests
Stack = LinkedStack
# Stack = ArrayStack