Skip to content

Commit 37c3070

Browse files
committed
Merge remote-tracking branch 'upstream/data_structures' into undirected_adjacency_matrix
Conflicts: algorithms/tests/test_data_structures.py
2 parents 109a592 + 6cda5b0 commit 37c3070

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Stacks are cool
3+
"""
4+
5+
class EmptyStackError(Exception):
6+
pass
7+
8+
class Element(object):
9+
def __init__(self, value, next):
10+
self.value = value
11+
self.next = next
12+
13+
14+
class Stack(object):
15+
def __init__(self):
16+
self.head = None
17+
18+
def push(self, element):
19+
self.head = Element(element, self.head)
20+
21+
def pop(self):
22+
if self.empty():
23+
raise EmptyStackError
24+
result = self.head.value
25+
self.head = self.head.next
26+
return result
27+
28+
def empty(self):
29+
return self.head == None

algorithms/tests/test_data_structures.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
""" Unit Tests for data structures """
22
import unittest
3-
from ..data_structures import undirected_adjacency_matrix
3+
from ..data_structures import undirected_adjacency_matrix, Stack, EmptyStackError
44

55

66
class TestUndirectedAdjacencyMatrix(unittest.TestCase):
@@ -73,3 +73,26 @@ def test_undirected_adjacency_matrix(self):
7373
for i in range(0, 20, 2):
7474
self.graph.removeVertex(i)
7575
self.assertEqual(self.expected[5], self.graph.edge)
76+
77+
78+
class TestStack(unittest.TestCase):
79+
"""
80+
Tests the methods of the Stack data structure.
81+
"""
82+
83+
def setUp(self):
84+
self.input = range(5)
85+
self.stack = Stack()
86+
self.reverse = [4, 3, 2, 1, 0]
87+
88+
def test_stack(self):
89+
for i in self.input:
90+
self.stack.push(i)
91+
92+
result = []
93+
94+
while not self.stack.empty():
95+
result.append(self.stack.pop())
96+
97+
self.assertEqual(self.reverse, result)
98+
self.assertRaises(EmptyStackError, self.stack.pop)

0 commit comments

Comments
 (0)