#273: Remove nose dependency for stacks_queues/ (#276)

This commit is contained in:
Donne Martin 2020-07-08 19:57:55 -04:00 committed by GitHub
parent 0e7ed80228
commit 139e157250
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 290 additions and 373 deletions

View File

@ -80,9 +80,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"class Stacks(object):\n", "class Stacks(object):\n",
@ -118,25 +116,20 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"# %load test_n_stacks.py\n", "# %load test_n_stacks.py\n",
"from nose.tools import assert_equal\n", "import unittest\n",
"from nose.tools import raises\n",
"\n", "\n",
"\n", "\n",
"class TestStacks(object):\n", "class TestStacks(unittest.TestCase):\n",
"\n", "\n",
" @raises(Exception)\n",
" def test_pop_on_empty(self, num_stacks, stack_size):\n", " def test_pop_on_empty(self, num_stacks, stack_size):\n",
" print('Test: Pop on empty stack')\n", " print('Test: Pop on empty stack')\n",
" stacks = Stacks(num_stacks, stack_size)\n", " stacks = Stacks(num_stacks, stack_size)\n",
" stacks.pop(0)\n", " stacks.pop(0)\n",
"\n", "\n",
" @raises(Exception)\n",
" def test_push_on_full(self, num_stacks, stack_size):\n", " def test_push_on_full(self, num_stacks, stack_size):\n",
" print('Test: Push to full stack')\n", " print('Test: Push to full stack')\n",
" stacks = Stacks(num_stacks, stack_size)\n", " stacks = Stacks(num_stacks, stack_size)\n",
@ -153,10 +146,10 @@
" stacks.push(2, 4)\n", " stacks.push(2, 4)\n",
"\n", "\n",
" print('Test: Pop on non-empty stack')\n", " print('Test: Pop on non-empty stack')\n",
" assert_equal(stacks.pop(0), 2)\n", " self.assertEqual(stacks.pop(0), 2)\n",
" assert_equal(stacks.pop(0), 1)\n", " self.assertEqual(stacks.pop(0), 1)\n",
" assert_equal(stacks.pop(1), 3)\n", " self.assertEqual(stacks.pop(1), 3)\n",
" assert_equal(stacks.pop(2), 4)\n", " self.assertEqual(stacks.pop(2), 4)\n",
"\n", "\n",
" print('Success: test_stacks\\n')\n", " print('Success: test_stacks\\n')\n",
"\n", "\n",
@ -165,8 +158,10 @@
" num_stacks = 3\n", " num_stacks = 3\n",
" stack_size = 100\n", " stack_size = 100\n",
" test = TestStacks()\n", " test = TestStacks()\n",
" test.test_pop_on_empty(num_stacks, stack_size)\n", " test.assertRaises(Exception, test.test_pop_on_empty, num_stacks,\n",
" test.test_push_on_full(num_stacks, stack_size)\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", " test.test_stacks(num_stacks, stack_size)\n",
"\n", "\n",
"\n", "\n",
@ -200,9 +195,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.0" "version": "3.7.2"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -110,9 +110,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"class Stacks(object):\n", "class Stacks(object):\n",
@ -154,9 +152,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -168,19 +164,16 @@
], ],
"source": [ "source": [
"%%writefile test_n_stacks.py\n", "%%writefile test_n_stacks.py\n",
"from nose.tools import assert_equal\n", "import unittest\n",
"from nose.tools import raises\n",
"\n", "\n",
"\n", "\n",
"class TestStacks(object):\n", "class TestStacks(unittest.TestCase):\n",
"\n", "\n",
" @raises(Exception)\n",
" def test_pop_on_empty(self, num_stacks, stack_size):\n", " def test_pop_on_empty(self, num_stacks, stack_size):\n",
" print('Test: Pop on empty stack')\n", " print('Test: Pop on empty stack')\n",
" stacks = Stacks(num_stacks, stack_size)\n", " stacks = Stacks(num_stacks, stack_size)\n",
" stacks.pop(0)\n", " stacks.pop(0)\n",
"\n", "\n",
" @raises(Exception)\n",
" def test_push_on_full(self, num_stacks, stack_size):\n", " def test_push_on_full(self, num_stacks, stack_size):\n",
" print('Test: Push to full stack')\n", " print('Test: Push to full stack')\n",
" stacks = Stacks(num_stacks, stack_size)\n", " stacks = Stacks(num_stacks, stack_size)\n",
@ -197,10 +190,10 @@
" stacks.push(2, 4)\n", " stacks.push(2, 4)\n",
"\n", "\n",
" print('Test: Pop on non-empty stack')\n", " print('Test: Pop on non-empty stack')\n",
" assert_equal(stacks.pop(0), 2)\n", " self.assertEqual(stacks.pop(0), 2)\n",
" assert_equal(stacks.pop(0), 1)\n", " self.assertEqual(stacks.pop(0), 1)\n",
" assert_equal(stacks.pop(1), 3)\n", " self.assertEqual(stacks.pop(1), 3)\n",
" assert_equal(stacks.pop(2), 4)\n", " self.assertEqual(stacks.pop(2), 4)\n",
"\n", "\n",
" print('Success: test_stacks\\n')\n", " print('Success: test_stacks\\n')\n",
"\n", "\n",
@ -209,8 +202,10 @@
" num_stacks = 3\n", " num_stacks = 3\n",
" stack_size = 100\n", " stack_size = 100\n",
" test = TestStacks()\n", " test = TestStacks()\n",
" test.test_pop_on_empty(num_stacks, stack_size)\n", " test.assertRaises(Exception, test.test_pop_on_empty, num_stacks,\n",
" test.test_push_on_full(num_stacks, stack_size)\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", " test.test_stacks(num_stacks, stack_size)\n",
"\n", "\n",
"\n", "\n",
@ -221,9 +216,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 3,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -241,6 +234,13 @@
"source": [ "source": [
"run -i test_n_stacks.py" "run -i test_n_stacks.py"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {
@ -259,9 +259,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.0" "version": "3.7.2"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -1,16 +1,13 @@
from nose.tools import assert_equal import unittest
from nose.tools import raises
class TestStacks(object): class TestStacks(unittest.TestCase):
@raises(Exception)
def test_pop_on_empty(self, num_stacks, stack_size): def test_pop_on_empty(self, num_stacks, stack_size):
print('Test: Pop on empty stack') print('Test: Pop on empty stack')
stacks = Stacks(num_stacks, stack_size) stacks = Stacks(num_stacks, stack_size)
stacks.pop(0) stacks.pop(0)
@raises(Exception)
def test_push_on_full(self, num_stacks, stack_size): def test_push_on_full(self, num_stacks, stack_size):
print('Test: Push to full stack') print('Test: Push to full stack')
stacks = Stacks(num_stacks, stack_size) stacks = Stacks(num_stacks, stack_size)
@ -27,10 +24,10 @@ class TestStacks(object):
stacks.push(2, 4) stacks.push(2, 4)
print('Test: Pop on non-empty stack') print('Test: Pop on non-empty stack')
assert_equal(stacks.pop(0), 2) self.assertEqual(stacks.pop(0), 2)
assert_equal(stacks.pop(0), 1) self.assertEqual(stacks.pop(0), 1)
assert_equal(stacks.pop(1), 3) self.assertEqual(stacks.pop(1), 3)
assert_equal(stacks.pop(2), 4) self.assertEqual(stacks.pop(2), 4)
print('Success: test_stacks\n') print('Success: test_stacks\n')
@ -39,10 +36,12 @@ def main():
num_stacks = 3 num_stacks = 3
stack_size = 100 stack_size = 100
test = TestStacks() test = TestStacks()
test.test_pop_on_empty(num_stacks, stack_size) test.assertRaises(Exception, test.test_pop_on_empty, num_stacks,
test.test_push_on_full(num_stacks, stack_size) stack_size)
test.assertRaises(Exception, test.test_push_on_full, num_stacks,
stack_size)
test.test_stacks(num_stacks, stack_size) test.test_stacks(num_stacks, stack_size)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -77,9 +77,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"%run ../stack/stack.py\n", "%run ../stack/stack.py\n",
@ -89,9 +87,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"class QueueFromStacks(object):\n", "class QueueFromStacks(object):\n",
@ -124,21 +120,19 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"# %load test_queue_from_stacks.py\n", "# %load test_queue_from_stacks.py\n",
"from nose.tools import assert_equal\n", "import unittest\n",
"\n", "\n",
"\n", "\n",
"class TestQueueFromStacks(object):\n", "class TestQueueFromStacks(unittest.TestCase):\n",
"\n", "\n",
" def test_queue_from_stacks(self):\n", " def test_queue_from_stacks(self):\n",
" print('Test: Dequeue on empty stack')\n", " print('Test: Dequeue on empty stack')\n",
" queue = QueueFromStacks()\n", " queue = QueueFromStacks()\n",
" assert_equal(queue.dequeue(), None)\n", " self.assertEqual(queue.dequeue(), None)\n",
"\n", "\n",
" print('Test: Enqueue on empty stack')\n", " print('Test: Enqueue on empty stack')\n",
" print('Test: Enqueue on non-empty stack')\n", " print('Test: Enqueue on non-empty stack')\n",
@ -149,15 +143,15 @@
"\n", "\n",
" print('Test: Dequeue on non-empty stack')\n", " print('Test: Dequeue on non-empty stack')\n",
" print('Test: Dequeue after an enqueue')\n", " print('Test: Dequeue after an enqueue')\n",
" assert_equal(queue.dequeue(), 0)\n", " self.assertEqual(queue.dequeue(), 0)\n",
"\n", "\n",
" print('Test: Multiple dequeue in a row')\n", " print('Test: Multiple dequeue in a row')\n",
" assert_equal(queue.dequeue(), 1)\n", " self.assertEqual(queue.dequeue(), 1)\n",
" assert_equal(queue.dequeue(), 2)\n", " self.assertEqual(queue.dequeue(), 2)\n",
"\n", "\n",
" print('Test: Enqueue after a dequeue')\n", " print('Test: Enqueue after a dequeue')\n",
" queue.enqueue(5)\n", " queue.enqueue(5)\n",
" assert_equal(queue.dequeue(), 5)\n", " self.assertEqual(queue.dequeue(), 5)\n",
"\n", "\n",
" print('Success: test_queue_from_stacks')\n", " print('Success: test_queue_from_stacks')\n",
"\n", "\n",
@ -197,9 +191,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.0" "version": "3.7.2"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -107,9 +107,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"%run ../stack/stack.py" "%run ../stack/stack.py"
@ -118,9 +116,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"class QueueFromStacks(object):\n", "class QueueFromStacks(object):\n",
@ -153,9 +149,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 3,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -167,15 +161,15 @@
], ],
"source": [ "source": [
"%%writefile test_queue_from_stacks.py\n", "%%writefile test_queue_from_stacks.py\n",
"from nose.tools import assert_equal\n", "import unittest\n",
"\n", "\n",
"\n", "\n",
"class TestQueueFromStacks(object):\n", "class TestQueueFromStacks(unittest.TestCase):\n",
"\n", "\n",
" def test_queue_from_stacks(self):\n", " def test_queue_from_stacks(self):\n",
" print('Test: Dequeue on empty stack')\n", " print('Test: Dequeue on empty stack')\n",
" queue = QueueFromStacks()\n", " queue = QueueFromStacks()\n",
" assert_equal(queue.dequeue(), None)\n", " self.assertEqual(queue.dequeue(), None)\n",
"\n", "\n",
" print('Test: Enqueue on empty stack')\n", " print('Test: Enqueue on empty stack')\n",
" print('Test: Enqueue on non-empty stack')\n", " print('Test: Enqueue on non-empty stack')\n",
@ -186,15 +180,15 @@
"\n", "\n",
" print('Test: Dequeue on non-empty stack')\n", " print('Test: Dequeue on non-empty stack')\n",
" print('Test: Dequeue after an enqueue')\n", " print('Test: Dequeue after an enqueue')\n",
" assert_equal(queue.dequeue(), 0)\n", " self.assertEqual(queue.dequeue(), 0)\n",
"\n", "\n",
" print('Test: Multiple dequeue in a row')\n", " print('Test: Multiple dequeue in a row')\n",
" assert_equal(queue.dequeue(), 1)\n", " self.assertEqual(queue.dequeue(), 1)\n",
" assert_equal(queue.dequeue(), 2)\n", " self.assertEqual(queue.dequeue(), 2)\n",
"\n", "\n",
" print('Test: Enqueue after a dequeue')\n", " print('Test: Enqueue after a dequeue')\n",
" queue.enqueue(5)\n", " queue.enqueue(5)\n",
" assert_equal(queue.dequeue(), 5)\n", " self.assertEqual(queue.dequeue(), 5)\n",
"\n", "\n",
" print('Success: test_queue_from_stacks')\n", " print('Success: test_queue_from_stacks')\n",
"\n", "\n",
@ -211,9 +205,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 4,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -252,9 +244,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.0" "version": "3.7.2"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -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): def test_queue_from_stacks(self):
print('Test: Dequeue on empty stack') print('Test: Dequeue on empty stack')
queue = QueueFromStacks() queue = QueueFromStacks()
assert_equal(queue.dequeue(), None) self.assertEqual(queue.dequeue(), None)
print('Test: Enqueue on empty stack') print('Test: Enqueue on empty stack')
print('Test: Enqueue on non-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 on non-empty stack')
print('Test: Dequeue after an enqueue') print('Test: Dequeue after an enqueue')
assert_equal(queue.dequeue(), 0) self.assertEqual(queue.dequeue(), 0)
print('Test: Multiple dequeue in a row') print('Test: Multiple dequeue in a row')
assert_equal(queue.dequeue(), 1) self.assertEqual(queue.dequeue(), 1)
assert_equal(queue.dequeue(), 2) self.assertEqual(queue.dequeue(), 2)
print('Test: Enqueue after a dequeue') print('Test: Enqueue after a dequeue')
queue.enqueue(5) queue.enqueue(5)
assert_equal(queue.dequeue(), 5) self.assertEqual(queue.dequeue(), 5)
print('Success: test_queue_from_stacks') print('Success: test_queue_from_stacks')
@ -36,4 +36,4 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -32,4 +32,4 @@ class Queue(object):
self.tail = None self.tail = None
else: else:
self.head = self.head.next self.head = self.head.next
return data return data

View File

@ -82,9 +82,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"class Node(object):\n", "class Node(object):\n",
@ -123,29 +121,27 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"# %load test_queue_list.py\n", "# %load test_queue_list.py\n",
"from nose.tools import assert_equal\n", "import unittest\n",
"\n", "\n",
"\n", "\n",
"class TestQueue(object):\n", "class TestQueue(unittest.TestCase):\n",
"\n", "\n",
" # TODO: It would be better if we had unit tests for each\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", " # method in addition to the following end-to-end test\n",
" def test_end_to_end(self):\n", " def test_end_to_end(self):\n",
" print('Test: Dequeue an empty queue')\n", " print('Test: Dequeue an empty queue')\n",
" queue = Queue()\n", " queue = Queue()\n",
" assert_equal(queue.dequeue(), None)\n", " self.assertEqual(queue.dequeue(), None)\n",
"\n", "\n",
" print('Test: Enqueue to an empty queue')\n", " print('Test: Enqueue to an empty queue')\n",
" queue.enqueue(1)\n", " queue.enqueue(1)\n",
"\n", "\n",
" print('Test: Dequeue a queue with one element')\n", " print('Test: Dequeue a queue with one element')\n",
" assert_equal(queue.dequeue(), 1)\n", " self.assertEqual(queue.dequeue(), 1)\n",
"\n", "\n",
" print('Test: Enqueue to a non-empty queue')\n", " print('Test: Enqueue to a non-empty queue')\n",
" queue.enqueue(2)\n", " queue.enqueue(2)\n",
@ -153,9 +149,9 @@
" queue.enqueue(4)\n", " queue.enqueue(4)\n",
"\n", "\n",
" print('Test: Dequeue a queue with more than one element')\n", " print('Test: Dequeue a queue with more than one element')\n",
" assert_equal(queue.dequeue(), 2)\n", " self.assertEqual(queue.dequeue(), 2)\n",
" assert_equal(queue.dequeue(), 3)\n", " self.assertEqual(queue.dequeue(), 3)\n",
" assert_equal(queue.dequeue(), 4)\n", " self.assertEqual(queue.dequeue(), 4)\n",
"\n", "\n",
" print('Success: test_end_to_end')\n", " print('Success: test_end_to_end')\n",
"\n", "\n",
@ -195,9 +191,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.0" "version": "3.7.2"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -104,9 +104,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -158,9 +156,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"%run queue_list.py" "%run queue_list.py"
@ -177,9 +173,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 3,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -191,23 +185,23 @@
], ],
"source": [ "source": [
"%%writefile test_queue_list.py\n", "%%writefile test_queue_list.py\n",
"from nose.tools import assert_equal\n", "import unittest\n",
"\n", "\n",
"\n", "\n",
"class TestQueue(object):\n", "class TestQueue(unittest.TestCase):\n",
"\n", "\n",
" # TODO: It would be better if we had unit tests for each\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", " # method in addition to the following end-to-end test\n",
" def test_end_to_end(self):\n", " def test_end_to_end(self):\n",
" print('Test: Dequeue an empty queue')\n", " print('Test: Dequeue an empty queue')\n",
" queue = Queue()\n", " queue = Queue()\n",
" assert_equal(queue.dequeue(), None)\n", " self.assertEqual(queue.dequeue(), None)\n",
"\n", "\n",
" print('Test: Enqueue to an empty queue')\n", " print('Test: Enqueue to an empty queue')\n",
" queue.enqueue(1)\n", " queue.enqueue(1)\n",
"\n", "\n",
" print('Test: Dequeue a queue with one element')\n", " print('Test: Dequeue a queue with one element')\n",
" assert_equal(queue.dequeue(), 1)\n", " self.assertEqual(queue.dequeue(), 1)\n",
"\n", "\n",
" print('Test: Enqueue to a non-empty queue')\n", " print('Test: Enqueue to a non-empty queue')\n",
" queue.enqueue(2)\n", " queue.enqueue(2)\n",
@ -215,9 +209,9 @@
" queue.enqueue(4)\n", " queue.enqueue(4)\n",
"\n", "\n",
" print('Test: Dequeue a queue with more than one element')\n", " print('Test: Dequeue a queue with more than one element')\n",
" assert_equal(queue.dequeue(), 2)\n", " self.assertEqual(queue.dequeue(), 2)\n",
" assert_equal(queue.dequeue(), 3)\n", " self.assertEqual(queue.dequeue(), 3)\n",
" assert_equal(queue.dequeue(), 4)\n", " self.assertEqual(queue.dequeue(), 4)\n",
"\n", "\n",
" print('Success: test_end_to_end')\n", " print('Success: test_end_to_end')\n",
"\n", "\n",
@ -234,9 +228,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 4,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -303,9 +295,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.0" "version": "3.7.2"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -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 # TODO: It would be better if we had unit tests for each
# method in addition to the following end-to-end test # method in addition to the following end-to-end test
def test_end_to_end(self): def test_end_to_end(self):
print('Test: Dequeue an empty queue') print('Test: Dequeue an empty queue')
queue = Queue() queue = Queue()
assert_equal(queue.dequeue(), None) self.assertEqual(queue.dequeue(), None)
print('Test: Enqueue to an empty queue') print('Test: Enqueue to an empty queue')
queue.enqueue(1) queue.enqueue(1)
print('Test: Dequeue a queue with one element') 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') print('Test: Enqueue to a non-empty queue')
queue.enqueue(2) queue.enqueue(2)
@ -22,9 +22,9 @@ class TestQueue(object):
queue.enqueue(4) queue.enqueue(4)
print('Test: Dequeue a queue with more than one element') print('Test: Dequeue a queue with more than one element')
assert_equal(queue.dequeue(), 2) self.assertEqual(queue.dequeue(), 2)
assert_equal(queue.dequeue(), 3) self.assertEqual(queue.dequeue(), 3)
assert_equal(queue.dequeue(), 4) self.assertEqual(queue.dequeue(), 4)
print('Success: test_end_to_end') print('Success: test_end_to_end')
@ -35,4 +35,4 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -79,9 +79,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"%run ../stack/stack.py\n", "%run ../stack/stack.py\n",
@ -91,9 +89,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"class StackWithCapacity(Stack):\n", "class StackWithCapacity(Stack):\n",
@ -144,16 +140,14 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"# %load test_set_of_stacks.py\n", "# %load test_set_of_stacks.py\n",
"from nose.tools import assert_equal\n", "import unittest\n",
"\n", "\n",
"\n", "\n",
"class TestSetOfStacks(object):\n", "class TestSetOfStacks(unittest.TestCase):\n",
"\n", "\n",
" def test_set_of_stacks(self):\n", " def test_set_of_stacks(self):\n",
" print('Test: Push on an empty stack')\n", " print('Test: Push on an empty stack')\n",
@ -167,14 +161,14 @@
" stacks.push('a')\n", " stacks.push('a')\n",
"\n", "\n",
" print('Test: Pop on a stack to destroy it')\n", " print('Test: Pop on a stack to destroy it')\n",
" assert_equal(stacks.pop(), 'a')\n", " self.assertEqual(stacks.pop(), 'a')\n",
"\n", "\n",
" print('Test: Pop general case')\n", " print('Test: Pop general case')\n",
" assert_equal(stacks.pop(), 5)\n", " self.assertEqual(stacks.pop(), 5)\n",
" assert_equal(stacks.pop(), 3)\n", " self.assertEqual(stacks.pop(), 3)\n",
"\n", "\n",
" print('Test: Pop on no elements')\n", " print('Test: Pop on no elements')\n",
" assert_equal(stacks.pop(), None)\n", " self.assertEqual(stacks.pop(), None)\n",
"\n", "\n",
" print('Success: test_set_of_stacks')\n", " print('Success: test_set_of_stacks')\n",
"\n", "\n",
@ -214,9 +208,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.0" "version": "3.7.2"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -100,9 +100,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"%run ../stack/stack.py" "%run ../stack/stack.py"
@ -111,9 +109,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"class StackWithCapacity(Stack):\n", "class StackWithCapacity(Stack):\n",
@ -174,9 +170,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 3,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -188,10 +182,10 @@
], ],
"source": [ "source": [
"%%writefile test_set_of_stacks.py\n", "%%writefile test_set_of_stacks.py\n",
"from nose.tools import assert_equal\n", "import unittest\n",
"\n", "\n",
"\n", "\n",
"class TestSetOfStacks(object):\n", "class TestSetOfStacks(unittest.TestCase):\n",
"\n", "\n",
" def test_set_of_stacks(self):\n", " def test_set_of_stacks(self):\n",
" print('Test: Push on an empty stack')\n", " print('Test: Push on an empty stack')\n",
@ -205,14 +199,14 @@
" stacks.push('a')\n", " stacks.push('a')\n",
"\n", "\n",
" print('Test: Pop on a stack to destroy it')\n", " print('Test: Pop on a stack to destroy it')\n",
" assert_equal(stacks.pop(), 'a')\n", " self.assertEqual(stacks.pop(), 'a')\n",
"\n", "\n",
" print('Test: Pop general case')\n", " print('Test: Pop general case')\n",
" assert_equal(stacks.pop(), 5)\n", " self.assertEqual(stacks.pop(), 5)\n",
" assert_equal(stacks.pop(), 3)\n", " self.assertEqual(stacks.pop(), 3)\n",
"\n", "\n",
" print('Test: Pop on no elements')\n", " print('Test: Pop on no elements')\n",
" assert_equal(stacks.pop(), None)\n", " self.assertEqual(stacks.pop(), None)\n",
"\n", "\n",
" print('Success: test_set_of_stacks')\n", " print('Success: test_set_of_stacks')\n",
"\n", "\n",
@ -229,9 +223,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 4,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -268,9 +260,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.0" "version": "3.7.2"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -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): def test_set_of_stacks(self):
print('Test: Push on an empty stack') print('Test: Push on an empty stack')
@ -15,14 +15,14 @@ class TestSetOfStacks(object):
stacks.push('a') stacks.push('a')
print('Test: Pop on a stack to destroy it') print('Test: Pop on a stack to destroy it')
assert_equal(stacks.pop(), 'a') self.assertEqual(stacks.pop(), 'a')
print('Test: Pop general case') print('Test: Pop general case')
assert_equal(stacks.pop(), 5) self.assertEqual(stacks.pop(), 5)
assert_equal(stacks.pop(), 3) self.assertEqual(stacks.pop(), 3)
print('Test: Pop on no elements') print('Test: Pop on no elements')
assert_equal(stacks.pop(), None) self.assertEqual(stacks.pop(), None)
print('Success: test_set_of_stacks') print('Success: test_set_of_stacks')
@ -33,4 +33,4 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -87,9 +87,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyStack(Stack):\n", "class MyStack(Stack):\n",
@ -113,17 +111,15 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"# %load test_sort_stack.py\n", "# %load test_sort_stack.py\n",
"from random import randint\n", "from random import randint\n",
"from nose.tools import assert_equal\n", "import unittest\n",
"\n", "\n",
"\n", "\n",
"class TestSortStack(object):\n", "class TestSortStack(unittest.TestCase):\n",
"\n", "\n",
" def get_sorted_stack(self, stack, numbers):\n", " def get_sorted_stack(self, stack, numbers):\n",
" for x in numbers:\n", " for x in numbers:\n",
@ -134,11 +130,11 @@
" def test_sort_stack(self, stack):\n", " def test_sort_stack(self, stack):\n",
" print('Test: Empty stack')\n", " print('Test: Empty stack')\n",
" sorted_stack = self.get_sorted_stack(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", "\n",
" print('Test: One element stack')\n", " print('Test: One element stack')\n",
" sorted_stack = self.get_sorted_stack(stack, [1])\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", "\n",
" print('Test: Two or more element stack (general case)')\n", " print('Test: Two or more element stack (general case)')\n",
" num_items = 10\n", " num_items = 10\n",
@ -147,7 +143,7 @@
" sorted_numbers = []\n", " sorted_numbers = []\n",
" for _ in range(num_items):\n", " for _ in range(num_items):\n",
" sorted_numbers.append(sorted_stack.pop())\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", "\n",
" print('Success: test_sort_stack')\n", " print('Success: test_sort_stack')\n",
"\n", "\n",
@ -193,9 +189,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.0" "version": "3.7.2"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -84,9 +84,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"%run ../stack/stack.py" "%run ../stack/stack.py"
@ -95,9 +93,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyStack(Stack):\n", "class MyStack(Stack):\n",
@ -125,9 +121,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 3,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyStackSimplified(Stack):\n", "class MyStackSimplified(Stack):\n",
@ -153,9 +147,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 4,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -168,10 +160,10 @@
"source": [ "source": [
"%%writefile test_sort_stack.py\n", "%%writefile test_sort_stack.py\n",
"from random import randint\n", "from random import randint\n",
"from nose.tools import assert_equal\n", "import unittest\n",
"\n", "\n",
"\n", "\n",
"class TestSortStack(object):\n", "class TestSortStack(unittest.TestCase):\n",
"\n", "\n",
" def get_sorted_stack(self, stack, numbers):\n", " def get_sorted_stack(self, stack, numbers):\n",
" for x in numbers:\n", " for x in numbers:\n",
@ -182,11 +174,11 @@
" def test_sort_stack(self, stack):\n", " def test_sort_stack(self, stack):\n",
" print('Test: Empty stack')\n", " print('Test: Empty stack')\n",
" sorted_stack = self.get_sorted_stack(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", "\n",
" print('Test: One element stack')\n", " print('Test: One element stack')\n",
" sorted_stack = self.get_sorted_stack(stack, [1])\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", "\n",
" print('Test: Two or more element stack (general case)')\n", " print('Test: Two or more element stack (general case)')\n",
" num_items = 10\n", " num_items = 10\n",
@ -195,7 +187,7 @@
" sorted_numbers = []\n", " sorted_numbers = []\n",
" for _ in range(num_items):\n", " for _ in range(num_items):\n",
" sorted_numbers.append(sorted_stack.pop())\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", "\n",
" print('Success: test_sort_stack')\n", " print('Success: test_sort_stack')\n",
"\n", "\n",
@ -213,9 +205,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 5,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -253,9 +243,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.0" "version": "3.7.2"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -1,8 +1,8 @@
from random import randint 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): def get_sorted_stack(self, stack, numbers):
for x in numbers: for x in numbers:
@ -13,11 +13,11 @@ class TestSortStack(object):
def test_sort_stack(self, stack): def test_sort_stack(self, stack):
print('Test: Empty stack') print('Test: Empty stack')
sorted_stack = self.get_sorted_stack(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') print('Test: One element stack')
sorted_stack = self.get_sorted_stack(stack, [1]) 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)') print('Test: Two or more element stack (general case)')
num_items = 10 num_items = 10
@ -26,7 +26,7 @@ class TestSortStack(object):
sorted_numbers = [] sorted_numbers = []
for _ in range(num_items): for _ in range(num_items):
sorted_numbers.append(sorted_stack.pop()) 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') print('Success: test_sort_stack')
@ -38,4 +38,4 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -24,4 +24,4 @@ class Stack(object):
return self.top.data if self.top is not None else None return self.top.data if self.top is not None else None
def is_empty(self): def is_empty(self):
return self.peek() is None return self.peek() is None

View File

@ -88,9 +88,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"class Node(object):\n", "class Node(object):\n",
@ -137,44 +135,42 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"# %load test_stack.py\n", "# %load test_stack.py\n",
"from nose.tools import assert_equal\n", "import unittest\n",
"\n", "\n",
"\n", "\n",
"class TestStack(object):\n", "class TestStack(unittest.TestCase):\n",
"\n", "\n",
" # TODO: It would be better if we had unit tests for each\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", " # method in addition to the following end-to-end test\n",
" def test_end_to_end(self):\n", " def test_end_to_end(self):\n",
" print('Test: Empty stack')\n", " print('Test: Empty stack')\n",
" stack = Stack()\n", " stack = Stack()\n",
" assert_equal(stack.peek(), None)\n", " self.assertEqual(stack.peek(), None)\n",
" assert_equal(stack.pop(), None)\n", " self.assertEqual(stack.pop(), None)\n",
"\n", "\n",
" print('Test: One element')\n", " print('Test: One element')\n",
" top = Node(5)\n", " top = Node(5)\n",
" stack = Stack(top)\n", " stack = Stack(top)\n",
" assert_equal(stack.pop(), 5)\n", " self.assertEqual(stack.pop(), 5)\n",
" assert_equal(stack.peek(), None)\n", " self.assertEqual(stack.peek(), None)\n",
"\n", "\n",
" print('Test: More than one element')\n", " print('Test: More than one element')\n",
" stack = Stack()\n", " stack = Stack()\n",
" stack.push(1)\n", " stack.push(1)\n",
" stack.push(2)\n", " stack.push(2)\n",
" stack.push(3)\n", " stack.push(3)\n",
" assert_equal(stack.pop(), 3)\n", " self.assertEqual(stack.pop(), 3)\n",
" assert_equal(stack.peek(), 2)\n", " self.assertEqual(stack.peek(), 2)\n",
" assert_equal(stack.pop(), 2)\n", " self.assertEqual(stack.pop(), 2)\n",
" assert_equal(stack.peek(), 1)\n", " self.assertEqual(stack.peek(), 1)\n",
" assert_equal(stack.is_empty(), False)\n", " self.assertEqual(stack.is_empty(), False)\n",
" assert_equal(stack.pop(), 1)\n", " self.assertEqual(stack.pop(), 1)\n",
" assert_equal(stack.peek(), None)\n", " self.assertEqual(stack.peek(), None)\n",
" assert_equal(stack.is_empty(), True)\n", " self.assertEqual(stack.is_empty(), True)\n",
"\n", "\n",
" print('Success: test_end_to_end')\n", " print('Success: test_end_to_end')\n",
"\n", "\n",
@ -214,9 +210,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.0" "version": "3.7.2"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -124,9 +124,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -170,9 +168,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"%run stack.py" "%run stack.py"
@ -189,9 +185,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 3,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -203,38 +197,38 @@
], ],
"source": [ "source": [
"%%writefile test_stack.py\n", "%%writefile test_stack.py\n",
"from nose.tools import assert_equal\n", "import unittest\n",
"\n", "\n",
"\n", "\n",
"class TestStack(object):\n", "class TestStack(unittest.TestCase):\n",
"\n", "\n",
" # TODO: It would be better if we had unit tests for each\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", " # method in addition to the following end-to-end test\n",
" def test_end_to_end(self):\n", " def test_end_to_end(self):\n",
" print('Test: Empty stack')\n", " print('Test: Empty stack')\n",
" stack = Stack()\n", " stack = Stack()\n",
" assert_equal(stack.peek(), None)\n", " self.assertEqual(stack.peek(), None)\n",
" assert_equal(stack.pop(), None)\n", " self.assertEqual(stack.pop(), None)\n",
"\n", "\n",
" print('Test: One element')\n", " print('Test: One element')\n",
" top = Node(5)\n", " top = Node(5)\n",
" stack = Stack(top)\n", " stack = Stack(top)\n",
" assert_equal(stack.pop(), 5)\n", " self.assertEqual(stack.pop(), 5)\n",
" assert_equal(stack.peek(), None)\n", " self.assertEqual(stack.peek(), None)\n",
"\n", "\n",
" print('Test: More than one element')\n", " print('Test: More than one element')\n",
" stack = Stack()\n", " stack = Stack()\n",
" stack.push(1)\n", " stack.push(1)\n",
" stack.push(2)\n", " stack.push(2)\n",
" stack.push(3)\n", " stack.push(3)\n",
" assert_equal(stack.pop(), 3)\n", " self.assertEqual(stack.pop(), 3)\n",
" assert_equal(stack.peek(), 2)\n", " self.assertEqual(stack.peek(), 2)\n",
" assert_equal(stack.pop(), 2)\n", " self.assertEqual(stack.pop(), 2)\n",
" assert_equal(stack.peek(), 1)\n", " self.assertEqual(stack.peek(), 1)\n",
" assert_equal(stack.is_empty(), False)\n", " self.assertEqual(stack.is_empty(), False)\n",
" assert_equal(stack.pop(), 1)\n", " self.assertEqual(stack.pop(), 1)\n",
" assert_equal(stack.peek(), None)\n", " self.assertEqual(stack.peek(), None)\n",
" assert_equal(stack.is_empty(), True)\n", " self.assertEqual(stack.is_empty(), True)\n",
"\n", "\n",
" print('Success: test_end_to_end')\n", " print('Success: test_end_to_end')\n",
"\n", "\n",
@ -251,9 +245,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 4,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -321,9 +313,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.0" "version": "3.7.2"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -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 # TODO: It would be better if we had unit tests for each
# method in addition to the following end-to-end test # method in addition to the following end-to-end test
def test_end_to_end(self): def test_end_to_end(self):
print('Test: Empty stack') print('Test: Empty stack')
stack = Stack() stack = Stack()
assert_equal(stack.peek(), None) self.assertEqual(stack.peek(), None)
assert_equal(stack.pop(), None) self.assertEqual(stack.pop(), None)
print('Test: One element') print('Test: One element')
top = Node(5) top = Node(5)
stack = Stack(top) stack = Stack(top)
assert_equal(stack.pop(), 5) self.assertEqual(stack.pop(), 5)
assert_equal(stack.peek(), None) self.assertEqual(stack.peek(), None)
print('Test: More than one element') print('Test: More than one element')
stack = Stack() stack = Stack()
stack.push(1) stack.push(1)
stack.push(2) stack.push(2)
stack.push(3) stack.push(3)
assert_equal(stack.pop(), 3) self.assertEqual(stack.pop(), 3)
assert_equal(stack.peek(), 2) self.assertEqual(stack.peek(), 2)
assert_equal(stack.pop(), 2) self.assertEqual(stack.pop(), 2)
assert_equal(stack.peek(), 1) self.assertEqual(stack.peek(), 1)
assert_equal(stack.is_empty(), False) self.assertEqual(stack.is_empty(), False)
assert_equal(stack.pop(), 1) self.assertEqual(stack.pop(), 1)
assert_equal(stack.peek(), None) self.assertEqual(stack.peek(), None)
assert_equal(stack.is_empty(), True) self.assertEqual(stack.is_empty(), True)
print('Success: test_end_to_end') print('Success: test_end_to_end')
@ -40,4 +40,4 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -130,45 +130,43 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"# %load test_stack_min.py\n", "# %load test_stack_min.py\n",
"from nose.tools import assert_equal\n", "import unittest\n",
"\n", "\n",
"\n", "\n",
"class TestStackMin(object):\n", "class TestStackMin(unittest.TestCase):\n",
"\n", "\n",
" def test_stack_min(self):\n", " def test_stack_min(self):\n",
" print('Test: Push on empty stack, non-empty stack')\n", " print('Test: Push on empty stack, non-empty stack')\n",
" stack = StackMin()\n", " stack = StackMin()\n",
" stack.push(5)\n", " stack.push(5)\n",
" assert_equal(stack.peek(), 5)\n", " self.assertEqual(stack.peek(), 5)\n",
" assert_equal(stack.minimum(), 5)\n", " self.assertEqual(stack.minimum(), 5)\n",
" stack.push(1)\n", " stack.push(1)\n",
" assert_equal(stack.peek(), 1)\n", " self.assertEqual(stack.peek(), 1)\n",
" assert_equal(stack.minimum(), 1)\n", " self.assertEqual(stack.minimum(), 1)\n",
" stack.push(3)\n", " stack.push(3)\n",
" assert_equal(stack.peek(), 3)\n", " self.assertEqual(stack.peek(), 3)\n",
" assert_equal(stack.minimum(), 1)\n", " self.assertEqual(stack.minimum(), 1)\n",
" stack.push(0)\n", " stack.push(0)\n",
" assert_equal(stack.peek(), 0)\n", " self.assertEqual(stack.peek(), 0)\n",
" assert_equal(stack.minimum(), 0)\n", " self.assertEqual(stack.minimum(), 0)\n",
"\n", "\n",
" print('Test: Pop on non-empty stack')\n", " print('Test: Pop on non-empty stack')\n",
" assert_equal(stack.pop(), 0)\n", " self.assertEqual(stack.pop(), 0)\n",
" assert_equal(stack.minimum(), 1)\n", " self.assertEqual(stack.minimum(), 1)\n",
" assert_equal(stack.pop(), 3)\n", " self.assertEqual(stack.pop(), 3)\n",
" assert_equal(stack.minimum(), 1)\n", " self.assertEqual(stack.minimum(), 1)\n",
" assert_equal(stack.pop(), 1)\n", " self.assertEqual(stack.pop(), 1)\n",
" assert_equal(stack.minimum(), 5)\n", " self.assertEqual(stack.minimum(), 5)\n",
" assert_equal(stack.pop(), 5)\n", " self.assertEqual(stack.pop(), 5)\n",
" assert_equal(stack.minimum(), sys.maxsize)\n", " self.assertEqual(stack.minimum(), sys.maxsize)\n",
"\n", "\n",
" print('Test: Pop empty stack')\n", " print('Test: Pop empty stack')\n",
" assert_equal(stack.pop(), None)\n", " self.assertEqual(stack.pop(), None)\n",
"\n", "\n",
" print('Success: test_stack_min')\n", " print('Success: test_stack_min')\n",
"\n", "\n",
@ -208,9 +206,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.0" "version": "3.7.2"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -106,9 +106,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"%run ../stack/stack.py" "%run ../stack/stack.py"
@ -117,9 +115,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"import sys\n", "import sys\n",
@ -160,9 +156,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 3,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -174,39 +168,39 @@
], ],
"source": [ "source": [
"%%writefile test_stack_min.py\n", "%%writefile test_stack_min.py\n",
"from nose.tools import assert_equal\n", "import unittest\n",
"\n", "\n",
"\n", "\n",
"class TestStackMin(object):\n", "class TestStackMin(unittest.TestCase):\n",
"\n", "\n",
" def test_stack_min(self):\n", " def test_stack_min(self):\n",
" print('Test: Push on empty stack, non-empty stack')\n", " print('Test: Push on empty stack, non-empty stack')\n",
" stack = StackMin()\n", " stack = StackMin()\n",
" stack.push(5)\n", " stack.push(5)\n",
" assert_equal(stack.peek(), 5)\n", " self.assertEqual(stack.peek(), 5)\n",
" assert_equal(stack.minimum(), 5)\n", " self.assertEqual(stack.minimum(), 5)\n",
" stack.push(1)\n", " stack.push(1)\n",
" assert_equal(stack.peek(), 1)\n", " self.assertEqual(stack.peek(), 1)\n",
" assert_equal(stack.minimum(), 1)\n", " self.assertEqual(stack.minimum(), 1)\n",
" stack.push(3)\n", " stack.push(3)\n",
" assert_equal(stack.peek(), 3)\n", " self.assertEqual(stack.peek(), 3)\n",
" assert_equal(stack.minimum(), 1)\n", " self.assertEqual(stack.minimum(), 1)\n",
" stack.push(0)\n", " stack.push(0)\n",
" assert_equal(stack.peek(), 0)\n", " self.assertEqual(stack.peek(), 0)\n",
" assert_equal(stack.minimum(), 0)\n", " self.assertEqual(stack.minimum(), 0)\n",
"\n", "\n",
" print('Test: Pop on non-empty stack')\n", " print('Test: Pop on non-empty stack')\n",
" assert_equal(stack.pop(), 0)\n", " self.assertEqual(stack.pop(), 0)\n",
" assert_equal(stack.minimum(), 1)\n", " self.assertEqual(stack.minimum(), 1)\n",
" assert_equal(stack.pop(), 3)\n", " self.assertEqual(stack.pop(), 3)\n",
" assert_equal(stack.minimum(), 1)\n", " self.assertEqual(stack.minimum(), 1)\n",
" assert_equal(stack.pop(), 1)\n", " self.assertEqual(stack.pop(), 1)\n",
" assert_equal(stack.minimum(), 5)\n", " self.assertEqual(stack.minimum(), 5)\n",
" assert_equal(stack.pop(), 5)\n", " self.assertEqual(stack.pop(), 5)\n",
" assert_equal(stack.minimum(), sys.maxsize)\n", " self.assertEqual(stack.minimum(), sys.maxsize)\n",
"\n", "\n",
" print('Test: Pop empty stack')\n", " print('Test: Pop empty stack')\n",
" assert_equal(stack.pop(), None)\n", " self.assertEqual(stack.pop(), None)\n",
"\n", "\n",
" print('Success: test_stack_min')\n", " print('Success: test_stack_min')\n",
"\n", "\n",
@ -223,9 +217,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 4,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -259,9 +251,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.0" "version": "3.7.2"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -1,37 +1,36 @@
import sys import unittest
from nose.tools import assert_equal
class TestStackMin(object): class TestStackMin(unittest.TestCase):
def test_stack_min(self): def test_stack_min(self):
print('Test: Push on empty stack, non-empty stack') print('Test: Push on empty stack, non-empty stack')
stack = StackMin() stack = StackMin()
stack.push(5) stack.push(5)
assert_equal(stack.peek(), 5) self.assertEqual(stack.peek(), 5)
assert_equal(stack.minimum(), 5) self.assertEqual(stack.minimum(), 5)
stack.push(1) stack.push(1)
assert_equal(stack.peek(), 1) self.assertEqual(stack.peek(), 1)
assert_equal(stack.minimum(), 1) self.assertEqual(stack.minimum(), 1)
stack.push(3) stack.push(3)
assert_equal(stack.peek(), 3) self.assertEqual(stack.peek(), 3)
assert_equal(stack.minimum(), 1) self.assertEqual(stack.minimum(), 1)
stack.push(0) stack.push(0)
assert_equal(stack.peek(), 0) self.assertEqual(stack.peek(), 0)
assert_equal(stack.minimum(), 0) self.assertEqual(stack.minimum(), 0)
print('Test: Pop on non-empty stack') print('Test: Pop on non-empty stack')
assert_equal(stack.pop(), 0) self.assertEqual(stack.pop(), 0)
assert_equal(stack.minimum(), 1) self.assertEqual(stack.minimum(), 1)
assert_equal(stack.pop(), 3) self.assertEqual(stack.pop(), 3)
assert_equal(stack.minimum(), 1) self.assertEqual(stack.minimum(), 1)
assert_equal(stack.pop(), 1) self.assertEqual(stack.pop(), 1)
assert_equal(stack.minimum(), 5) self.assertEqual(stack.minimum(), 5)
assert_equal(stack.pop(), 5) self.assertEqual(stack.pop(), 5)
assert_equal(stack.minimum(), sys.maxsize) self.assertEqual(stack.minimum(), sys.maxsize)
print('Test: Pop empty stack') print('Test: Pop empty stack')
assert_equal(stack.pop(), None) self.assertEqual(stack.pop(), None)
print('Success: test_stack_min') print('Success: test_stack_min')