Add heap and stack to test (#245) · wxpython/algorithms@69887e4 · GitHub
Skip to content

Commit 69887e4

Browse files
author
Hai Hoang Dang
authored
Add heap and stack to test (keon#245)
* Add test_stack io tests * Add test_heap to test
1 parent 88dd56a commit 69887e4

13 files changed

Lines changed: 185 additions & 236 deletions

heap/__init__.py

Whitespace-only changes.

heap/binary_heap.py

Lines changed: 0 additions & 35 deletions

heap/skyline.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
into one in the final output as such: [...[2 3], [4 5], [12 7], ...]
3535
3636
"""
37-
3837
import heapq
3938

4039
def get_skyline(lrh):
@@ -59,7 +58,3 @@ def get_skyline(lrh):
5958
if not skyline or height != skyline[-1][1]:
6059
skyline += [x, height],
6160
return skyline
62-
63-
buildings = [ [2, 9, 10], [3, 7, 15], [5, 12, 12], [15, 20, 10], [19, 24, 8] ]
64-
# [ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ]
65-
print(get_skyline(buildings))

stack/is_consecutive.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
first_is_consecutive: it uses a single stack as auxiliary storage
1717
second_is_consecutive: it uses a single queue as auxiliary storage
1818
"""
19-
import unittest
2019
import collections
2120

2221
def first_is_consecutive(stack):
@@ -57,19 +56,3 @@ def second_is_consecutive(stack):
5756
stack.append(q.pop())
5857

5958
return True
60-
61-
class TestSuite(unittest.TestCase):
62-
"""
63-
test suite for the function (above)
64-
"""
65-
def test_is_consecutive(self):
66-
self.assertTrue(first_is_consecutive([3, 4, 5, 6, 7]))
67-
self.assertFalse(first_is_consecutive([3, 4, 6, 7]))
68-
self.assertFalse(first_is_consecutive([3, 2, 1]))
69-
70-
self.assertTrue(second_is_consecutive([3, 4, 5, 6, 7]))
71-
self.assertFalse(second_is_consecutive([3, 4, 6, 7]))
72-
self.assertFalse(second_is_consecutive([3, 2, 1]))
73-
74-
if __name__ == "__main__":
75-
unittest.main()

stack/is_sorted.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
bottom [1, 2, 3, 4, 5, 6] top
1010
The function should return true
1111
"""
12-
import unittest
13-
1412
def is_sorted(stack):
1513
storage_stack = []
1614
for i in range(len(stack)):
@@ -30,15 +28,3 @@ def is_sorted(stack):
3028
stack.append(storage_stack.pop())
3129

3230
return True
33-
34-
class TestSuite(unittest.TestCase):
35-
"""
36-
test suite for the function (above)
37-
"""
38-
def test_stutter(self):
39-
# Test case: bottom [6, 3, 5, 1, 2, 4] top
40-
self.assertFalse(is_sorted([6, 3, 5, 1, 2, 4]))
41-
self.assertTrue(is_sorted([1, 2, 3, 4, 5, 6]))
42-
43-
if __name__ == "__main__":
44-
unittest.main()

stack/remove_min.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
bottom [2, 8, 3, 7, 3] top
99
1010
"""
11-
import unittest
12-
1311
def remove_min(stack):
1412
storage_stack = []
1513
if len(stack) == 0: # Stack is empty
@@ -28,16 +26,3 @@ def remove_min(stack):
2826
if val != min:
2927
stack.append(val)
3028
return stack
31-
32-
class TestSuite(unittest.TestCase):
33-
"""
34-
test suite for the function (above)
35-
"""
36-
def test_stutter(self):
37-
# Test case: bottom [2, 8, 3, -6, 7, 3] top
38-
self.assertEqual([2, 8, 3, 7, 3], remove_min([2, 8, 3, -6, 7, 3]))
39-
# Test case: 2 smallest value [2, 8, 3, 7, 3]
40-
self.assertEqual([4, 8, 7], remove_min([4, 8, 3, 7, 3]))
41-
42-
if __name__ == "__main__":
43-
unittest.main()

stack/simplify_path.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
* Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
1111
In this case, you should ignore redundant slashes and return "/home/foo".
1212
"""
13-
14-
1513
def simplify_path(path):
1614
"""
1715
:type path: str
@@ -27,10 +25,3 @@ def simplify_path(path):
2725
elif tok not in skip:
2826
stack.append(tok)
2927
return '/' + '/'.join(stack)
30-
31-
32-
if __name__ == '__main__':
33-
34-
p = '/my/name/is/..//keon'
35-
print(p)
36-
print(simplify_path(p))

stack/stack.py

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
size() returns the number of items on the stack.
1414
It needs no parameters and returns an integer.
1515
"""
16-
import unittest
1716
from abc import ABCMeta, abstractmethod
18-
19-
2017
class AbstractStack(metaclass=ABCMeta):
2118
"""Abstract Class for Stacks."""
2219
def __init__(self):
@@ -133,84 +130,3 @@ def peek(self):
133130
if self.is_empty():
134131
raise IndexError("Stack is empty")
135132
return self.head.value
136-
137-
# optional
138-
"""
139-
def is_empty(self):
140-
return self.head is None
141-
"""
142-
143-
144-
class TestSuite(unittest.TestCase):
145-
"""
146-
Test suite for the stack data structures (above)
147-
"""
148-
149-
def test_ArrayStack(self):
150-
stack = ArrayStack()
151-
stack.push(1)
152-
stack.push(2)
153-
stack.push(3)
154-
155-
# test __iter__()
156-
it = iter(stack)
157-
self.assertEqual(3, next(it))
158-
self.assertEqual(2, next(it))
159-
self.assertEqual(1, next(it))
160-
self.assertRaises(StopIteration, next, it)
161-
162-
# test __len__()
163-
self.assertEqual(3, len(stack))
164-
165-
# test __str__()
166-
self.assertEqual(str(stack), "Top-> 3 2 1")
167-
168-
# test is_empty()
169-
self.assertFalse(stack.is_empty())
170-
171-
# test peek()
172-
self.assertEqual(3, stack.peek())
173-
174-
# test pop()
175-
self.assertEqual(3, stack.pop())
176-
self.assertEqual(2, stack.pop())
177-
self.assertEqual(1, stack.pop())
178-
179-
self.assertTrue(stack.is_empty())
180-
181-
def test_LinkedListStack(self):
182-
stack = LinkedListStack()
183-
184-
stack.push(1)
185-
stack.push(2)
186-
stack.push(3)
187-
188-
# test __iter__()
189-
it = iter(stack)
190-
self.assertEqual(3, next(it))
191-
self.assertEqual(2, next(it))
192-
self.assertEqual(1, next(it))
193-
self.assertRaises(StopIteration, next, it)
194-
195-
# test __len__()
196-
self.assertEqual(3, len(stack))
197-
198-
# test __str__()
199-
self.assertEqual(str(stack), "Top-> 3 2 1")
200-
201-
# test is_empty()
202-
self.assertFalse(stack.is_empty())
203-
204-
# test peek()
205-
self.assertEqual(3, stack.peek())
206-
207-
# test pop()
208-
self.assertEqual(3, stack.pop())
209-
self.assertEqual(2, stack.pop())
210-
self.assertEqual(1, stack.pop())
211-
212-
self.assertTrue(stack.is_empty())
213-
214-
215-
if __name__ == "__main__":
216-
unittest.main()

stack/stutter.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
first_stutter: it uses a single stack as auxiliary storage
1212
second_stutter: it uses a single queue as auxiliary storage
1313
"""
14-
import unittest
1514
import collections
1615

1716
def first_stutter(stack):
@@ -43,16 +42,3 @@ def second_stutter(stack):
4342
stack.append(val)
4443

4544
return stack
46-
class TestSuite(unittest.TestCase):
47-
"""
48-
test suite for the function (above)
49-
"""
50-
def test_stutter(self):
51-
# Test case: bottom [3, 7, 1, 14, 9] top
52-
self.assertEqual([3, 3, 7, 7, 1, 1, 14, 14, 9, 9],
53-
first_stutter([3, 7, 1, 14, 9]))
54-
self.assertEqual([3, 3, 7, 7, 1, 1, 14, 14, 9, 9],
55-
second_stutter([3, 7, 1, 14, 9]))
56-
57-
if __name__ == "__main__":
58-
unittest.main()

stack/switch_pairs.py

Lines changed: 0 additions & 22 deletions

0 commit comments

Comments
 (0)