add test case for properly deleting the head node of a linked list

This commit is contained in:
Rick 2018-01-27 00:23:13 -05:00
parent b4a64f78dc
commit 9ab9d70229
2 changed files with 31 additions and 27 deletions

View File

@ -103,9 +103,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",
@ -173,9 +171,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"# %load test_linked_list.py\n", "# %load test_linked_list.py\n",
@ -255,7 +251,15 @@
" linked_list = LinkedList(head)\n", " linked_list = LinkedList(head)\n",
" linked_list.delete(None)\n", " linked_list.delete(None)\n",
" assert_equal(linked_list.get_all_data(), [10])\n", " assert_equal(linked_list.get_all_data(), [10])\n",
"\n", " \n",
" print('Test: delete general case with match at the head')\n",
" head = Node(10)\n",
" linked_list = LinkedList(head)\n",
" linked_list.insert_to_front('a')\n",
" linked_list.insert_to_front('bc')\n",
" linked_list.delete('bc')\n",
" assert_equal(linked_list.get_all_data(), ['a', 10])\n",
" \n",
" print('Test: delete general case with matches')\n", " print('Test: delete general case with matches')\n",
" head = Node(10)\n", " head = Node(10)\n",
" linked_list = LinkedList(head)\n", " linked_list = LinkedList(head)\n",
@ -310,23 +314,23 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 3", "display_name": "Python 2",
"language": "python", "language": "python",
"name": "python3" "name": "python2"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 3 "version": 2
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython2",
"version": "3.5.0" "version": "2.7.13"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -171,9 +171,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -288,9 +286,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"%run linked_list.py" "%run linked_list.py"
@ -306,9 +302,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 3,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -396,6 +390,14 @@
" linked_list = LinkedList(head)\n", " linked_list = LinkedList(head)\n",
" linked_list.delete(None)\n", " linked_list.delete(None)\n",
" assert_equal(linked_list.get_all_data(), [10])\n", " assert_equal(linked_list.get_all_data(), [10])\n",
" \n",
" print('Test: delete general case with match at the head')\n",
" head = Node(10)\n",
" linked_list = LinkedList(head)\n",
" linked_list.insert_to_front('a')\n",
" linked_list.insert_to_front('bc')\n",
" linked_list.delete('bc')\n",
" assert_equal(linked_list.get_all_data(), ['a', 10])\n",
"\n", "\n",
" print('Test: delete general case with matches')\n", " print('Test: delete general case with matches')\n",
" head = Node(10)\n", " head = Node(10)\n",
@ -442,9 +444,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 4,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -501,9 +501,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython2", "pygments_lexer": "ipython2",
"version": "2.7.12" "version": "2.7.13"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }