mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
parent
0e7ed80228
commit
139e157250
|
@ -80,9 +80,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Stacks(object):\n",
|
||||
|
@ -118,25 +116,20 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_n_stacks.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"from nose.tools import raises\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestStacks(object):\n",
|
||||
"class TestStacks(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" @raises(Exception)\n",
|
||||
" def test_pop_on_empty(self, num_stacks, stack_size):\n",
|
||||
" print('Test: Pop on empty stack')\n",
|
||||
" stacks = Stacks(num_stacks, stack_size)\n",
|
||||
" stacks.pop(0)\n",
|
||||
"\n",
|
||||
" @raises(Exception)\n",
|
||||
" def test_push_on_full(self, num_stacks, stack_size):\n",
|
||||
" print('Test: Push to full stack')\n",
|
||||
" stacks = Stacks(num_stacks, stack_size)\n",
|
||||
|
@ -153,10 +146,10 @@
|
|||
" stacks.push(2, 4)\n",
|
||||
"\n",
|
||||
" print('Test: Pop on non-empty stack')\n",
|
||||
" assert_equal(stacks.pop(0), 2)\n",
|
||||
" assert_equal(stacks.pop(0), 1)\n",
|
||||
" assert_equal(stacks.pop(1), 3)\n",
|
||||
" assert_equal(stacks.pop(2), 4)\n",
|
||||
" self.assertEqual(stacks.pop(0), 2)\n",
|
||||
" self.assertEqual(stacks.pop(0), 1)\n",
|
||||
" self.assertEqual(stacks.pop(1), 3)\n",
|
||||
" self.assertEqual(stacks.pop(2), 4)\n",
|
||||
"\n",
|
||||
" print('Success: test_stacks\\n')\n",
|
||||
"\n",
|
||||
|
@ -165,8 +158,10 @@
|
|||
" num_stacks = 3\n",
|
||||
" stack_size = 100\n",
|
||||
" test = TestStacks()\n",
|
||||
" test.test_pop_on_empty(num_stacks, stack_size)\n",
|
||||
" test.test_push_on_full(num_stacks, stack_size)\n",
|
||||
" test.assertRaises(Exception, test.test_pop_on_empty, num_stacks,\n",
|
||||
" stack_size)\n",
|
||||
" test.assertRaises(Exception, test.test_push_on_full, num_stacks,\n",
|
||||
" stack_size)\n",
|
||||
" test.test_stacks(num_stacks, stack_size)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -200,9 +195,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -110,9 +110,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Stacks(object):\n",
|
||||
|
@ -154,9 +152,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -168,19 +164,16 @@
|
|||
],
|
||||
"source": [
|
||||
"%%writefile test_n_stacks.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"from nose.tools import raises\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestStacks(object):\n",
|
||||
"class TestStacks(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" @raises(Exception)\n",
|
||||
" def test_pop_on_empty(self, num_stacks, stack_size):\n",
|
||||
" print('Test: Pop on empty stack')\n",
|
||||
" stacks = Stacks(num_stacks, stack_size)\n",
|
||||
" stacks.pop(0)\n",
|
||||
"\n",
|
||||
" @raises(Exception)\n",
|
||||
" def test_push_on_full(self, num_stacks, stack_size):\n",
|
||||
" print('Test: Push to full stack')\n",
|
||||
" stacks = Stacks(num_stacks, stack_size)\n",
|
||||
|
@ -197,10 +190,10 @@
|
|||
" stacks.push(2, 4)\n",
|
||||
"\n",
|
||||
" print('Test: Pop on non-empty stack')\n",
|
||||
" assert_equal(stacks.pop(0), 2)\n",
|
||||
" assert_equal(stacks.pop(0), 1)\n",
|
||||
" assert_equal(stacks.pop(1), 3)\n",
|
||||
" assert_equal(stacks.pop(2), 4)\n",
|
||||
" self.assertEqual(stacks.pop(0), 2)\n",
|
||||
" self.assertEqual(stacks.pop(0), 1)\n",
|
||||
" self.assertEqual(stacks.pop(1), 3)\n",
|
||||
" self.assertEqual(stacks.pop(2), 4)\n",
|
||||
"\n",
|
||||
" print('Success: test_stacks\\n')\n",
|
||||
"\n",
|
||||
|
@ -209,8 +202,10 @@
|
|||
" num_stacks = 3\n",
|
||||
" stack_size = 100\n",
|
||||
" test = TestStacks()\n",
|
||||
" test.test_pop_on_empty(num_stacks, stack_size)\n",
|
||||
" test.test_push_on_full(num_stacks, stack_size)\n",
|
||||
" test.assertRaises(Exception, test.test_pop_on_empty, num_stacks,\n",
|
||||
" stack_size)\n",
|
||||
" test.assertRaises(Exception, test.test_push_on_full, num_stacks,\n",
|
||||
" stack_size)\n",
|
||||
" test.test_stacks(num_stacks, stack_size)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -221,9 +216,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -241,6 +234,13 @@
|
|||
"source": [
|
||||
"run -i test_n_stacks.py"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
@ -259,9 +259,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -1,16 +1,13 @@
|
|||
from nose.tools import assert_equal
|
||||
from nose.tools import raises
|
||||
import unittest
|
||||
|
||||
|
||||
class TestStacks(object):
|
||||
class TestStacks(unittest.TestCase):
|
||||
|
||||
@raises(Exception)
|
||||
def test_pop_on_empty(self, num_stacks, stack_size):
|
||||
print('Test: Pop on empty stack')
|
||||
stacks = Stacks(num_stacks, stack_size)
|
||||
stacks.pop(0)
|
||||
|
||||
@raises(Exception)
|
||||
def test_push_on_full(self, num_stacks, stack_size):
|
||||
print('Test: Push to full stack')
|
||||
stacks = Stacks(num_stacks, stack_size)
|
||||
|
@ -27,10 +24,10 @@ class TestStacks(object):
|
|||
stacks.push(2, 4)
|
||||
|
||||
print('Test: Pop on non-empty stack')
|
||||
assert_equal(stacks.pop(0), 2)
|
||||
assert_equal(stacks.pop(0), 1)
|
||||
assert_equal(stacks.pop(1), 3)
|
||||
assert_equal(stacks.pop(2), 4)
|
||||
self.assertEqual(stacks.pop(0), 2)
|
||||
self.assertEqual(stacks.pop(0), 1)
|
||||
self.assertEqual(stacks.pop(1), 3)
|
||||
self.assertEqual(stacks.pop(2), 4)
|
||||
|
||||
print('Success: test_stacks\n')
|
||||
|
||||
|
@ -39,10 +36,12 @@ def main():
|
|||
num_stacks = 3
|
||||
stack_size = 100
|
||||
test = TestStacks()
|
||||
test.test_pop_on_empty(num_stacks, stack_size)
|
||||
test.test_push_on_full(num_stacks, stack_size)
|
||||
test.assertRaises(Exception, test.test_pop_on_empty, num_stacks,
|
||||
stack_size)
|
||||
test.assertRaises(Exception, test.test_push_on_full, num_stacks,
|
||||
stack_size)
|
||||
test.test_stacks(num_stacks, stack_size)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
|
|
@ -77,9 +77,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run ../stack/stack.py\n",
|
||||
|
@ -89,9 +87,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class QueueFromStacks(object):\n",
|
||||
|
@ -124,21 +120,19 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_queue_from_stacks.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestQueueFromStacks(object):\n",
|
||||
"class TestQueueFromStacks(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_queue_from_stacks(self):\n",
|
||||
" print('Test: Dequeue on empty stack')\n",
|
||||
" queue = QueueFromStacks()\n",
|
||||
" assert_equal(queue.dequeue(), None)\n",
|
||||
" self.assertEqual(queue.dequeue(), None)\n",
|
||||
"\n",
|
||||
" print('Test: Enqueue on empty stack')\n",
|
||||
" print('Test: Enqueue on non-empty stack')\n",
|
||||
|
@ -149,15 +143,15 @@
|
|||
"\n",
|
||||
" print('Test: Dequeue on non-empty stack')\n",
|
||||
" print('Test: Dequeue after an enqueue')\n",
|
||||
" assert_equal(queue.dequeue(), 0)\n",
|
||||
" self.assertEqual(queue.dequeue(), 0)\n",
|
||||
"\n",
|
||||
" print('Test: Multiple dequeue in a row')\n",
|
||||
" assert_equal(queue.dequeue(), 1)\n",
|
||||
" assert_equal(queue.dequeue(), 2)\n",
|
||||
" self.assertEqual(queue.dequeue(), 1)\n",
|
||||
" self.assertEqual(queue.dequeue(), 2)\n",
|
||||
"\n",
|
||||
" print('Test: Enqueue after a dequeue')\n",
|
||||
" queue.enqueue(5)\n",
|
||||
" assert_equal(queue.dequeue(), 5)\n",
|
||||
" self.assertEqual(queue.dequeue(), 5)\n",
|
||||
"\n",
|
||||
" print('Success: test_queue_from_stacks')\n",
|
||||
"\n",
|
||||
|
@ -197,9 +191,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -107,9 +107,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run ../stack/stack.py"
|
||||
|
@ -118,9 +116,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class QueueFromStacks(object):\n",
|
||||
|
@ -153,9 +149,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -167,15 +161,15 @@
|
|||
],
|
||||
"source": [
|
||||
"%%writefile test_queue_from_stacks.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestQueueFromStacks(object):\n",
|
||||
"class TestQueueFromStacks(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_queue_from_stacks(self):\n",
|
||||
" print('Test: Dequeue on empty stack')\n",
|
||||
" queue = QueueFromStacks()\n",
|
||||
" assert_equal(queue.dequeue(), None)\n",
|
||||
" self.assertEqual(queue.dequeue(), None)\n",
|
||||
"\n",
|
||||
" print('Test: Enqueue on empty stack')\n",
|
||||
" print('Test: Enqueue on non-empty stack')\n",
|
||||
|
@ -186,15 +180,15 @@
|
|||
"\n",
|
||||
" print('Test: Dequeue on non-empty stack')\n",
|
||||
" print('Test: Dequeue after an enqueue')\n",
|
||||
" assert_equal(queue.dequeue(), 0)\n",
|
||||
" self.assertEqual(queue.dequeue(), 0)\n",
|
||||
"\n",
|
||||
" print('Test: Multiple dequeue in a row')\n",
|
||||
" assert_equal(queue.dequeue(), 1)\n",
|
||||
" assert_equal(queue.dequeue(), 2)\n",
|
||||
" self.assertEqual(queue.dequeue(), 1)\n",
|
||||
" self.assertEqual(queue.dequeue(), 2)\n",
|
||||
"\n",
|
||||
" print('Test: Enqueue after a dequeue')\n",
|
||||
" queue.enqueue(5)\n",
|
||||
" assert_equal(queue.dequeue(), 5)\n",
|
||||
" self.assertEqual(queue.dequeue(), 5)\n",
|
||||
"\n",
|
||||
" print('Success: test_queue_from_stacks')\n",
|
||||
"\n",
|
||||
|
@ -211,9 +205,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -252,9 +244,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
from nose.tools import assert_equal
|
||||
import unittest
|
||||
|
||||
|
||||
class TestQueueFromStacks(object):
|
||||
class TestQueueFromStacks(unittest.TestCase):
|
||||
|
||||
def test_queue_from_stacks(self):
|
||||
print('Test: Dequeue on empty stack')
|
||||
queue = QueueFromStacks()
|
||||
assert_equal(queue.dequeue(), None)
|
||||
self.assertEqual(queue.dequeue(), None)
|
||||
|
||||
print('Test: Enqueue on empty stack')
|
||||
print('Test: Enqueue on non-empty stack')
|
||||
|
@ -17,15 +17,15 @@ class TestQueueFromStacks(object):
|
|||
|
||||
print('Test: Dequeue on non-empty stack')
|
||||
print('Test: Dequeue after an enqueue')
|
||||
assert_equal(queue.dequeue(), 0)
|
||||
self.assertEqual(queue.dequeue(), 0)
|
||||
|
||||
print('Test: Multiple dequeue in a row')
|
||||
assert_equal(queue.dequeue(), 1)
|
||||
assert_equal(queue.dequeue(), 2)
|
||||
self.assertEqual(queue.dequeue(), 1)
|
||||
self.assertEqual(queue.dequeue(), 2)
|
||||
|
||||
print('Test: Enqueue after a dequeue')
|
||||
queue.enqueue(5)
|
||||
assert_equal(queue.dequeue(), 5)
|
||||
self.assertEqual(queue.dequeue(), 5)
|
||||
|
||||
print('Success: test_queue_from_stacks')
|
||||
|
||||
|
@ -36,4 +36,4 @@ def main():
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
|
|
@ -32,4 +32,4 @@ class Queue(object):
|
|||
self.tail = None
|
||||
else:
|
||||
self.head = self.head.next
|
||||
return data
|
||||
return data
|
||||
|
|
|
@ -82,9 +82,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Node(object):\n",
|
||||
|
@ -123,29 +121,27 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_queue_list.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestQueue(object):\n",
|
||||
"class TestQueue(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" # TODO: It would be better if we had unit tests for each\n",
|
||||
" # method in addition to the following end-to-end test\n",
|
||||
" def test_end_to_end(self):\n",
|
||||
" print('Test: Dequeue an empty queue')\n",
|
||||
" queue = Queue()\n",
|
||||
" assert_equal(queue.dequeue(), None)\n",
|
||||
" self.assertEqual(queue.dequeue(), None)\n",
|
||||
"\n",
|
||||
" print('Test: Enqueue to an empty queue')\n",
|
||||
" queue.enqueue(1)\n",
|
||||
"\n",
|
||||
" print('Test: Dequeue a queue with one element')\n",
|
||||
" assert_equal(queue.dequeue(), 1)\n",
|
||||
" self.assertEqual(queue.dequeue(), 1)\n",
|
||||
"\n",
|
||||
" print('Test: Enqueue to a non-empty queue')\n",
|
||||
" queue.enqueue(2)\n",
|
||||
|
@ -153,9 +149,9 @@
|
|||
" queue.enqueue(4)\n",
|
||||
"\n",
|
||||
" print('Test: Dequeue a queue with more than one element')\n",
|
||||
" assert_equal(queue.dequeue(), 2)\n",
|
||||
" assert_equal(queue.dequeue(), 3)\n",
|
||||
" assert_equal(queue.dequeue(), 4)\n",
|
||||
" self.assertEqual(queue.dequeue(), 2)\n",
|
||||
" self.assertEqual(queue.dequeue(), 3)\n",
|
||||
" self.assertEqual(queue.dequeue(), 4)\n",
|
||||
"\n",
|
||||
" print('Success: test_end_to_end')\n",
|
||||
"\n",
|
||||
|
@ -195,9 +191,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -104,9 +104,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -158,9 +156,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run queue_list.py"
|
||||
|
@ -177,9 +173,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -191,23 +185,23 @@
|
|||
],
|
||||
"source": [
|
||||
"%%writefile test_queue_list.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestQueue(object):\n",
|
||||
"class TestQueue(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" # TODO: It would be better if we had unit tests for each\n",
|
||||
" # method in addition to the following end-to-end test\n",
|
||||
" def test_end_to_end(self):\n",
|
||||
" print('Test: Dequeue an empty queue')\n",
|
||||
" queue = Queue()\n",
|
||||
" assert_equal(queue.dequeue(), None)\n",
|
||||
" self.assertEqual(queue.dequeue(), None)\n",
|
||||
"\n",
|
||||
" print('Test: Enqueue to an empty queue')\n",
|
||||
" queue.enqueue(1)\n",
|
||||
"\n",
|
||||
" print('Test: Dequeue a queue with one element')\n",
|
||||
" assert_equal(queue.dequeue(), 1)\n",
|
||||
" self.assertEqual(queue.dequeue(), 1)\n",
|
||||
"\n",
|
||||
" print('Test: Enqueue to a non-empty queue')\n",
|
||||
" queue.enqueue(2)\n",
|
||||
|
@ -215,9 +209,9 @@
|
|||
" queue.enqueue(4)\n",
|
||||
"\n",
|
||||
" print('Test: Dequeue a queue with more than one element')\n",
|
||||
" assert_equal(queue.dequeue(), 2)\n",
|
||||
" assert_equal(queue.dequeue(), 3)\n",
|
||||
" assert_equal(queue.dequeue(), 4)\n",
|
||||
" self.assertEqual(queue.dequeue(), 2)\n",
|
||||
" self.assertEqual(queue.dequeue(), 3)\n",
|
||||
" self.assertEqual(queue.dequeue(), 4)\n",
|
||||
"\n",
|
||||
" print('Success: test_end_to_end')\n",
|
||||
"\n",
|
||||
|
@ -234,9 +228,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -303,9 +295,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
from nose.tools import assert_equal
|
||||
import unittest
|
||||
|
||||
|
||||
class TestQueue(object):
|
||||
class TestQueue(unittest.TestCase):
|
||||
|
||||
# TODO: It would be better if we had unit tests for each
|
||||
# method in addition to the following end-to-end test
|
||||
def test_end_to_end(self):
|
||||
print('Test: Dequeue an empty queue')
|
||||
queue = Queue()
|
||||
assert_equal(queue.dequeue(), None)
|
||||
self.assertEqual(queue.dequeue(), None)
|
||||
|
||||
print('Test: Enqueue to an empty queue')
|
||||
queue.enqueue(1)
|
||||
|
||||
print('Test: Dequeue a queue with one element')
|
||||
assert_equal(queue.dequeue(), 1)
|
||||
self.assertEqual(queue.dequeue(), 1)
|
||||
|
||||
print('Test: Enqueue to a non-empty queue')
|
||||
queue.enqueue(2)
|
||||
|
@ -22,9 +22,9 @@ class TestQueue(object):
|
|||
queue.enqueue(4)
|
||||
|
||||
print('Test: Dequeue a queue with more than one element')
|
||||
assert_equal(queue.dequeue(), 2)
|
||||
assert_equal(queue.dequeue(), 3)
|
||||
assert_equal(queue.dequeue(), 4)
|
||||
self.assertEqual(queue.dequeue(), 2)
|
||||
self.assertEqual(queue.dequeue(), 3)
|
||||
self.assertEqual(queue.dequeue(), 4)
|
||||
|
||||
print('Success: test_end_to_end')
|
||||
|
||||
|
@ -35,4 +35,4 @@ def main():
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
|
|
@ -79,9 +79,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run ../stack/stack.py\n",
|
||||
|
@ -91,9 +89,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class StackWithCapacity(Stack):\n",
|
||||
|
@ -144,16 +140,14 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_set_of_stacks.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestSetOfStacks(object):\n",
|
||||
"class TestSetOfStacks(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_set_of_stacks(self):\n",
|
||||
" print('Test: Push on an empty stack')\n",
|
||||
|
@ -167,14 +161,14 @@
|
|||
" stacks.push('a')\n",
|
||||
"\n",
|
||||
" print('Test: Pop on a stack to destroy it')\n",
|
||||
" assert_equal(stacks.pop(), 'a')\n",
|
||||
" self.assertEqual(stacks.pop(), 'a')\n",
|
||||
"\n",
|
||||
" print('Test: Pop general case')\n",
|
||||
" assert_equal(stacks.pop(), 5)\n",
|
||||
" assert_equal(stacks.pop(), 3)\n",
|
||||
" self.assertEqual(stacks.pop(), 5)\n",
|
||||
" self.assertEqual(stacks.pop(), 3)\n",
|
||||
"\n",
|
||||
" print('Test: Pop on no elements')\n",
|
||||
" assert_equal(stacks.pop(), None)\n",
|
||||
" self.assertEqual(stacks.pop(), None)\n",
|
||||
"\n",
|
||||
" print('Success: test_set_of_stacks')\n",
|
||||
"\n",
|
||||
|
@ -214,9 +208,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -100,9 +100,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run ../stack/stack.py"
|
||||
|
@ -111,9 +109,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class StackWithCapacity(Stack):\n",
|
||||
|
@ -174,9 +170,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -188,10 +182,10 @@
|
|||
],
|
||||
"source": [
|
||||
"%%writefile test_set_of_stacks.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestSetOfStacks(object):\n",
|
||||
"class TestSetOfStacks(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_set_of_stacks(self):\n",
|
||||
" print('Test: Push on an empty stack')\n",
|
||||
|
@ -205,14 +199,14 @@
|
|||
" stacks.push('a')\n",
|
||||
"\n",
|
||||
" print('Test: Pop on a stack to destroy it')\n",
|
||||
" assert_equal(stacks.pop(), 'a')\n",
|
||||
" self.assertEqual(stacks.pop(), 'a')\n",
|
||||
"\n",
|
||||
" print('Test: Pop general case')\n",
|
||||
" assert_equal(stacks.pop(), 5)\n",
|
||||
" assert_equal(stacks.pop(), 3)\n",
|
||||
" self.assertEqual(stacks.pop(), 5)\n",
|
||||
" self.assertEqual(stacks.pop(), 3)\n",
|
||||
"\n",
|
||||
" print('Test: Pop on no elements')\n",
|
||||
" assert_equal(stacks.pop(), None)\n",
|
||||
" self.assertEqual(stacks.pop(), None)\n",
|
||||
"\n",
|
||||
" print('Success: test_set_of_stacks')\n",
|
||||
"\n",
|
||||
|
@ -229,9 +223,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -268,9 +260,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from nose.tools import assert_equal
|
||||
import unittest
|
||||
|
||||
|
||||
class TestSetOfStacks(object):
|
||||
class TestSetOfStacks(unittest.TestCase):
|
||||
|
||||
def test_set_of_stacks(self):
|
||||
print('Test: Push on an empty stack')
|
||||
|
@ -15,14 +15,14 @@ class TestSetOfStacks(object):
|
|||
stacks.push('a')
|
||||
|
||||
print('Test: Pop on a stack to destroy it')
|
||||
assert_equal(stacks.pop(), 'a')
|
||||
self.assertEqual(stacks.pop(), 'a')
|
||||
|
||||
print('Test: Pop general case')
|
||||
assert_equal(stacks.pop(), 5)
|
||||
assert_equal(stacks.pop(), 3)
|
||||
self.assertEqual(stacks.pop(), 5)
|
||||
self.assertEqual(stacks.pop(), 3)
|
||||
|
||||
print('Test: Pop on no elements')
|
||||
assert_equal(stacks.pop(), None)
|
||||
self.assertEqual(stacks.pop(), None)
|
||||
|
||||
print('Success: test_set_of_stacks')
|
||||
|
||||
|
@ -33,4 +33,4 @@ def main():
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
|
|
@ -87,9 +87,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class MyStack(Stack):\n",
|
||||
|
@ -113,17 +111,15 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_sort_stack.py\n",
|
||||
"from random import randint\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestSortStack(object):\n",
|
||||
"class TestSortStack(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def get_sorted_stack(self, stack, numbers):\n",
|
||||
" for x in numbers:\n",
|
||||
|
@ -134,11 +130,11 @@
|
|||
" def test_sort_stack(self, stack):\n",
|
||||
" print('Test: Empty stack')\n",
|
||||
" sorted_stack = self.get_sorted_stack(stack, [])\n",
|
||||
" assert_equal(sorted_stack.pop(), None)\n",
|
||||
" self.assertEqual(sorted_stack.pop(), None)\n",
|
||||
"\n",
|
||||
" print('Test: One element stack')\n",
|
||||
" sorted_stack = self.get_sorted_stack(stack, [1])\n",
|
||||
" assert_equal(sorted_stack.pop(), 1)\n",
|
||||
" self.assertEqual(sorted_stack.pop(), 1)\n",
|
||||
"\n",
|
||||
" print('Test: Two or more element stack (general case)')\n",
|
||||
" num_items = 10\n",
|
||||
|
@ -147,7 +143,7 @@
|
|||
" sorted_numbers = []\n",
|
||||
" for _ in range(num_items):\n",
|
||||
" sorted_numbers.append(sorted_stack.pop())\n",
|
||||
" assert_equal(sorted_numbers, sorted(numbers, reverse=True))\n",
|
||||
" self.assertEqual(sorted_numbers, sorted(numbers, reverse=True))\n",
|
||||
"\n",
|
||||
" print('Success: test_sort_stack')\n",
|
||||
"\n",
|
||||
|
@ -193,9 +189,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -84,9 +84,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run ../stack/stack.py"
|
||||
|
@ -95,9 +93,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class MyStack(Stack):\n",
|
||||
|
@ -125,9 +121,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class MyStackSimplified(Stack):\n",
|
||||
|
@ -153,9 +147,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -168,10 +160,10 @@
|
|||
"source": [
|
||||
"%%writefile test_sort_stack.py\n",
|
||||
"from random import randint\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestSortStack(object):\n",
|
||||
"class TestSortStack(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def get_sorted_stack(self, stack, numbers):\n",
|
||||
" for x in numbers:\n",
|
||||
|
@ -182,11 +174,11 @@
|
|||
" def test_sort_stack(self, stack):\n",
|
||||
" print('Test: Empty stack')\n",
|
||||
" sorted_stack = self.get_sorted_stack(stack, [])\n",
|
||||
" assert_equal(sorted_stack.pop(), None)\n",
|
||||
" self.assertEqual(sorted_stack.pop(), None)\n",
|
||||
"\n",
|
||||
" print('Test: One element stack')\n",
|
||||
" sorted_stack = self.get_sorted_stack(stack, [1])\n",
|
||||
" assert_equal(sorted_stack.pop(), 1)\n",
|
||||
" self.assertEqual(sorted_stack.pop(), 1)\n",
|
||||
"\n",
|
||||
" print('Test: Two or more element stack (general case)')\n",
|
||||
" num_items = 10\n",
|
||||
|
@ -195,7 +187,7 @@
|
|||
" sorted_numbers = []\n",
|
||||
" for _ in range(num_items):\n",
|
||||
" sorted_numbers.append(sorted_stack.pop())\n",
|
||||
" assert_equal(sorted_numbers, sorted(numbers, reverse=True))\n",
|
||||
" self.assertEqual(sorted_numbers, sorted(numbers, reverse=True))\n",
|
||||
"\n",
|
||||
" print('Success: test_sort_stack')\n",
|
||||
"\n",
|
||||
|
@ -213,9 +205,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -253,9 +243,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
from random import randint
|
||||
from nose.tools import assert_equal
|
||||
import unittest
|
||||
|
||||
|
||||
class TestSortStack(object):
|
||||
class TestSortStack(unittest.TestCase):
|
||||
|
||||
def get_sorted_stack(self, stack, numbers):
|
||||
for x in numbers:
|
||||
|
@ -13,11 +13,11 @@ class TestSortStack(object):
|
|||
def test_sort_stack(self, stack):
|
||||
print('Test: Empty stack')
|
||||
sorted_stack = self.get_sorted_stack(stack, [])
|
||||
assert_equal(sorted_stack.pop(), None)
|
||||
self.assertEqual(sorted_stack.pop(), None)
|
||||
|
||||
print('Test: One element stack')
|
||||
sorted_stack = self.get_sorted_stack(stack, [1])
|
||||
assert_equal(sorted_stack.pop(), 1)
|
||||
self.assertEqual(sorted_stack.pop(), 1)
|
||||
|
||||
print('Test: Two or more element stack (general case)')
|
||||
num_items = 10
|
||||
|
@ -26,7 +26,7 @@ class TestSortStack(object):
|
|||
sorted_numbers = []
|
||||
for _ in range(num_items):
|
||||
sorted_numbers.append(sorted_stack.pop())
|
||||
assert_equal(sorted_numbers, sorted(numbers, reverse=True))
|
||||
self.assertEqual(sorted_numbers, sorted(numbers, reverse=True))
|
||||
|
||||
print('Success: test_sort_stack')
|
||||
|
||||
|
@ -38,4 +38,4 @@ def main():
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
|
|
@ -24,4 +24,4 @@ class Stack(object):
|
|||
return self.top.data if self.top is not None else None
|
||||
|
||||
def is_empty(self):
|
||||
return self.peek() is None
|
||||
return self.peek() is None
|
||||
|
|
|
@ -88,9 +88,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Node(object):\n",
|
||||
|
@ -137,44 +135,42 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_stack.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestStack(object):\n",
|
||||
"class TestStack(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" # TODO: It would be better if we had unit tests for each\n",
|
||||
" # method in addition to the following end-to-end test\n",
|
||||
" def test_end_to_end(self):\n",
|
||||
" print('Test: Empty stack')\n",
|
||||
" stack = Stack()\n",
|
||||
" assert_equal(stack.peek(), None)\n",
|
||||
" assert_equal(stack.pop(), None)\n",
|
||||
" self.assertEqual(stack.peek(), None)\n",
|
||||
" self.assertEqual(stack.pop(), None)\n",
|
||||
"\n",
|
||||
" print('Test: One element')\n",
|
||||
" top = Node(5)\n",
|
||||
" stack = Stack(top)\n",
|
||||
" assert_equal(stack.pop(), 5)\n",
|
||||
" assert_equal(stack.peek(), None)\n",
|
||||
" self.assertEqual(stack.pop(), 5)\n",
|
||||
" self.assertEqual(stack.peek(), None)\n",
|
||||
"\n",
|
||||
" print('Test: More than one element')\n",
|
||||
" stack = Stack()\n",
|
||||
" stack.push(1)\n",
|
||||
" stack.push(2)\n",
|
||||
" stack.push(3)\n",
|
||||
" assert_equal(stack.pop(), 3)\n",
|
||||
" assert_equal(stack.peek(), 2)\n",
|
||||
" assert_equal(stack.pop(), 2)\n",
|
||||
" assert_equal(stack.peek(), 1)\n",
|
||||
" assert_equal(stack.is_empty(), False)\n",
|
||||
" assert_equal(stack.pop(), 1)\n",
|
||||
" assert_equal(stack.peek(), None)\n",
|
||||
" assert_equal(stack.is_empty(), True)\n",
|
||||
" self.assertEqual(stack.pop(), 3)\n",
|
||||
" self.assertEqual(stack.peek(), 2)\n",
|
||||
" self.assertEqual(stack.pop(), 2)\n",
|
||||
" self.assertEqual(stack.peek(), 1)\n",
|
||||
" self.assertEqual(stack.is_empty(), False)\n",
|
||||
" self.assertEqual(stack.pop(), 1)\n",
|
||||
" self.assertEqual(stack.peek(), None)\n",
|
||||
" self.assertEqual(stack.is_empty(), True)\n",
|
||||
"\n",
|
||||
" print('Success: test_end_to_end')\n",
|
||||
"\n",
|
||||
|
@ -214,9 +210,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -124,9 +124,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -170,9 +168,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run stack.py"
|
||||
|
@ -189,9 +185,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -203,38 +197,38 @@
|
|||
],
|
||||
"source": [
|
||||
"%%writefile test_stack.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestStack(object):\n",
|
||||
"class TestStack(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" # TODO: It would be better if we had unit tests for each\n",
|
||||
" # method in addition to the following end-to-end test\n",
|
||||
" def test_end_to_end(self):\n",
|
||||
" print('Test: Empty stack')\n",
|
||||
" stack = Stack()\n",
|
||||
" assert_equal(stack.peek(), None)\n",
|
||||
" assert_equal(stack.pop(), None)\n",
|
||||
" self.assertEqual(stack.peek(), None)\n",
|
||||
" self.assertEqual(stack.pop(), None)\n",
|
||||
"\n",
|
||||
" print('Test: One element')\n",
|
||||
" top = Node(5)\n",
|
||||
" stack = Stack(top)\n",
|
||||
" assert_equal(stack.pop(), 5)\n",
|
||||
" assert_equal(stack.peek(), None)\n",
|
||||
" self.assertEqual(stack.pop(), 5)\n",
|
||||
" self.assertEqual(stack.peek(), None)\n",
|
||||
"\n",
|
||||
" print('Test: More than one element')\n",
|
||||
" stack = Stack()\n",
|
||||
" stack.push(1)\n",
|
||||
" stack.push(2)\n",
|
||||
" stack.push(3)\n",
|
||||
" assert_equal(stack.pop(), 3)\n",
|
||||
" assert_equal(stack.peek(), 2)\n",
|
||||
" assert_equal(stack.pop(), 2)\n",
|
||||
" assert_equal(stack.peek(), 1)\n",
|
||||
" assert_equal(stack.is_empty(), False)\n",
|
||||
" assert_equal(stack.pop(), 1)\n",
|
||||
" assert_equal(stack.peek(), None)\n",
|
||||
" assert_equal(stack.is_empty(), True)\n",
|
||||
" self.assertEqual(stack.pop(), 3)\n",
|
||||
" self.assertEqual(stack.peek(), 2)\n",
|
||||
" self.assertEqual(stack.pop(), 2)\n",
|
||||
" self.assertEqual(stack.peek(), 1)\n",
|
||||
" self.assertEqual(stack.is_empty(), False)\n",
|
||||
" self.assertEqual(stack.pop(), 1)\n",
|
||||
" self.assertEqual(stack.peek(), None)\n",
|
||||
" self.assertEqual(stack.is_empty(), True)\n",
|
||||
"\n",
|
||||
" print('Success: test_end_to_end')\n",
|
||||
"\n",
|
||||
|
@ -251,9 +245,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -321,9 +313,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -1,35 +1,35 @@
|
|||
from nose.tools import assert_equal
|
||||
import unittest
|
||||
|
||||
|
||||
class TestStack(object):
|
||||
class TestStack(unittest.TestCase):
|
||||
|
||||
# TODO: It would be better if we had unit tests for each
|
||||
# method in addition to the following end-to-end test
|
||||
def test_end_to_end(self):
|
||||
print('Test: Empty stack')
|
||||
stack = Stack()
|
||||
assert_equal(stack.peek(), None)
|
||||
assert_equal(stack.pop(), None)
|
||||
self.assertEqual(stack.peek(), None)
|
||||
self.assertEqual(stack.pop(), None)
|
||||
|
||||
print('Test: One element')
|
||||
top = Node(5)
|
||||
stack = Stack(top)
|
||||
assert_equal(stack.pop(), 5)
|
||||
assert_equal(stack.peek(), None)
|
||||
self.assertEqual(stack.pop(), 5)
|
||||
self.assertEqual(stack.peek(), None)
|
||||
|
||||
print('Test: More than one element')
|
||||
stack = Stack()
|
||||
stack.push(1)
|
||||
stack.push(2)
|
||||
stack.push(3)
|
||||
assert_equal(stack.pop(), 3)
|
||||
assert_equal(stack.peek(), 2)
|
||||
assert_equal(stack.pop(), 2)
|
||||
assert_equal(stack.peek(), 1)
|
||||
assert_equal(stack.is_empty(), False)
|
||||
assert_equal(stack.pop(), 1)
|
||||
assert_equal(stack.peek(), None)
|
||||
assert_equal(stack.is_empty(), True)
|
||||
self.assertEqual(stack.pop(), 3)
|
||||
self.assertEqual(stack.peek(), 2)
|
||||
self.assertEqual(stack.pop(), 2)
|
||||
self.assertEqual(stack.peek(), 1)
|
||||
self.assertEqual(stack.is_empty(), False)
|
||||
self.assertEqual(stack.pop(), 1)
|
||||
self.assertEqual(stack.peek(), None)
|
||||
self.assertEqual(stack.is_empty(), True)
|
||||
|
||||
print('Success: test_end_to_end')
|
||||
|
||||
|
@ -40,4 +40,4 @@ def main():
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
|
|
@ -130,45 +130,43 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_stack_min.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestStackMin(object):\n",
|
||||
"class TestStackMin(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_stack_min(self):\n",
|
||||
" print('Test: Push on empty stack, non-empty stack')\n",
|
||||
" stack = StackMin()\n",
|
||||
" stack.push(5)\n",
|
||||
" assert_equal(stack.peek(), 5)\n",
|
||||
" assert_equal(stack.minimum(), 5)\n",
|
||||
" self.assertEqual(stack.peek(), 5)\n",
|
||||
" self.assertEqual(stack.minimum(), 5)\n",
|
||||
" stack.push(1)\n",
|
||||
" assert_equal(stack.peek(), 1)\n",
|
||||
" assert_equal(stack.minimum(), 1)\n",
|
||||
" self.assertEqual(stack.peek(), 1)\n",
|
||||
" self.assertEqual(stack.minimum(), 1)\n",
|
||||
" stack.push(3)\n",
|
||||
" assert_equal(stack.peek(), 3)\n",
|
||||
" assert_equal(stack.minimum(), 1)\n",
|
||||
" self.assertEqual(stack.peek(), 3)\n",
|
||||
" self.assertEqual(stack.minimum(), 1)\n",
|
||||
" stack.push(0)\n",
|
||||
" assert_equal(stack.peek(), 0)\n",
|
||||
" assert_equal(stack.minimum(), 0)\n",
|
||||
" self.assertEqual(stack.peek(), 0)\n",
|
||||
" self.assertEqual(stack.minimum(), 0)\n",
|
||||
"\n",
|
||||
" print('Test: Pop on non-empty stack')\n",
|
||||
" assert_equal(stack.pop(), 0)\n",
|
||||
" assert_equal(stack.minimum(), 1)\n",
|
||||
" assert_equal(stack.pop(), 3)\n",
|
||||
" assert_equal(stack.minimum(), 1)\n",
|
||||
" assert_equal(stack.pop(), 1)\n",
|
||||
" assert_equal(stack.minimum(), 5)\n",
|
||||
" assert_equal(stack.pop(), 5)\n",
|
||||
" assert_equal(stack.minimum(), sys.maxsize)\n",
|
||||
" self.assertEqual(stack.pop(), 0)\n",
|
||||
" self.assertEqual(stack.minimum(), 1)\n",
|
||||
" self.assertEqual(stack.pop(), 3)\n",
|
||||
" self.assertEqual(stack.minimum(), 1)\n",
|
||||
" self.assertEqual(stack.pop(), 1)\n",
|
||||
" self.assertEqual(stack.minimum(), 5)\n",
|
||||
" self.assertEqual(stack.pop(), 5)\n",
|
||||
" self.assertEqual(stack.minimum(), sys.maxsize)\n",
|
||||
"\n",
|
||||
" print('Test: Pop empty stack')\n",
|
||||
" assert_equal(stack.pop(), None)\n",
|
||||
" self.assertEqual(stack.pop(), None)\n",
|
||||
"\n",
|
||||
" print('Success: test_stack_min')\n",
|
||||
"\n",
|
||||
|
@ -208,9 +206,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -106,9 +106,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run ../stack/stack.py"
|
||||
|
@ -117,9 +115,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
|
@ -160,9 +156,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -174,39 +168,39 @@
|
|||
],
|
||||
"source": [
|
||||
"%%writefile test_stack_min.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestStackMin(object):\n",
|
||||
"class TestStackMin(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_stack_min(self):\n",
|
||||
" print('Test: Push on empty stack, non-empty stack')\n",
|
||||
" stack = StackMin()\n",
|
||||
" stack.push(5)\n",
|
||||
" assert_equal(stack.peek(), 5)\n",
|
||||
" assert_equal(stack.minimum(), 5)\n",
|
||||
" self.assertEqual(stack.peek(), 5)\n",
|
||||
" self.assertEqual(stack.minimum(), 5)\n",
|
||||
" stack.push(1)\n",
|
||||
" assert_equal(stack.peek(), 1)\n",
|
||||
" assert_equal(stack.minimum(), 1)\n",
|
||||
" self.assertEqual(stack.peek(), 1)\n",
|
||||
" self.assertEqual(stack.minimum(), 1)\n",
|
||||
" stack.push(3)\n",
|
||||
" assert_equal(stack.peek(), 3)\n",
|
||||
" assert_equal(stack.minimum(), 1)\n",
|
||||
" self.assertEqual(stack.peek(), 3)\n",
|
||||
" self.assertEqual(stack.minimum(), 1)\n",
|
||||
" stack.push(0)\n",
|
||||
" assert_equal(stack.peek(), 0)\n",
|
||||
" assert_equal(stack.minimum(), 0)\n",
|
||||
" self.assertEqual(stack.peek(), 0)\n",
|
||||
" self.assertEqual(stack.minimum(), 0)\n",
|
||||
"\n",
|
||||
" print('Test: Pop on non-empty stack')\n",
|
||||
" assert_equal(stack.pop(), 0)\n",
|
||||
" assert_equal(stack.minimum(), 1)\n",
|
||||
" assert_equal(stack.pop(), 3)\n",
|
||||
" assert_equal(stack.minimum(), 1)\n",
|
||||
" assert_equal(stack.pop(), 1)\n",
|
||||
" assert_equal(stack.minimum(), 5)\n",
|
||||
" assert_equal(stack.pop(), 5)\n",
|
||||
" assert_equal(stack.minimum(), sys.maxsize)\n",
|
||||
" self.assertEqual(stack.pop(), 0)\n",
|
||||
" self.assertEqual(stack.minimum(), 1)\n",
|
||||
" self.assertEqual(stack.pop(), 3)\n",
|
||||
" self.assertEqual(stack.minimum(), 1)\n",
|
||||
" self.assertEqual(stack.pop(), 1)\n",
|
||||
" self.assertEqual(stack.minimum(), 5)\n",
|
||||
" self.assertEqual(stack.pop(), 5)\n",
|
||||
" self.assertEqual(stack.minimum(), sys.maxsize)\n",
|
||||
"\n",
|
||||
" print('Test: Pop empty stack')\n",
|
||||
" assert_equal(stack.pop(), None)\n",
|
||||
" self.assertEqual(stack.pop(), None)\n",
|
||||
"\n",
|
||||
" print('Success: test_stack_min')\n",
|
||||
"\n",
|
||||
|
@ -223,9 +217,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -259,9 +251,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -1,37 +1,36 @@
|
|||
import sys
|
||||
from nose.tools import assert_equal
|
||||
import unittest
|
||||
|
||||
|
||||
class TestStackMin(object):
|
||||
class TestStackMin(unittest.TestCase):
|
||||
|
||||
def test_stack_min(self):
|
||||
print('Test: Push on empty stack, non-empty stack')
|
||||
stack = StackMin()
|
||||
stack.push(5)
|
||||
assert_equal(stack.peek(), 5)
|
||||
assert_equal(stack.minimum(), 5)
|
||||
self.assertEqual(stack.peek(), 5)
|
||||
self.assertEqual(stack.minimum(), 5)
|
||||
stack.push(1)
|
||||
assert_equal(stack.peek(), 1)
|
||||
assert_equal(stack.minimum(), 1)
|
||||
self.assertEqual(stack.peek(), 1)
|
||||
self.assertEqual(stack.minimum(), 1)
|
||||
stack.push(3)
|
||||
assert_equal(stack.peek(), 3)
|
||||
assert_equal(stack.minimum(), 1)
|
||||
self.assertEqual(stack.peek(), 3)
|
||||
self.assertEqual(stack.minimum(), 1)
|
||||
stack.push(0)
|
||||
assert_equal(stack.peek(), 0)
|
||||
assert_equal(stack.minimum(), 0)
|
||||
self.assertEqual(stack.peek(), 0)
|
||||
self.assertEqual(stack.minimum(), 0)
|
||||
|
||||
print('Test: Pop on non-empty stack')
|
||||
assert_equal(stack.pop(), 0)
|
||||
assert_equal(stack.minimum(), 1)
|
||||
assert_equal(stack.pop(), 3)
|
||||
assert_equal(stack.minimum(), 1)
|
||||
assert_equal(stack.pop(), 1)
|
||||
assert_equal(stack.minimum(), 5)
|
||||
assert_equal(stack.pop(), 5)
|
||||
assert_equal(stack.minimum(), sys.maxsize)
|
||||
self.assertEqual(stack.pop(), 0)
|
||||
self.assertEqual(stack.minimum(), 1)
|
||||
self.assertEqual(stack.pop(), 3)
|
||||
self.assertEqual(stack.minimum(), 1)
|
||||
self.assertEqual(stack.pop(), 1)
|
||||
self.assertEqual(stack.minimum(), 5)
|
||||
self.assertEqual(stack.pop(), 5)
|
||||
self.assertEqual(stack.minimum(), sys.maxsize)
|
||||
|
||||
print('Test: Pop empty stack')
|
||||
assert_equal(stack.pop(), None)
|
||||
self.assertEqual(stack.pop(), None)
|
||||
|
||||
print('Success: test_stack_min')
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user