Skip to content

Commit 5e44bc6

Browse files
committed
Exercises - Reinforcement completed.
1 parent df7b14d commit 5e44bc6

File tree

7 files changed

+304
-79
lines changed

7 files changed

+304
-79
lines changed

.idea/workspace.xml

+147-79
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import stackarray as Stack
2+
3+
def transfer(S, T):
4+
"""Transfer the contents of stack of S to stack T"""
5+
6+
while not S.is_empty():
7+
T.push(S.pop())
8+
9+
10+
# Testing
11+
if __name__ == "__main__":
12+
S = Stack.ArrayStack()
13+
T = Stack.ArrayStack()
14+
15+
S.push(12)
16+
S.push(10)
17+
S.push(25)
18+
print(S)
19+
print(T)
20+
transfer(S,T)
21+
print(T)
22+
23+
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import stackarray as Stack
2+
3+
def empty_stack(S):
4+
"""Empty the stack"""
5+
6+
if S.is_empty():
7+
return
8+
else:
9+
print('Item popped: ',S.pop())
10+
empty_stack(S)
11+
12+
13+
# Testing
14+
if __name__ == "__main__":
15+
S = Stack.ArrayStack()
16+
S.push(12)
17+
S.push(10)
18+
S.push(25)
19+
print(S)
20+
empty_stack(S)
21+
print("Length of stack S: ",len(S))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import stackarray as Stack
2+
3+
def reverse_list(data):
4+
"""Reverse the elements of list using stack"""
5+
6+
S = Stack.ArrayStack()
7+
for i in range(len(data)):
8+
S.push(data[i])
9+
for i in range(len(data)):
10+
data[i] = S.pop()
11+
12+
# Testing
13+
if __name__ == "__main__":
14+
data_set = [1,2,3,4,5,6,7]
15+
print(data_set)
16+
reverse_list(data_set)
17+
print(data_set)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from collections import deque
2+
3+
class Empty(Exception):
4+
"""Error attempting to access an element from an empty container"""
5+
pass
6+
7+
class DequeQueue:
8+
"""FIFO Queue implementation using collections.deque as underlying storage"""
9+
10+
def __init__(self):
11+
self._data = deque()
12+
13+
def __len__(self):
14+
return len(self._data)
15+
16+
def is_empty(self):
17+
return len(self._data) == 0
18+
19+
def first(self):
20+
if self.is_empty():
21+
raise Empty('Queue is empty')
22+
return self._data[0]
23+
24+
def dequeue(self):
25+
if self.is_empty():
26+
raise Empty('Queue is empty')
27+
return self._data.popleft()
28+
29+
def enqueue(self,element):
30+
self._data.append(element)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Empty(Exception):
2+
"""Error attempting to access an element from an empty container"""
3+
pass
4+
5+
class ArrayStack:
6+
"""LIFO Stack implementation using a Python list as underlying storage."""
7+
8+
def __init__(self):
9+
"""Create an empty stack."""
10+
self._data = []
11+
12+
def __len__(self):
13+
"""Return the number of elements in the stack."""
14+
return len(self._data)
15+
16+
def is_empty(self):
17+
"""Return True if stack is empty."""
18+
return len(self._data) == 0
19+
20+
def push(self,e):
21+
"""Add element e to the top of the stack."""
22+
self._data.append(e)
23+
24+
def top(self):
25+
"""Return the element at the top of the stack.
26+
27+
Raise empty exception if the stack is empty.
28+
"""
29+
if self.is_empty():
30+
raise Empty('Stack is empty.')
31+
return self._data[-1]
32+
33+
def pop(self):
34+
"""Remove and return the element from the top of the Stack.
35+
36+
Raise empty exception if the stack is empty.
37+
"""
38+
if self.is_empty():
39+
raise Empty('Stack is empty.')
40+
return self._data.pop()
41+
42+
def __str__(self):
43+
"""Prints the elements of stack"""
44+
result = ''.join(str(self._data[i]) + " " for i in range(len(self._data)))
45+
return result
46+
47+
48+
49+
# Testing
50+
if __name__ == "__main__":
51+
S = ArrayStack()
52+
S.push(5)
53+
S.push(3)
54+
print(len(S))
55+
print(S.pop())
56+
print(S.is_empty())
57+
print(S.pop())
58+
print(S.is_empty())
59+
S.push(7)
60+
S.push(9)
61+
print(S.top())
62+
S.push(4)
63+
print(len(S))
64+
print(S.pop())
65+
S.push(6)

0 commit comments

Comments
 (0)