Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 17 additions & 13 deletions linked_lists/linked_list/linked_list_challenge.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"class Node(object):\n",
Expand Down Expand Up @@ -173,9 +171,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# %load test_linked_list.py\n",
Expand Down Expand Up @@ -255,7 +251,15 @@
" linked_list = LinkedList(head)\n",
" linked_list.delete(None)\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",
" head = Node(10)\n",
" linked_list = LinkedList(head)\n",
Expand Down Expand Up @@ -310,23 +314,23 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 2",
"language": "python",
"name": "python3"
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}
43 changes: 22 additions & 21 deletions linked_lists/linked_list/linked_list_solution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,8 @@
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -260,10 +258,10 @@
" return\n",
" if self.head is None:\n",
" return\n",
" curr_node = self.head\n",
" if curr_node.data == data:\n",
" curr_node = curr_node.next\n",
" if self.head.data == data:\n",
" self.head = self.head.next\n",
" return\n",
" curr_node = self.head\n",
" while curr_node.next is not None:\n",
" if curr_node.next.data == data:\n",
" curr_node.next = curr_node.next.next\n",
Expand All @@ -287,10 +285,8 @@
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"%run linked_list.py"
Expand All @@ -305,10 +301,8 @@
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -396,6 +390,14 @@
" linked_list = LinkedList(head)\n",
" linked_list.delete(None)\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",
" print('Test: delete general case with matches')\n",
" head = Node(10)\n",
Expand Down Expand Up @@ -441,10 +443,8 @@
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand All @@ -468,6 +468,7 @@
"\n",
"Test: delete on an empty list\n",
"Test: delete a None\n",
"Test: delete general case with match at the head\n",
"Test: delete general case with matches\n",
"Test: delete general case with no matches\n",
"Success: test_delete\n",
Expand Down Expand Up @@ -501,9 +502,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}