mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-09 02:48:02 +00:00
@@ -171,9 +171,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
@@ -288,9 +286,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run linked_list.py"
|
||||
@@ -306,9 +302,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
@@ -320,25 +314,25 @@
|
||||
],
|
||||
"source": [
|
||||
"%%writefile test_linked_list.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestLinkedList(object):\n",
|
||||
"class TestLinkedList(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_insert_to_front(self):\n",
|
||||
" print('Test: insert_to_front on an empty list')\n",
|
||||
" linked_list = LinkedList(None)\n",
|
||||
" linked_list.insert_to_front(10)\n",
|
||||
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||
" self.assertEqual(linked_list.get_all_data(), [10])\n",
|
||||
"\n",
|
||||
" print('Test: insert_to_front on a None')\n",
|
||||
" linked_list.insert_to_front(None)\n",
|
||||
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||
" self.assertEqual(linked_list.get_all_data(), [10])\n",
|
||||
"\n",
|
||||
" print('Test: insert_to_front general case')\n",
|
||||
" linked_list.insert_to_front('a')\n",
|
||||
" linked_list.insert_to_front('bc')\n",
|
||||
" assert_equal(linked_list.get_all_data(), ['bc', 'a', 10])\n",
|
||||
" self.assertEqual(linked_list.get_all_data(), ['bc', 'a', 10])\n",
|
||||
"\n",
|
||||
" print('Success: test_insert_to_front\\n')\n",
|
||||
"\n",
|
||||
@@ -346,16 +340,16 @@
|
||||
" print('Test: append on an empty list')\n",
|
||||
" linked_list = LinkedList(None)\n",
|
||||
" linked_list.append(10)\n",
|
||||
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||
" self.assertEqual(linked_list.get_all_data(), [10])\n",
|
||||
"\n",
|
||||
" print('Test: append a None')\n",
|
||||
" linked_list.append(None)\n",
|
||||
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||
" self.assertEqual(linked_list.get_all_data(), [10])\n",
|
||||
"\n",
|
||||
" print('Test: append general case')\n",
|
||||
" linked_list.append('a')\n",
|
||||
" linked_list.append('bc')\n",
|
||||
" assert_equal(linked_list.get_all_data(), [10, 'a', 'bc'])\n",
|
||||
" self.assertEqual(linked_list.get_all_data(), [10, 'a', 'bc'])\n",
|
||||
"\n",
|
||||
" print('Success: test_append\\n')\n",
|
||||
"\n",
|
||||
@@ -363,13 +357,13 @@
|
||||
" print('Test: find on an empty list')\n",
|
||||
" linked_list = LinkedList(None)\n",
|
||||
" node = linked_list.find('a')\n",
|
||||
" assert_equal(node, None)\n",
|
||||
" self.assertEqual(node, None)\n",
|
||||
"\n",
|
||||
" print('Test: find a None')\n",
|
||||
" head = Node(10)\n",
|
||||
" linked_list = LinkedList(head)\n",
|
||||
" node = linked_list.find(None)\n",
|
||||
" assert_equal(node, None)\n",
|
||||
" self.assertEqual(node, None)\n",
|
||||
"\n",
|
||||
" print('Test: find general case with matches')\n",
|
||||
" head = Node(10)\n",
|
||||
@@ -377,11 +371,11 @@
|
||||
" linked_list.insert_to_front('a')\n",
|
||||
" linked_list.insert_to_front('bc')\n",
|
||||
" node = linked_list.find('a')\n",
|
||||
" assert_equal(str(node), 'a')\n",
|
||||
" self.assertEqual(str(node), 'a')\n",
|
||||
"\n",
|
||||
" print('Test: find general case with no matches')\n",
|
||||
" node = linked_list.find('aaa')\n",
|
||||
" assert_equal(node, None)\n",
|
||||
" self.assertEqual(node, None)\n",
|
||||
"\n",
|
||||
" print('Success: test_find\\n')\n",
|
||||
"\n",
|
||||
@@ -389,13 +383,13 @@
|
||||
" print('Test: delete on an empty list')\n",
|
||||
" linked_list = LinkedList(None)\n",
|
||||
" linked_list.delete('a')\n",
|
||||
" assert_equal(linked_list.get_all_data(), [])\n",
|
||||
" self.assertEqual(linked_list.get_all_data(), [])\n",
|
||||
"\n",
|
||||
" print('Test: delete a None')\n",
|
||||
" head = Node(10)\n",
|
||||
" linked_list = LinkedList(head)\n",
|
||||
" linked_list.delete(None)\n",
|
||||
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||
" self.assertEqual(linked_list.get_all_data(), [10])\n",
|
||||
"\n",
|
||||
" print('Test: delete general case with matches')\n",
|
||||
" head = Node(10)\n",
|
||||
@@ -403,25 +397,25 @@
|
||||
" linked_list.insert_to_front('a')\n",
|
||||
" linked_list.insert_to_front('bc')\n",
|
||||
" linked_list.delete('a')\n",
|
||||
" assert_equal(linked_list.get_all_data(), ['bc', 10])\n",
|
||||
" self.assertEqual(linked_list.get_all_data(), ['bc', 10])\n",
|
||||
"\n",
|
||||
" print('Test: delete general case with no matches')\n",
|
||||
" linked_list.delete('aa')\n",
|
||||
" assert_equal(linked_list.get_all_data(), ['bc', 10])\n",
|
||||
" self.assertEqual(linked_list.get_all_data(), ['bc', 10])\n",
|
||||
"\n",
|
||||
" print('Success: test_delete\\n')\n",
|
||||
"\n",
|
||||
" def test_len(self):\n",
|
||||
" print('Test: len on an empty list')\n",
|
||||
" linked_list = LinkedList(None)\n",
|
||||
" assert_equal(len(linked_list), 0)\n",
|
||||
" self.assertEqual(len(linked_list), 0)\n",
|
||||
"\n",
|
||||
" print('Test: len general case')\n",
|
||||
" head = Node(10)\n",
|
||||
" linked_list = LinkedList(head)\n",
|
||||
" linked_list.insert_to_front('a')\n",
|
||||
" linked_list.insert_to_front('bc')\n",
|
||||
" assert_equal(len(linked_list), 3)\n",
|
||||
" self.assertEqual(len(linked_list), 3)\n",
|
||||
"\n",
|
||||
" print('Success: test_len\\n')\n",
|
||||
"\n",
|
||||
@@ -442,9 +436,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
@@ -485,25 +477,24 @@
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"celltoolbar": "Edit Metadata",
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.12"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user