From 3864cd2cdd1e5460fcdc10a0af4d8568b71942b5 Mon Sep 17 00:00:00 2001 From: Young Jin J Date: Fri, 5 Jun 2020 14:09:05 -0400 Subject: [PATCH 1/8] Fork, array sequence interview questions --- .../Big-O Examples-checkpoint.ipynb | 377 +++++ .../Big O Examples .ipynb | 2 +- .../Big-O Examples Lecture.ipynb | 427 ++++++ Array Sequences/Amortization.ipynb | 6 +- .../Anagram Check .ipynb | 20 +- .../Anagram Check - SOLUTION.ipynb | 4 +- .../Anagram Check .ipynb | 56 +- .../Array Pair Sum .ipynb | 51 +- Array Sequences/Dynamic Array Exercise.ipynb | 4 +- Practice.ipynb | 1293 ++--------------- 10 files changed, 999 insertions(+), 1241 deletions(-) create mode 100644 Algorithm Analysis and Big O/.ipynb_checkpoints/Big-O Examples-checkpoint.ipynb create mode 100644 Algorithm Analysis and Big O/Big-O Examples Lecture.ipynb diff --git a/Algorithm Analysis and Big O/.ipynb_checkpoints/Big-O Examples-checkpoint.ipynb b/Algorithm Analysis and Big O/.ipynb_checkpoints/Big-O Examples-checkpoint.ipynb new file mode 100644 index 00000000..4726d67a --- /dev/null +++ b/Algorithm Analysis and Big O/.ipynb_checkpoints/Big-O Examples-checkpoint.ipynb @@ -0,0 +1,377 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "def func_constant(values):\n", + " print(values[0])\n", + "\n", + "lst = [1,2,3]\n", + "func_constant((lst))\n", + " \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# O(n) Linear" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "def func_lin(lst):\n", + " for val in lst:\n", + " print(val)\n", + "func_lin(lst)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# O(n^2) Quadratic" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 1\n", + "1 2\n", + "1 3\n", + "2 1\n", + "2 2\n", + "2 3\n", + "3 1\n", + "3 2\n", + "3 3\n" + ] + } + ], + "source": [ + "def func_quad(lst):\n", + " for item_1 in lst:\n", + " for item_2 in lst:\n", + " print (item_1, item_2)\n", + "func_quad(lst)\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Big-O Calculation\n", + "When it comes to the Big-O notation, only most least significant is dropped." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "def print_once(lst): #O(n)\n", + " for val in lst:\n", + " print (val)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "print_once(lst)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "def print_2(lst): #O(n)\n", + " for val in lst:\n", + " print (val)\n", + " for val in lst:\n", + " print (val)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "1\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "print_2(lst)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "def comp(lst):\n", + " print (lst[0]) #O(1)\n", + "\n", + " #### #O(n/2) = O(1/2 * n)\n", + " midpoint = len(lst)//2\n", + " for val in lst[:midpoint]:\n", + " print (val)\n", + " ####\n", + "\n", + " for x in range(10): #O(10)\n", + " print ('hello world')\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "hello world\n", + "hello world\n", + "hello world\n", + "hello world\n", + "hello world\n", + "hello world\n", + "hello world\n", + "hello world\n", + "hello world\n", + "hello world\n" + ] + } + ], + "source": [ + "lst = [1,2,3,4,5,6,7,8,9,10]\n", + "comp(lst)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## O(1 + n/2 + 10)\n", + "We can see that n can get grow larger and larger making 1 and 10 insignificant.\n", + "Also 1/2 in n becomes insignificant as well when n is inf.\n", + "This means that this can be simplified to:\n", + "## O(n)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "def matcher (lst, match):\n", + " for item in lst:\n", + " if item == match:\n", + " return True\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "matcher(lst, 1) #Best case, O(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "matcher(lst, 11) #Worst case, O(n)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "def create_list(n):\n", + " new_list = []\n", + " for num in range(n):\n", + " new_list.append('new')\n", + " return new_list" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.2" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/Algorithm Analysis and Big O/Big O Examples .ipynb b/Algorithm Analysis and Big O/Big O Examples .ipynb index 9352c727..eeeb2d81 100644 --- a/Algorithm Analysis and Big O/Big O Examples .ipynb +++ b/Algorithm Analysis and Big O/Big O Examples .ipynb @@ -550,7 +550,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.2" } }, "nbformat": 4, diff --git a/Algorithm Analysis and Big O/Big-O Examples Lecture.ipynb b/Algorithm Analysis and Big O/Big-O Examples Lecture.ipynb new file mode 100644 index 00000000..55274726 --- /dev/null +++ b/Algorithm Analysis and Big O/Big-O Examples Lecture.ipynb @@ -0,0 +1,427 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "def func_constant(values):\n", + " print(values[0])\n", + "\n", + "lst = [1,2,3]\n", + "func_constant((lst))\n", + " \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# O(n) Linear" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "def func_lin(lst):\n", + " for val in lst:\n", + " print(val)\n", + "func_lin(lst)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# O(n^2) Quadratic" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 1\n", + "1 2\n", + "1 3\n", + "2 1\n", + "2 2\n", + "2 3\n", + "3 1\n", + "3 2\n", + "3 3\n" + ] + } + ], + "source": [ + "def func_quad(lst):\n", + " for item_1 in lst:\n", + " for item_2 in lst:\n", + " print (item_1, item_2)\n", + "func_quad(lst)\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Big-O Calculation\n", + "When it comes to the Big-O notation, only most least significant is dropped." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "def print_once(lst): #O(n)\n", + " for val in lst:\n", + " print (val)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "print_once(lst)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "def print_2(lst): #O(n)\n", + " for val in lst:\n", + " print (val)\n", + " for val in lst:\n", + " print (val)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "1\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "print_2(lst)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "def comp(lst):\n", + " print (lst[0]) #O(1)\n", + "\n", + " #### #O(n/2) = O(1/2 * n)\n", + " midpoint = len(lst)//2\n", + " for val in lst[:midpoint]:\n", + " print (val)\n", + " ####\n", + "\n", + " for x in range(10): #O(10)\n", + " print ('hello world')\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "hello world\n", + "hello world\n", + "hello world\n", + "hello world\n", + "hello world\n", + "hello world\n", + "hello world\n", + "hello world\n", + "hello world\n", + "hello world\n" + ] + } + ], + "source": [ + "lst = [1,2,3,4,5,6,7,8,9,10]\n", + "comp(lst)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## O(1 + n/2 + 10)\n", + "We can see that n can get grow larger and larger making 1 and 10 insignificant.\n", + "Also 1/2 in n becomes insignificant as well when n is inf.\n", + "This means that this can be simplified to:\n", + "## O(n)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "def matcher (lst, match):\n", + " for item in lst:\n", + " if item == match:\n", + " return True\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "matcher(lst, 1) #Best case, O(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "matcher(lst, 11) #Worst case, O(n)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "def create_list(n):\n", + " new_list = []\n", + " for num in range(n):\n", + " new_list.append('new')\n", + " return new_list" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "outputs": [ + { + "data": { + "text/plain": "['new', 'new', 'new', 'new', 'new']" + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "create_list(5)" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "def printer(n):\n", + " for x in range(10): #Time complexity O(n)\n", + " print('Hello world') #Space complexity O(1)" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "markdown", + "source": [ + "Time Complexity is Linear, O(n)\n", + "\n", + "Space Complexity is Constant, O(1) because it's printing out same thing over and over again\n" + ], + "metadata": { + "collapsed": false + } + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.2" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} \ No newline at end of file diff --git a/Array Sequences/Amortization.ipynb b/Array Sequences/Amortization.ipynb index d58ff51c..73c4e1c5 100644 --- a/Array Sequences/Amortization.ipynb +++ b/Array Sequences/Amortization.ipynb @@ -22,9 +22,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "name": "python3", "language": "python", - "name": "python2" + "display_name": "Python 3" }, "language_info": { "codemirror_mode": { @@ -41,4 +41,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - PRACTICE/Anagram Check .ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - PRACTICE/Anagram Check .ipynb index eac9c379..60bd4f06 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - PRACTICE/Anagram Check .ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - PRACTICE/Anagram Check .ipynb @@ -25,7 +25,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -132,7 +132,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -166,14 +166,18 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 3, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "ALL TEST CASES PASSED\n" + "ename": "NameError", + "evalue": "name 'anagram2' is not defined", + "output_type": "error", + "traceback": [ + "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", + "\u001B[0;31mNameError\u001B[0m Traceback (most recent call last)", + "\u001B[0;32m\u001B[0m in \u001B[0;36m\u001B[0;34m\u001B[0m\n\u001B[0;32m----> 1\u001B[0;31m \u001B[0mt\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mtest\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0managram2\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m 2\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n", + "\u001B[0;31mNameError\u001B[0m: name 'anagram2' is not defined" ] } ], @@ -210,4 +214,4 @@ }, "nbformat": 4, "nbformat_minor": 1 -} +} \ No newline at end of file diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Anagram Check - SOLUTION.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Anagram Check - SOLUTION.ipynb index 91e7d3d2..9af6fc79 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Anagram Check - SOLUTION.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Anagram Check - SOLUTION.ipynb @@ -302,9 +302,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 1 -} +} \ No newline at end of file diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Anagram Check .ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Anagram Check .ipynb index fd6f0824..86bedc90 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Anagram Check .ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Anagram Check .ipynb @@ -25,31 +25,45 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 89, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def anagram(s1,s2):\n", - " \n", - " pass" + " s1Nospace = s1.replace(' ', '').lower()\n", + " s2Nospace = s2.replace(' ', '').lower()\n", + " s1lst = list(s1Nospace)\n", + " s2lst = list(s2Nospace)\n", + "\n", + " for idx1, l1 in enumerate(s1lst):\n", + " for idx2, l2 in enumerate(s2lst):\n", + " if l1 == l2:\n", + " s1lst[idx1] = 0\n", + " s2lst[idx2] = 0\n", + " break\n", + " try:\n", + " if not sum(s1lst) and not sum(s2lst):\n", + " return True\n", + " else:\n", + " return False\n", + " except:\n", + " return False\n" ] }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 90, "metadata": { "collapsed": false }, "outputs": [ { "data": { - "text/plain": [ - "True" - ] + "text/plain": "True" }, - "execution_count": 27, + "execution_count": 90, "metadata": {}, "output_type": "execute_result" } @@ -60,18 +74,16 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 91, "metadata": { "collapsed": false }, "outputs": [ { "data": { - "text/plain": [ - "True" - ] + "text/plain": "True" }, - "execution_count": 34, + "execution_count": 91, "metadata": {}, "output_type": "execute_result" } @@ -82,18 +94,16 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 92, "metadata": { "collapsed": false }, "outputs": [ { "data": { - "text/plain": [ - "False" - ] + "text/plain": "False" }, - "execution_count": 35, + "execution_count": 92, "metadata": {}, "output_type": "execute_result" } @@ -112,7 +122,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 93, "metadata": { "collapsed": false }, @@ -139,7 +149,7 @@ " assert_equal(sol('hi man','hi man'),True)\n", " assert_equal(sol('aabbcc','aabbc'),False)\n", " assert_equal(sol('123','1 2'),False)\n", - " print \"ALL TEST CASES PASSED\"\n", + " print (\"ALL TEST CASES PASSED\")\n", "\n", "# Run Tests\n", "t = AnagramTest()\n", @@ -175,9 +185,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "name": "python3", "language": "python", - "name": "python2" + "display_name": "Python 3" }, "language_info": { "codemirror_mode": { @@ -194,4 +204,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Array Pair Sum .ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Array Pair Sum .ipynb index 50ee6bfa..89f535b6 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Array Pair Sum .ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Array Pair Sum .ipynb @@ -8,7 +8,7 @@ "\n", "## Problem\n", "\n", - "Given an integer array, output all the ** *unique* ** pairs that sum up to a specific value **k**.\n", + "Given an integer array, output all the ***unique*** pairs that sum up to a specific value **k**.\n", "\n", "So the input:\n", " \n", @@ -28,31 +28,36 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 89, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def pair_sum(arr,k):\n", - " \n", - " pass" + " count= 0\n", + " lst = list()\n", + " for idx, ele in enumerate(arr):\n", + " for i in range(len(arr)):\n", + " if i != idx:\n", + " if ele+arr[i] == k:\n", + " pair = (ele, arr[i])\n", + " lst.append(tuple(sorted(pair)))\n", + "\n", + " lst = set(lst)\n", + " return len(lst)" ] }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 90, + "metadata": {}, "outputs": [ { "data": { - "text/plain": [ - "2" - ] + "text/plain": "2" }, - "execution_count": 14, + "execution_count": 90, "metadata": {}, "output_type": "execute_result" } @@ -70,10 +75,8 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, + "execution_count": 92, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -95,7 +98,7 @@ " assert_equal(sol([1,9,2,8,3,7,4,6,5,5,13,14,11,13,-1],10),6)\n", " assert_equal(sol([1,2,3,1],3),1)\n", " assert_equal(sol([1,3,2,2],4),2)\n", - " print 'ALL TEST CASES PASSED'\n", + " print ('ALL TEST CASES PASSED')\n", " \n", "#Run tests\n", "t = TestPair()\n", @@ -113,23 +116,23 @@ ], "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.10" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 -} + "nbformat_minor": 1 +} \ No newline at end of file diff --git a/Array Sequences/Dynamic Array Exercise.ipynb b/Array Sequences/Dynamic Array Exercise.ipynb index 2b484bd7..413fdf09 100644 --- a/Array Sequences/Dynamic Array Exercise.ipynb +++ b/Array Sequences/Dynamic Array Exercise.ipynb @@ -301,9 +301,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 1 -} +} \ No newline at end of file diff --git a/Practice.ipynb b/Practice.ipynb index b2ecf2ed..8e1766ec 100644 --- a/Practice.ipynb +++ b/Practice.ipynb @@ -2,16 +2,14 @@ "cells": [ { "cell_type": "code", - "execution_count": 5, + "execution_count": 1, "metadata": {}, "outputs": [ { "data": { - "text/plain": [ - "'HELLO'" - ] + "text/plain": "'HELLO'" }, - "execution_count": 5, + "execution_count": 1, "metadata": {}, "output_type": "execute_result" } @@ -23,7 +21,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -41,16 +39,14 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 3, "metadata": {}, "outputs": [ { "data": { - "text/plain": [ - "('hello',)" - ] + "text/plain": "('hello',)" }, - "execution_count": 6, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -64,18 +60,13 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "{'banana', 'apple', 'orange', 'pear'}\n" - ] + "execution_count": null, + "metadata": { + "pycharm": { + "is_executing": true } - ], + }, + "outputs": [], "source": [ "basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}\n", "print(type(basket))\n", @@ -84,17 +75,13 @@ }, { "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{' ', 't', 's', 'i', 'h', 'o', 'e', 'n', 'm', 'g'}\n" - ] + "execution_count": null, + "metadata": { + "pycharm": { + "is_executing": true } - ], + }, + "outputs": [], "source": [ "chars = {c for c in 'this is something'}\n", "print(chars)\n" @@ -102,38 +89,26 @@ }, { "cell_type": "code", - "execution_count": 57, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 57, - "metadata": {}, - "output_type": "execute_result" + "execution_count": null, + "metadata": { + "pycharm": { + "is_executing": true } - ], + }, + "outputs": [], "source": [ "s = 'A man, a plan, a canal: Panama'\n" ] }, { "cell_type": "code", - "execution_count": 66, - "metadata": {}, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "'return' outside function (, line 7)", - "output_type": "error", - "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m7\u001b[0m\n\u001b[0;31m return False\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m 'return' outside function\n" - ] + "execution_count": null, + "metadata": { + "pycharm": { + "is_executing": true } - ], + }, + "outputs": [], "source": [ "s='ab'\n", "s=s.casefold()\n", @@ -147,8 +122,12 @@ }, { "cell_type": "code", - "execution_count": 109, - "metadata": {}, + "execution_count": null, + "metadata": { + "pycharm": { + "is_executing": true + } + }, "outputs": [], "source": [ "s='aba'\n", @@ -157,27 +136,13 @@ }, { "cell_type": "code", - "execution_count": 110, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "bbc\n" - ] - }, - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 110, - "metadata": {}, - "output_type": "execute_result" + "execution_count": null, + "metadata": { + "pycharm": { + "is_executing": true } - ], + }, + "outputs": [], "source": [ "print(s)\n", "s = ''.join(filter(lambda c: c.isalnum(), s)).lower()\n", @@ -191,7 +156,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "pycharm": { + "is_executing": true + } + }, "outputs": [], "source": [ ">>> a_string = 'amanaplanacanalpanama' * 10\n", @@ -207,8 +176,12 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": {}, + "execution_count": null, + "metadata": { + "pycharm": { + "is_executing": true + } + }, "outputs": [], "source": [ "s = 'amanaplanacanalpanama'" @@ -216,8 +189,12 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": {}, + "execution_count": null, + "metadata": { + "pycharm": { + "is_executing": true + } + }, "outputs": [], "source": [ "\n", @@ -229,20 +206,13 @@ }, { "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0.00010040099732577801" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" + "execution_count": null, + "metadata": { + "pycharm": { + "is_executing": true } - ], + }, + "outputs": [], "source": [ "import timeit\n", "\n" @@ -250,83 +220,62 @@ }, { "cell_type": "code", - "execution_count": 44, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0.9958420610055327" - ] - }, - "execution_count": 44, - "metadata": {}, - "output_type": "execute_result" + "execution_count": null, + "metadata": { + "pycharm": { + "is_executing": true } - ], + }, + "outputs": [], "source": [] }, { "cell_type": "code", - "execution_count": 48, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1.6800561340060085" - ] - }, - "execution_count": 48, - "metadata": {}, - "output_type": "execute_result" + "execution_count": null, + "metadata": { + "pycharm": { + "is_executing": true } - ], + }, + "outputs": [], "source": [ "timeit.timeit('\"amanaplanacanalpanama\"[::-1]', number=10000000)" ] }, { "cell_type": "code", - "execution_count": 49, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "9.636620013043284" - ] - }, - "execution_count": 49, - "metadata": {}, - "output_type": "execute_result" + "execution_count": null, + "metadata": { + "pycharm": { + "is_executing": true } - ], + }, + "outputs": [], "source": [ "timeit.timeit('\"\".join(reversed(\"amanaplanacanalpanama\"))', number=10000000)" ] }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "/Users/bin/Documents/Workspace/GitHub/Python-for-Algorithm-and-Interviews\r\n" - ] + "execution_count": null, + "metadata": { + "pycharm": { + "is_executing": true } - ], + }, + "outputs": [], "source": [ "!pwd" ] }, { "cell_type": "code", - "execution_count": 2, - "metadata": {}, + "execution_count": null, + "metadata": { + "pycharm": { + "is_executing": true + } + }, "outputs": [], "source": [ "!say 'hi'" @@ -334,1042 +283,26 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "ename": "ValueError", - "evalue": "stmt is neither a string nor callable", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtimeit\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mn\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mn\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1000\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mnumber\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1000\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/timeit.py\u001b[0m in \u001b[0;36mtimeit\u001b[0;34m(stmt, setup, timer, number, globals)\u001b[0m\n\u001b[1;32m 231\u001b[0m number=default_number, globals=None):\n\u001b[1;32m 232\u001b[0m \u001b[0;34m\"\"\"Convenience function to create Timer object and call timeit method.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 233\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mTimer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstmt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msetup\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mglobals\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumber\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 234\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 235\u001b[0m def repeat(stmt=\"pass\", setup=\"pass\", timer=default_timer,\n", - "\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/timeit.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, stmt, setup, timer, globals)\u001b[0m\n\u001b[1;32m 128\u001b[0m \u001b[0mstmt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'_stmt()'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 129\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 130\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"stmt is neither a string nor callable\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 131\u001b[0m \u001b[0msrc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtemplate\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstmt\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mstmt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msetup\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msetup\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minit\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minit\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 132\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msrc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msrc\u001b[0m \u001b[0;31m# Save for traceback display\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mValueError\u001b[0m: stmt is neither a string nor callable" - ] + "execution_count": null, + "metadata": { + "pycharm": { + "is_executing": true } - ], + }, + "outputs": [], "source": [ "timeit.timeit(,number=1000)" ] }, { "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[0,\n", - " 2,\n", - " 4,\n", - " 6,\n", - " 8,\n", - " 10,\n", - " 12,\n", - " 14,\n", - " 16,\n", - " 18,\n", - " 20,\n", - " 22,\n", - " 24,\n", - " 26,\n", - " 28,\n", - " 30,\n", - " 32,\n", - " 34,\n", - " 36,\n", - " 38,\n", - " 40,\n", - " 42,\n", - " 44,\n", - " 46,\n", - " 48,\n", - " 50,\n", - " 52,\n", - " 54,\n", - " 56,\n", - " 58,\n", - " 60,\n", - " 62,\n", - " 64,\n", - " 66,\n", - " 68,\n", - " 70,\n", - " 72,\n", - " 74,\n", - " 76,\n", - " 78,\n", - " 80,\n", - " 82,\n", - " 84,\n", - " 86,\n", - " 88,\n", - " 90,\n", - " 92,\n", - " 94,\n", - " 96,\n", - " 98,\n", - " 100,\n", - " 102,\n", - " 104,\n", - " 106,\n", - " 108,\n", - " 110,\n", - " 112,\n", - " 114,\n", - " 116,\n", - " 118,\n", - " 120,\n", - " 122,\n", - " 124,\n", - " 126,\n", - " 128,\n", - " 130,\n", - " 132,\n", - " 134,\n", - " 136,\n", - " 138,\n", - " 140,\n", - " 142,\n", - " 144,\n", - " 146,\n", - " 148,\n", - " 150,\n", - " 152,\n", - " 154,\n", - " 156,\n", - " 158,\n", - " 160,\n", - " 162,\n", - " 164,\n", - " 166,\n", - " 168,\n", - " 170,\n", - " 172,\n", - " 174,\n", - " 176,\n", - " 178,\n", - " 180,\n", - " 182,\n", - " 184,\n", - " 186,\n", - " 188,\n", - " 190,\n", - " 192,\n", - " 194,\n", - " 196,\n", - " 198,\n", - " 200,\n", - " 202,\n", - " 204,\n", - " 206,\n", - " 208,\n", - " 210,\n", - " 212,\n", - " 214,\n", - " 216,\n", - " 218,\n", - " 220,\n", - " 222,\n", - " 224,\n", - " 226,\n", - " 228,\n", - " 230,\n", - " 232,\n", - " 234,\n", - " 236,\n", - " 238,\n", - " 240,\n", - " 242,\n", - " 244,\n", - " 246,\n", - " 248,\n", - " 250,\n", - " 252,\n", - " 254,\n", - " 256,\n", - " 258,\n", - " 260,\n", - " 262,\n", - " 264,\n", - " 266,\n", - " 268,\n", - " 270,\n", - " 272,\n", - " 274,\n", - " 276,\n", - " 278,\n", - " 280,\n", - " 282,\n", - " 284,\n", - " 286,\n", - " 288,\n", - " 290,\n", - " 292,\n", - " 294,\n", - " 296,\n", - " 298,\n", - " 300,\n", - " 302,\n", - " 304,\n", - " 306,\n", - " 308,\n", - " 310,\n", - " 312,\n", - " 314,\n", - " 316,\n", - " 318,\n", - " 320,\n", - " 322,\n", - " 324,\n", - " 326,\n", - " 328,\n", - " 330,\n", - " 332,\n", - " 334,\n", - " 336,\n", - " 338,\n", - " 340,\n", - " 342,\n", - " 344,\n", - " 346,\n", - " 348,\n", - " 350,\n", - " 352,\n", - " 354,\n", - " 356,\n", - " 358,\n", - " 360,\n", - " 362,\n", - " 364,\n", - " 366,\n", - " 368,\n", - " 370,\n", - " 372,\n", - " 374,\n", - " 376,\n", - " 378,\n", - " 380,\n", - " 382,\n", - " 384,\n", - " 386,\n", - " 388,\n", - " 390,\n", - " 392,\n", - " 394,\n", - " 396,\n", - " 398,\n", - " 400,\n", - " 402,\n", - " 404,\n", - " 406,\n", - " 408,\n", - " 410,\n", - " 412,\n", - " 414,\n", - " 416,\n", - " 418,\n", - " 420,\n", - " 422,\n", - " 424,\n", - " 426,\n", - " 428,\n", - " 430,\n", - " 432,\n", - " 434,\n", - " 436,\n", - " 438,\n", - " 440,\n", - " 442,\n", - " 444,\n", - " 446,\n", - " 448,\n", - " 450,\n", - " 452,\n", - " 454,\n", - " 456,\n", - " 458,\n", - " 460,\n", - " 462,\n", - " 464,\n", - " 466,\n", - " 468,\n", - " 470,\n", - " 472,\n", - " 474,\n", - " 476,\n", - " 478,\n", - " 480,\n", - " 482,\n", - " 484,\n", - " 486,\n", - " 488,\n", - " 490,\n", - " 492,\n", - " 494,\n", - " 496,\n", - " 498,\n", - " 500,\n", - " 502,\n", - " 504,\n", - " 506,\n", - " 508,\n", - " 510,\n", - " 512,\n", - " 514,\n", - " 516,\n", - " 518,\n", - " 520,\n", - " 522,\n", - " 524,\n", - " 526,\n", - " 528,\n", - " 530,\n", - " 532,\n", - " 534,\n", - " 536,\n", - " 538,\n", - " 540,\n", - " 542,\n", - " 544,\n", - " 546,\n", - " 548,\n", - " 550,\n", - " 552,\n", - " 554,\n", - " 556,\n", - " 558,\n", - " 560,\n", - " 562,\n", - " 564,\n", - " 566,\n", - " 568,\n", - " 570,\n", - " 572,\n", - " 574,\n", - " 576,\n", - " 578,\n", - " 580,\n", - " 582,\n", - " 584,\n", - " 586,\n", - " 588,\n", - " 590,\n", - " 592,\n", - " 594,\n", - " 596,\n", - " 598,\n", - " 600,\n", - " 602,\n", - " 604,\n", - " 606,\n", - " 608,\n", - " 610,\n", - " 612,\n", - " 614,\n", - " 616,\n", - " 618,\n", - " 620,\n", - " 622,\n", - " 624,\n", - " 626,\n", - " 628,\n", - " 630,\n", - " 632,\n", - " 634,\n", - " 636,\n", - " 638,\n", - " 640,\n", - " 642,\n", - " 644,\n", - " 646,\n", - " 648,\n", - " 650,\n", - " 652,\n", - " 654,\n", - " 656,\n", - " 658,\n", - " 660,\n", - " 662,\n", - " 664,\n", - " 666,\n", - " 668,\n", - " 670,\n", - " 672,\n", - " 674,\n", - " 676,\n", - " 678,\n", - " 680,\n", - " 682,\n", - " 684,\n", - " 686,\n", - " 688,\n", - " 690,\n", - " 692,\n", - " 694,\n", - " 696,\n", - " 698,\n", - " 700,\n", - " 702,\n", - " 704,\n", - " 706,\n", - " 708,\n", - " 710,\n", - " 712,\n", - " 714,\n", - " 716,\n", - " 718,\n", - " 720,\n", - " 722,\n", - " 724,\n", - " 726,\n", - " 728,\n", - " 730,\n", - " 732,\n", - " 734,\n", - " 736,\n", - " 738,\n", - " 740,\n", - " 742,\n", - " 744,\n", - " 746,\n", - " 748,\n", - " 750,\n", - " 752,\n", - " 754,\n", - " 756,\n", - " 758,\n", - " 760,\n", - " 762,\n", - " 764,\n", - " 766,\n", - " 768,\n", - " 770,\n", - " 772,\n", - " 774,\n", - " 776,\n", - " 778,\n", - " 780,\n", - " 782,\n", - " 784,\n", - " 786,\n", - " 788,\n", - " 790,\n", - " 792,\n", - " 794,\n", - " 796,\n", - " 798,\n", - " 800,\n", - " 802,\n", - " 804,\n", - " 806,\n", - " 808,\n", - " 810,\n", - " 812,\n", - " 814,\n", - " 816,\n", - " 818,\n", - " 820,\n", - " 822,\n", - " 824,\n", - " 826,\n", - " 828,\n", - " 830,\n", - " 832,\n", - " 834,\n", - " 836,\n", - " 838,\n", - " 840,\n", - " 842,\n", - " 844,\n", - " 846,\n", - " 848,\n", - " 850,\n", - " 852,\n", - " 854,\n", - " 856,\n", - " 858,\n", - " 860,\n", - " 862,\n", - " 864,\n", - " 866,\n", - " 868,\n", - " 870,\n", - " 872,\n", - " 874,\n", - " 876,\n", - " 878,\n", - " 880,\n", - " 882,\n", - " 884,\n", - " 886,\n", - " 888,\n", - " 890,\n", - " 892,\n", - " 894,\n", - " 896,\n", - " 898,\n", - " 900,\n", - " 902,\n", - " 904,\n", - " 906,\n", - " 908,\n", - " 910,\n", - " 912,\n", - " 914,\n", - " 916,\n", - " 918,\n", - " 920,\n", - " 922,\n", - " 924,\n", - " 926,\n", - " 928,\n", - " 930,\n", - " 932,\n", - " 934,\n", - " 936,\n", - " 938,\n", - " 940,\n", - " 942,\n", - " 944,\n", - " 946,\n", - " 948,\n", - " 950,\n", - " 952,\n", - " 954,\n", - " 956,\n", - " 958,\n", - " 960,\n", - " 962,\n", - " 964,\n", - " 966,\n", - " 968,\n", - " 970,\n", - " 972,\n", - " 974,\n", - " 976,\n", - " 978,\n", - " 980,\n", - " 982,\n", - " 984,\n", - " 986,\n", - " 988,\n", - " 990,\n", - " 992,\n", - " 994,\n", - " 996,\n", - " 998,\n", - " 1000,\n", - " 1002,\n", - " 1004,\n", - " 1006,\n", - " 1008,\n", - " 1010,\n", - " 1012,\n", - " 1014,\n", - " 1016,\n", - " 1018,\n", - " 1020,\n", - " 1022,\n", - " 1024,\n", - " 1026,\n", - " 1028,\n", - " 1030,\n", - " 1032,\n", - " 1034,\n", - " 1036,\n", - " 1038,\n", - " 1040,\n", - " 1042,\n", - " 1044,\n", - " 1046,\n", - " 1048,\n", - " 1050,\n", - " 1052,\n", - " 1054,\n", - " 1056,\n", - " 1058,\n", - " 1060,\n", - " 1062,\n", - " 1064,\n", - " 1066,\n", - " 1068,\n", - " 1070,\n", - " 1072,\n", - " 1074,\n", - " 1076,\n", - " 1078,\n", - " 1080,\n", - " 1082,\n", - " 1084,\n", - " 1086,\n", - " 1088,\n", - " 1090,\n", - " 1092,\n", - " 1094,\n", - " 1096,\n", - " 1098,\n", - " 1100,\n", - " 1102,\n", - " 1104,\n", - " 1106,\n", - " 1108,\n", - " 1110,\n", - " 1112,\n", - " 1114,\n", - " 1116,\n", - " 1118,\n", - " 1120,\n", - " 1122,\n", - " 1124,\n", - " 1126,\n", - " 1128,\n", - " 1130,\n", - " 1132,\n", - " 1134,\n", - " 1136,\n", - " 1138,\n", - " 1140,\n", - " 1142,\n", - " 1144,\n", - " 1146,\n", - " 1148,\n", - " 1150,\n", - " 1152,\n", - " 1154,\n", - " 1156,\n", - " 1158,\n", - " 1160,\n", - " 1162,\n", - " 1164,\n", - " 1166,\n", - " 1168,\n", - " 1170,\n", - " 1172,\n", - " 1174,\n", - " 1176,\n", - " 1178,\n", - " 1180,\n", - " 1182,\n", - " 1184,\n", - " 1186,\n", - " 1188,\n", - " 1190,\n", - " 1192,\n", - " 1194,\n", - " 1196,\n", - " 1198,\n", - " 1200,\n", - " 1202,\n", - " 1204,\n", - " 1206,\n", - " 1208,\n", - " 1210,\n", - " 1212,\n", - " 1214,\n", - " 1216,\n", - " 1218,\n", - " 1220,\n", - " 1222,\n", - " 1224,\n", - " 1226,\n", - " 1228,\n", - " 1230,\n", - " 1232,\n", - " 1234,\n", - " 1236,\n", - " 1238,\n", - " 1240,\n", - " 1242,\n", - " 1244,\n", - " 1246,\n", - " 1248,\n", - " 1250,\n", - " 1252,\n", - " 1254,\n", - " 1256,\n", - " 1258,\n", - " 1260,\n", - " 1262,\n", - " 1264,\n", - " 1266,\n", - " 1268,\n", - " 1270,\n", - " 1272,\n", - " 1274,\n", - " 1276,\n", - " 1278,\n", - " 1280,\n", - " 1282,\n", - " 1284,\n", - " 1286,\n", - " 1288,\n", - " 1290,\n", - " 1292,\n", - " 1294,\n", - " 1296,\n", - " 1298,\n", - " 1300,\n", - " 1302,\n", - " 1304,\n", - " 1306,\n", - " 1308,\n", - " 1310,\n", - " 1312,\n", - " 1314,\n", - " 1316,\n", - " 1318,\n", - " 1320,\n", - " 1322,\n", - " 1324,\n", - " 1326,\n", - " 1328,\n", - " 1330,\n", - " 1332,\n", - " 1334,\n", - " 1336,\n", - " 1338,\n", - " 1340,\n", - " 1342,\n", - " 1344,\n", - " 1346,\n", - " 1348,\n", - " 1350,\n", - " 1352,\n", - " 1354,\n", - " 1356,\n", - " 1358,\n", - " 1360,\n", - " 1362,\n", - " 1364,\n", - " 1366,\n", - " 1368,\n", - " 1370,\n", - " 1372,\n", - " 1374,\n", - " 1376,\n", - " 1378,\n", - " 1380,\n", - " 1382,\n", - " 1384,\n", - " 1386,\n", - " 1388,\n", - " 1390,\n", - " 1392,\n", - " 1394,\n", - " 1396,\n", - " 1398,\n", - " 1400,\n", - " 1402,\n", - " 1404,\n", - " 1406,\n", - " 1408,\n", - " 1410,\n", - " 1412,\n", - " 1414,\n", - " 1416,\n", - " 1418,\n", - " 1420,\n", - " 1422,\n", - " 1424,\n", - " 1426,\n", - " 1428,\n", - " 1430,\n", - " 1432,\n", - " 1434,\n", - " 1436,\n", - " 1438,\n", - " 1440,\n", - " 1442,\n", - " 1444,\n", - " 1446,\n", - " 1448,\n", - " 1450,\n", - " 1452,\n", - " 1454,\n", - " 1456,\n", - " 1458,\n", - " 1460,\n", - " 1462,\n", - " 1464,\n", - " 1466,\n", - " 1468,\n", - " 1470,\n", - " 1472,\n", - " 1474,\n", - " 1476,\n", - " 1478,\n", - " 1480,\n", - " 1482,\n", - " 1484,\n", - " 1486,\n", - " 1488,\n", - " 1490,\n", - " 1492,\n", - " 1494,\n", - " 1496,\n", - " 1498,\n", - " 1500,\n", - " 1502,\n", - " 1504,\n", - " 1506,\n", - " 1508,\n", - " 1510,\n", - " 1512,\n", - " 1514,\n", - " 1516,\n", - " 1518,\n", - " 1520,\n", - " 1522,\n", - " 1524,\n", - " 1526,\n", - " 1528,\n", - " 1530,\n", - " 1532,\n", - " 1534,\n", - " 1536,\n", - " 1538,\n", - " 1540,\n", - " 1542,\n", - " 1544,\n", - " 1546,\n", - " 1548,\n", - " 1550,\n", - " 1552,\n", - " 1554,\n", - " 1556,\n", - " 1558,\n", - " 1560,\n", - " 1562,\n", - " 1564,\n", - " 1566,\n", - " 1568,\n", - " 1570,\n", - " 1572,\n", - " 1574,\n", - " 1576,\n", - " 1578,\n", - " 1580,\n", - " 1582,\n", - " 1584,\n", - " 1586,\n", - " 1588,\n", - " 1590,\n", - " 1592,\n", - " 1594,\n", - " 1596,\n", - " 1598,\n", - " 1600,\n", - " 1602,\n", - " 1604,\n", - " 1606,\n", - " 1608,\n", - " 1610,\n", - " 1612,\n", - " 1614,\n", - " 1616,\n", - " 1618,\n", - " 1620,\n", - " 1622,\n", - " 1624,\n", - " 1626,\n", - " 1628,\n", - " 1630,\n", - " 1632,\n", - " 1634,\n", - " 1636,\n", - " 1638,\n", - " 1640,\n", - " 1642,\n", - " 1644,\n", - " 1646,\n", - " 1648,\n", - " 1650,\n", - " 1652,\n", - " 1654,\n", - " 1656,\n", - " 1658,\n", - " 1660,\n", - " 1662,\n", - " 1664,\n", - " 1666,\n", - " 1668,\n", - " 1670,\n", - " 1672,\n", - " 1674,\n", - " 1676,\n", - " 1678,\n", - " 1680,\n", - " 1682,\n", - " 1684,\n", - " 1686,\n", - " 1688,\n", - " 1690,\n", - " 1692,\n", - " 1694,\n", - " 1696,\n", - " 1698,\n", - " 1700,\n", - " 1702,\n", - " 1704,\n", - " 1706,\n", - " 1708,\n", - " 1710,\n", - " 1712,\n", - " 1714,\n", - " 1716,\n", - " 1718,\n", - " 1720,\n", - " 1722,\n", - " 1724,\n", - " 1726,\n", - " 1728,\n", - " 1730,\n", - " 1732,\n", - " 1734,\n", - " 1736,\n", - " 1738,\n", - " 1740,\n", - " 1742,\n", - " 1744,\n", - " 1746,\n", - " 1748,\n", - " 1750,\n", - " 1752,\n", - " 1754,\n", - " 1756,\n", - " 1758,\n", - " 1760,\n", - " 1762,\n", - " 1764,\n", - " 1766,\n", - " 1768,\n", - " 1770,\n", - " 1772,\n", - " 1774,\n", - " 1776,\n", - " 1778,\n", - " 1780,\n", - " 1782,\n", - " 1784,\n", - " 1786,\n", - " 1788,\n", - " 1790,\n", - " 1792,\n", - " 1794,\n", - " 1796,\n", - " 1798,\n", - " 1800,\n", - " 1802,\n", - " 1804,\n", - " 1806,\n", - " 1808,\n", - " 1810,\n", - " 1812,\n", - " 1814,\n", - " 1816,\n", - " 1818,\n", - " 1820,\n", - " 1822,\n", - " 1824,\n", - " 1826,\n", - " 1828,\n", - " 1830,\n", - " 1832,\n", - " 1834,\n", - " 1836,\n", - " 1838,\n", - " 1840,\n", - " 1842,\n", - " 1844,\n", - " 1846,\n", - " 1848,\n", - " 1850,\n", - " 1852,\n", - " 1854,\n", - " 1856,\n", - " 1858,\n", - " 1860,\n", - " 1862,\n", - " 1864,\n", - " 1866,\n", - " 1868,\n", - " 1870,\n", - " 1872,\n", - " 1874,\n", - " 1876,\n", - " 1878,\n", - " 1880,\n", - " 1882,\n", - " 1884,\n", - " 1886,\n", - " 1888,\n", - " 1890,\n", - " 1892,\n", - " 1894,\n", - " 1896,\n", - " 1898,\n", - " 1900,\n", - " 1902,\n", - " 1904,\n", - " 1906,\n", - " 1908,\n", - " 1910,\n", - " 1912,\n", - " 1914,\n", - " 1916,\n", - " 1918,\n", - " 1920,\n", - " 1922,\n", - " 1924,\n", - " 1926,\n", - " 1928,\n", - " 1930,\n", - " 1932,\n", - " 1934,\n", - " 1936,\n", - " 1938,\n", - " 1940,\n", - " 1942,\n", - " 1944,\n", - " 1946,\n", - " 1948,\n", - " 1950,\n", - " 1952,\n", - " 1954,\n", - " 1956,\n", - " 1958,\n", - " 1960,\n", - " 1962,\n", - " 1964,\n", - " 1966,\n", - " 1968,\n", - " 1970,\n", - " 1972,\n", - " 1974,\n", - " 1976,\n", - " 1978,\n", - " 1980,\n", - " 1982,\n", - " 1984,\n", - " 1986,\n", - " 1988,\n", - " 1990,\n", - " 1992,\n", - " 1994,\n", - " 1996,\n", - " 1998]" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" + "execution_count": null, + "metadata": { + "pycharm": { + "is_executing": true } - ], + }, + "outputs": [], "source": [ "import timeit\n", "[2 * n for n in range(1000)]" @@ -1378,7 +311,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "pycharm": { + "is_executing": true + } + }, "outputs": [], "source": [] } @@ -1399,9 +336,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 2 -} +} \ No newline at end of file From 18f7cdd980e16e47a6e2e2b776c2d3e07ccb3da3 Mon Sep 17 00:00:00 2001 From: Young Jin J Date: Fri, 5 Jun 2020 18:24:29 -0400 Subject: [PATCH 2/8] Stack, Deque, Queue implement --- .../Array Pair Sum - SOLUTION.ipynb | 22 ++- .../Find the Missing Element - SOLUTION.ipynb | 41 +++--- .../Largest Continuous Sum - SOLUTION.ipynb | 4 +- .../Sentence Reversal - SOLUTION.ipynb | 117 ++++++++++++--- .../String Compression -SOLUTION.ipynb | 4 +- ...ique Characters in String - SOLUTION.ipynb | 12 +- .../Find the Missing Element .ipynb | 60 ++++---- .../Largest Continuous Sum .ipynb | 19 ++- .../Sentence Reversal.ipynb | 33 +++-- .../String Compression .ipynb | 47 ++++-- .../Unique Characters in String.ipynb | 43 ++++-- .../Deques Lecture.ipynb | 134 ++++++++++++++++++ .../Deques Overview.ipynb | 16 +-- .../Implementation of Queue.ipynb | 24 ++-- .../Implementation of Stack.ipynb | 54 +++---- Stacks, Queues and Deques/Queue Lecture.ipynb | 128 +++++++++++++++++ .../Queues Overview.ipynb | 20 ++- Stacks, Queues and Deques/Stack Lecture.ipynb | 105 ++++++++++++++ .../Implement a Deque .ipynb | 125 +++++++++++++++- .../Implement a Queue.ipynb | 84 ++++++++++- .../Implement a Stack .ipynb | 57 +++++++- 21 files changed, 920 insertions(+), 229 deletions(-) create mode 100644 Stacks, Queues and Deques/Deques Lecture.ipynb create mode 100644 Stacks, Queues and Deques/Queue Lecture.ipynb create mode 100644 Stacks, Queues and Deques/Stack Lecture.ipynb diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Array Pair Sum - SOLUTION.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Array Pair Sum - SOLUTION.ipynb index 2419f818..171c0b07 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Array Pair Sum - SOLUTION.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Array Pair Sum - SOLUTION.ipynb @@ -69,9 +69,7 @@ { "cell_type": "code", "execution_count": 14, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -98,9 +96,7 @@ { "cell_type": "code", "execution_count": 16, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -140,23 +136,23 @@ ], "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.10" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 -} + "nbformat_minor": 1 +} \ No newline at end of file diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Find the Missing Element - SOLUTION.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Find the Missing Element - SOLUTION.ipynb index 0b216746..0b21a0f7 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Find the Missing Element - SOLUTION.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Find the Missing Element - SOLUTION.ipynb @@ -44,6 +44,8 @@ " arr2.sort()\n", " \n", " # Compare elements in the sorted arrays\n", + " # zip takes corresponding index elements from two arrays and pair them in tuple\n", + " # zip([1,2,3,4], [4,5,6,7,8]) = [(1, 4), (2, 5), (3, 6), (4, 7)]\n", " for num1, num2 in zip(arr1,arr2):\n", " if num1!= num2:\n", " return num1\n", @@ -55,9 +57,7 @@ { "cell_type": "code", "execution_count": 46, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -86,9 +86,7 @@ { "cell_type": "code", "execution_count": 44, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "import collections\n", @@ -96,9 +94,11 @@ "def finder2(arr1, arr2): \n", " \n", " # Using default dict to avoid key errors\n", + " # collections.defaultdict(int) when there is no key, it simply add element to the key\n", + " # no error thrown\n", " d=collections.defaultdict(int) \n", " \n", - " # Add a count for every instance in Array 1\n", + " # Add a count for every instance in Array 2\n", " for num in arr2:\n", " d[num]+=1 \n", " \n", @@ -114,9 +114,7 @@ { "cell_type": "code", "execution_count": 47, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -153,6 +151,7 @@ }, "outputs": [], "source": [ + "# Clever trick version with linear time\n", "def finder3(arr1, arr2): \n", " result=0 \n", " \n", @@ -167,9 +166,7 @@ { "cell_type": "code", "execution_count": 39, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -209,9 +206,7 @@ { "cell_type": "code", "execution_count": 43, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -250,23 +245,23 @@ ], "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.10" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 -} + "nbformat_minor": 1 +} \ No newline at end of file diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Largest Continuous Sum - SOLUTION.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Largest Continuous Sum - SOLUTION.ipynb index f5fd1e30..c9d3560c 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Largest Continuous Sum - SOLUTION.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Largest Continuous Sum - SOLUTION.ipynb @@ -34,12 +34,14 @@ " max_sum=current_sum=arr[0] \n", " \n", " # For every element in array\n", + " # Not including the first one\n", " for num in arr[1:]: \n", " \n", " # Set current sum as the higher of the two\n", " current_sum=max(current_sum+num, num)\n", " \n", " # Set max as the higher between the currentSum and the current max\n", + " # max_sum is the largest currentSum ever happened\n", " max_sum=max(current_sum, max_sum) \n", " \n", " return max_sum " @@ -137,4 +139,4 @@ }, "nbformat": 4, "nbformat_minor": 1 -} +} \ No newline at end of file diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Sentence Reversal - SOLUTION.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Sentence Reversal - SOLUTION.ipynb index 0e6577a7..ca139c61 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Sentence Reversal - SOLUTION.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Sentence Reversal - SOLUTION.ipynb @@ -97,7 +97,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 24, "metadata": { "collapsed": true }, @@ -132,23 +132,46 @@ " words.append(s[word_start:i])\n", " # Add to index\n", " i += 1\n", - " \n", + " print(words)\n", " # Join the reversed words\n", " return \" \".join(reversed(words))" ] }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 15, "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "10\n", + "11\n", + "12\n", + "13\n", + "18\n", + "19\n", + "20\n", + "22\n", + "23\n", + "24\n", + "26\n", + "27\n", + "28\n", + "['Hello', 'John', 'how', 'are', 'you']\n" + ] + }, { "data": { - "text/plain": [ - "'you are how John Hello'" - ] + "text/plain": "'you are how John Hello'" }, - "execution_count": 19, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -159,16 +182,32 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 25, "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "11\n", + "12\n", + "13\n", + "14\n", + "15\n", + "16\n", + "['space', 'before']\n" + ] + }, { "data": { - "text/plain": [ - "'before space'" - ] + "text/plain": "'before space'" }, - "execution_count": 20, + "execution_count": 25, "metadata": {}, "output_type": "execute_result" } @@ -193,13 +232,57 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "11\n", + "12\n", + "13\n", + "14\n", + "15\n", + "16\n", + "['space', 'before']\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "7\n", + "8\n", + "9\n", + "10\n", + "11\n", + "['space', 'after']\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "10\n", + "11\n", + "12\n", + "13\n", + "18\n", + "19\n", + "20\n", + "22\n", + "23\n", + "24\n", + "26\n", + "27\n", + "28\n", + "['Hello', 'John', 'how', 'are', 'you']\n", + "1\n", + "['1']\n", "ALL TEST CASES PASSED\n" ] } @@ -218,11 +301,11 @@ " assert_equal(sol('space after '),'after space')\n", " assert_equal(sol(' Hello John how are you '),'you are how John Hello')\n", " assert_equal(sol('1'),'1')\n", - " print \"ALL TEST CASES PASSED\"\n", + " print (\"ALL TEST CASES PASSED\")\n", " \n", "# Run and test\n", "t = ReversalTest()\n", - "t.test(rev_word)" + "t.test(rev_word3)" ] }, { @@ -249,9 +332,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 1 -} +} \ No newline at end of file diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/String Compression -SOLUTION.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/String Compression -SOLUTION.ipynb index 68411f20..db0938f2 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/String Compression -SOLUTION.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/String Compression -SOLUTION.ipynb @@ -154,9 +154,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 1 -} +} \ No newline at end of file diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Unique Characters in String - SOLUTION.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Unique Characters in String - SOLUTION.ipynb index 4b18c4c4..77a5afaa 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Unique Characters in String - SOLUTION.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Unique Characters in String - SOLUTION.ipynb @@ -7,7 +7,7 @@ "# Unique Characters in String\n", "\n", "## Problem\n", - "Given a string,determine if it is compreised of all unique characters. For example, the string 'abcde' has all unique characters and should return True. The string 'aabcde' contains duplicate characters and should return false." + "Given a string,determine if it is comprised of all unique characters. For example, the string 'abcde' has all unique characters and should return True. The string 'aabcde' contains duplicate characters and should return false." ] }, { @@ -32,7 +32,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "collapsed": true }, @@ -59,7 +59,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -83,11 +83,11 @@ " assert_equal(sol(''), True)\n", " assert_equal(sol('goo'), False)\n", " assert_equal(sol('abcdefg'), True)\n", - " print 'ALL TEST CASES PASSED'\n", + " print ('ALL TEST CASES PASSED')\n", " \n", "# Run Tests\n", "t = TestUnique()\n", - "t.test(uni_char)" + "t.test(uni_char2)" ] }, { @@ -119,4 +119,4 @@ }, "nbformat": 4, "nbformat_minor": 1 -} +} \ No newline at end of file diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Find the Missing Element .ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Find the Missing Element .ipynb index fe1e12d7..4c4507ab 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Find the Missing Element .ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Find the Missing Element .ipynb @@ -27,31 +27,31 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def finder(arr1,arr2):\n", - " \n", - " pass" + " arr1 = sorted(arr1)\n", + " arr2 = sorted(arr2)\n", + "\n", + " for idx, a in enumerate(arr1):\n", + " if a - arr2[idx] != 0:\n", + " return a" ] }, { "cell_type": "code", - "execution_count": 46, - "metadata": { - "collapsed": false - }, + "execution_count": 5, + "metadata": {}, "outputs": [ { "data": { - "text/plain": [ - "5" - ] + "text/plain": "5" }, - "execution_count": 46, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -64,18 +64,14 @@ }, { "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": false - }, + "execution_count": 6, + "metadata": {}, "outputs": [ { "data": { - "text/plain": [ - "5" - ] + "text/plain": "5" }, - "execution_count": 47, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -89,9 +85,7 @@ }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ "_____" ] @@ -105,10 +99,8 @@ }, { "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": false - }, + "execution_count": 8, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -130,7 +122,7 @@ " assert_equal(sol([5,5,7,7],[5,7,7]),5)\n", " assert_equal(sol([1,2,3,4,5,6,7],[3,7,2,1,4,6]),5)\n", " assert_equal(sol([9,8,7,6,5,4,3,2,1],[9,8,7,5,4,3,2,1]),6)\n", - " print 'ALL TEST CASES PASSED'\n", + " print ('ALL TEST CASES PASSED')\n", "\n", "# Run test\n", "t = TestFinder()\n", @@ -147,23 +139,23 @@ ], "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.10" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 -} + "nbformat_minor": 1 +} \ No newline at end of file diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Largest Continuous Sum .ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Largest Continuous Sum .ipynb index a4220bb0..afddd6b8 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Largest Continuous Sum .ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Largest Continuous Sum .ipynb @@ -22,8 +22,17 @@ }, "outputs": [], "source": [ - "def large_cont_sum(arr): \n", - " pass" + "def large_cont_sum(arr):\n", + " if len(arr) == 0:\n", + " return 0\n", + "\n", + " maxSum = currentSum = arr[0]\n", + "\n", + " for num in arr[:1]:\n", + " # If the num is > currentSum+num, then start new currentSum from num\n", + " currentSum = max(currentSum+num, num)\n", + " maxSum = max(currentSum, maxSum)\n", + " return maxSum" ] }, { @@ -103,9 +112,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "name": "python3", "language": "python", - "name": "python2" + "display_name": "Python 3" }, "language_info": { "codemirror_mode": { @@ -122,4 +131,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Sentence Reversal.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Sentence Reversal.ipynb index e6cf296a..05bfe23b 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Sentence Reversal.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Sentence Reversal.ipynb @@ -33,30 +33,31 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 77, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def rev_word(s):\n", - " pass" + " s = s.strip().split()\n", + " ans = ' '\n", + "\n", + " return ans.join(reversed(s))\n" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 74, "metadata": { "collapsed": false }, "outputs": [ { "data": { - "text/plain": [ - "'go? to ready you are John, Hi'" - ] + "text/plain": "'go? to ready you are John, Hi'" }, - "execution_count": 4, + "execution_count": 74, "metadata": {}, "output_type": "execute_result" } @@ -67,18 +68,16 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 75, "metadata": { "collapsed": false }, "outputs": [ { "data": { - "text/plain": [ - "'before space'" - ] + "text/plain": "'before space'" }, - "execution_count": 20, + "execution_count": 75, "metadata": {}, "output_type": "execute_result" } @@ -103,7 +102,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 78, "metadata": { "collapsed": false }, @@ -130,7 +129,7 @@ " assert_equal(sol('space after '),'after space')\n", " assert_equal(sol(' Hello John how are you '),'you are how John Hello')\n", " assert_equal(sol('1'),'1')\n", - " print \"ALL TEST CASES PASSED\"\n", + " print (\"ALL TEST CASES PASSED\")\n", " \n", "# Run and test\n", "t = ReversalTest()\n", @@ -147,9 +146,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "name": "python3", "language": "python", - "name": "python2" + "display_name": "Python 3" }, "language_info": { "codemirror_mode": { @@ -166,4 +165,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/String Compression .ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/String Compression .ipynb index e8c7d5f4..cecea3f7 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/String Compression .ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/String Compression .ipynb @@ -23,36 +23,57 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 96, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def compress(s):\n", - " pass" + " if s == '' or s is None or len(s) <= 0:\n", + " return ''\n", + "\n", + " prevlet = s[0]\n", + " count = 0\n", + " ans = list()\n", + " for let in s:\n", + " if let != prevlet:\n", + " ans.append(prevlet)\n", + " ans.append(count)\n", + " prevlet = let\n", + " count = 1\n", + " elif let == prevlet:\n", + " count += 1\n", + " ans.append(prevlet)\n", + " ans.append(count)\n", + " return ''.join(map(str,ans))\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" ] }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 99, "metadata": { "collapsed": false }, "outputs": [ { "data": { - "text/plain": [ - "'A5B4C4'" - ] + "text/plain": "'A2a2'" }, - "execution_count": 66, + "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "compress('AAAAABBBBCCCC')" + "compress('AAaa')" ] }, { @@ -64,7 +85,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 98, "metadata": { "collapsed": false }, @@ -89,7 +110,7 @@ " assert_equal(sol(''), '')\n", " assert_equal(sol('AABBCC'), 'A2B2C2')\n", " assert_equal(sol('AAABCCDDDDD'), 'A3B1C2D5')\n", - " print 'ALL TEST CASES PASSED'\n", + " print ('ALL TEST CASES PASSED')\n", "\n", "# Run Tests\n", "t = TestCompress()\n", @@ -106,9 +127,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "name": "python3", "language": "python", - "name": "python2" + "display_name": "Python 3" }, "language_info": { "codemirror_mode": { @@ -125,4 +146,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Unique Characters in String.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Unique Characters in String.ipynb index 9659d2bf..00466378 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Unique Characters in String.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Unique Characters in String.ipynb @@ -7,7 +7,7 @@ "# Unique Characters in String\n", "\n", "## Problem\n", - "Given a string,determine if it is compreised of all unique characters. For example, the string 'abcde' has all unique characters and should return True. The string 'aabcde' contains duplicate characters and should return false." + "Given a string,determine if it is comprised of all unique characters. For example, the string 'abcde' has all unique characters and should return True. The string 'aabcde' contains duplicate characters and should return false." ] }, { @@ -20,14 +20,16 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def uni_char(s):\n", - " pass" + " if len(set(s)) != len(s):\n", + " return False\n", + " return True" ] }, { @@ -39,7 +41,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 12, "metadata": { "collapsed": false }, @@ -65,7 +67,7 @@ " assert_equal(sol(''), True)\n", " assert_equal(sol('goo'), False)\n", " assert_equal(sol('abcdefg'), True)\n", - " print 'ALL TEST CASES PASSED'\n", + " print ('ALL TEST CASES PASSED')\n", " \n", "# Run Tests\n", "t = TestUnique()\n", @@ -78,13 +80,38 @@ "source": [ "## Good Job!" ] + }, + { + "cell_type": "code", + "execution_count": 11, + "outputs": [ + { + "data": { + "text/plain": "False" + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = 'aabcde'\n", + "uni_char(s)\n", + "\n" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "name": "python3", "language": "python", - "name": "python2" + "display_name": "Python 3" }, "language_info": { "codemirror_mode": { @@ -101,4 +128,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Stacks, Queues and Deques/Deques Lecture.ipynb b/Stacks, Queues and Deques/Deques Lecture.ipynb new file mode 100644 index 00000000..e7f7f38b --- /dev/null +++ b/Stacks, Queues and Deques/Deques Lecture.ipynb @@ -0,0 +1,134 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true, + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "# Deque Implementation" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "outputs": [], + "source": [ + "class Deque(object):\n", + " def __init__(self):\n", + " self.items = []\n", + "\n", + " def isEmpty(self):\n", + " return self.items == []\n", + "\n", + " def addFront(self, item):\n", + " self.items.append(item)\n", + "\n", + " def addRear(self, item):\n", + " self.items.insert(0, item)\n", + "\n", + " def removeFront(self):\n", + " return self.items.pop()\n", + "\n", + " def removeRear(self):\n", + " return self.items.pop(0)\n", + "\n", + " def size(self):\n", + " return len(self.items)" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 24, + "outputs": [ + { + "data": { + "text/plain": "2" + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d = Deque()\n", + "d.addFront('hello')\n", + "d.addRear('World')\n", + "d.size()\n", + "\n" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 25, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello World\n" + ] + } + ], + "source": [ + "print(d.removeFront() + ' ' + d.removeRear())" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "\n" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file diff --git a/Stacks, Queues and Deques/Deques Overview.ipynb b/Stacks, Queues and Deques/Deques Overview.ipynb index 413f2635..9468ec59 100644 --- a/Stacks, Queues and Deques/Deques Overview.ipynb +++ b/Stacks, Queues and Deques/Deques Overview.ipynb @@ -16,9 +16,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -47,23 +45,23 @@ ], "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.10" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Stacks, Queues and Deques/Implementation of Queue.ipynb b/Stacks, Queues and Deques/Implementation of Queue.ipynb index 23c0e784..3acc5f0a 100644 --- a/Stacks, Queues and Deques/Implementation of Queue.ipynb +++ b/Stacks, Queues and Deques/Implementation of Queue.ipynb @@ -75,9 +75,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -97,9 +95,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -130,9 +126,7 @@ { "cell_type": "code", "execution_count": 6, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -159,23 +153,23 @@ ], "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.10" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Stacks, Queues and Deques/Implementation of Stack.ipynb b/Stacks, Queues and Deques/Implementation of Stack.ipynb index fc3983dd..5bbab8ac 100644 --- a/Stacks, Queues and Deques/Implementation of Stack.ipynb +++ b/Stacks, Queues and Deques/Implementation of Stack.ipynb @@ -32,9 +32,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class Stack:\n", @@ -80,9 +78,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -121,9 +117,7 @@ { "cell_type": "code", "execution_count": 7, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -154,9 +148,7 @@ { "cell_type": "code", "execution_count": 9, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -176,9 +168,7 @@ { "cell_type": "code", "execution_count": 10, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -198,9 +188,7 @@ { "cell_type": "code", "execution_count": 11, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -220,9 +208,7 @@ { "cell_type": "code", "execution_count": 12, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -239,9 +225,7 @@ { "cell_type": "code", "execution_count": 13, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -261,9 +245,7 @@ { "cell_type": "code", "execution_count": 14, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -283,9 +265,7 @@ { "cell_type": "code", "execution_count": 15, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -312,23 +292,23 @@ ], "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.10" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 -} + "nbformat_minor": 1 +} \ No newline at end of file diff --git a/Stacks, Queues and Deques/Queue Lecture.ipynb b/Stacks, Queues and Deques/Queue Lecture.ipynb new file mode 100644 index 00000000..6ce838f9 --- /dev/null +++ b/Stacks, Queues and Deques/Queue Lecture.ipynb @@ -0,0 +1,128 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true, + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "# Implementation of Queue" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "outputs": [], + "source": [ + "class Queue(object):\n", + " def __init__(self):\n", + " self.items = []\n", + "\n", + " def isEmpty(self):\n", + " return self.items == []\n", + "\n", + " def enqueue(self, item):\n", + " self.items.insert(0, item)\n", + "\n", + " def dequeue(self):\n", + " return self.items.pop()\n", + "\n", + " def size(self):\n", + " return len(self.items)" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 15, + "outputs": [ + { + "data": { + "text/plain": "True" + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q = Queue()\n", + "q.size()\n", + "q.isEmpty()\n" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 17, + "outputs": [], + "source": [ + "q.enqueue(2)\n", + "q.enqueue(3)" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 18, + "outputs": [ + { + "data": { + "text/plain": "2" + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q.dequeue()\n" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file diff --git a/Stacks, Queues and Deques/Queues Overview.ipynb b/Stacks, Queues and Deques/Queues Overview.ipynb index be7a713a..f090a045 100644 --- a/Stacks, Queues and Deques/Queues Overview.ipynb +++ b/Stacks, Queues and Deques/Queues Overview.ipynb @@ -26,9 +26,7 @@ { "cell_type": "code", "execution_count": 6, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -60,9 +58,7 @@ { "cell_type": "code", "execution_count": 7, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -93,23 +89,23 @@ ], "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.10" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Stacks, Queues and Deques/Stack Lecture.ipynb b/Stacks, Queues and Deques/Stack Lecture.ipynb new file mode 100644 index 00000000..63068c7b --- /dev/null +++ b/Stacks, Queues and Deques/Stack Lecture.ipynb @@ -0,0 +1,105 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true, + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "# Implementation of a Stack" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "outputs": [], + "source": [ + "class Stack(object):\n", + " def __init__(self):\n", + " self.items = []\n", + " def isEmpty(self):\n", + " return self.items == []\n", + " def push(self, item):\n", + " return self.items.append(item)\n", + " def pop(self):\n", + " return self.items.pop()\n", + " def peek(self):\n", + " return self.items[len(self.items)-1]\n", + " def size(self):\n", + " return len(self.items)\n", + "\n" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 14, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + }, + { + "data": { + "text/plain": "1" + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = Stack()\n", + "print (s.isEmpty())\n", + "\n", + "s.push(1)\n", + "s.push('two')\n", + "s.peek()\n", + "s.push(True)\n", + "s.size()\n", + "s.pop()\n", + "s.pop()\n", + "s.size()\n", + "s.peek()" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file diff --git a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Deque .ipynb b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Deque .ipynb index d5c255f7..2c3b975a 100644 --- a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Deque .ipynb +++ b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Deque .ipynb @@ -16,22 +16,137 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Deque(object):\n", - " pass" + " def __init__(self):\n", + " self.items = []\n", + "\n", + " def isEmpty(self):\n", + " return self.items == []\n", + "\n", + " def addFront(self, item):\n", + " self.items.insert(0, item)\n", + " def addRear(self, item):\n", + " self.items.append(item)\n", + "\n", + " def removeFront(self):\n", + " return self.items.pop(0)\n", + " def removeRear(self):\n", + " return self.items.pop()\n", + " def size(self):\n", + " return len(self.items)" ] + }, + { + "cell_type": "code", + "execution_count": 8, + "outputs": [ + { + "data": { + "text/plain": "True" + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d = Deque()\n", + "d.isEmpty()" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 9, + "outputs": [], + "source": [ + "d.addFront(1)\n", + "d.addRear('two')" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 10, + "outputs": [ + { + "data": { + "text/plain": "2" + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d.size()" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 11, + "outputs": [ + { + "data": { + "text/plain": "'1two'" + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'' + str(d.removeFront()) + str(d.removeRear())" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "\n" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "name": "python3", "language": "python", - "name": "python2" + "display_name": "Python 3" }, "language_info": { "codemirror_mode": { @@ -48,4 +163,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Queue.ipynb b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Queue.ipynb index a5ac0019..0a87f6b3 100644 --- a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Queue.ipynb +++ b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Queue.ipynb @@ -16,22 +16,96 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Queue(object):\n", - " pass" + " def __init__(self):\n", + " self.items = []\n", + "\n", + " def isEmpty(self):\n", + " return self.items == []\n", + " def enqueue(self, item):\n", + " self.items.insert(0, item)\n", + " def dequeue(self):\n", + " return self.items.pop()\n", + " def size(self):\n", + " return len(self.items)" ] + }, + { + "cell_type": "code", + "execution_count": 19, + "outputs": [ + { + "data": { + "text/plain": "True" + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q = Queue()\n", + "q.isEmpty()\n" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 20, + "outputs": [ + { + "data": { + "text/plain": "2" + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q.enqueue(1)\n", + "q.enqueue('two')\n", + "q.isEmpty()\n", + "q.size()" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "\n" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "name": "python3", "language": "python", - "name": "python2" + "display_name": "Python 3" }, "language_info": { "codemirror_mode": { @@ -48,4 +122,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Stack .ipynb b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Stack .ipynb index 6dea3952..05639983 100644 --- a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Stack .ipynb +++ b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Stack .ipynb @@ -19,25 +19,68 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Stack(object):\n", - " \n", - " \n", " # Fill out the Stack Methods here\n", - " pass" + " def __init__(self):\n", + " self.items = []\n", + "\n", + " def isEmpty(self):\n", + " return self.items == []\n", + "\n", + " def push(self, item):\n", + " self.items.append(item)\n", + "\n", + " def pop(self):\n", + " return self.items.pop()\n", + "\n", + " def peek(self):\n", + " return self.items[len(self.items) - 1]\n", + "\n", + " def size(self):\n", + " return len(self.items)\n" ] + }, + { + "cell_type": "code", + "execution_count": 11, + "outputs": [ + { + "data": { + "text/plain": "1" + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = Stack()\n", + "s.push(1)\n", + "s.push('two')\n", + "s.size()\n", + "s.pop()\n", + "s.size()\n", + "\n" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "name": "python3", "language": "python", - "name": "python2" + "display_name": "Python 3" }, "language_info": { "codemirror_mode": { @@ -54,4 +97,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file From 30146ccee41298eb2fa229d67d1f00fa26f385c2 Mon Sep 17 00:00:00 2001 From: Young Jin J Date: Sat, 6 Jun 2020 13:31:35 -0400 Subject: [PATCH 3/8] Linked Lists done --- ...ked List Nth to Last Node - SOLUTION.ipynb | 36 ++++---- .../Implement a Doubly Linked List.ipynb | 6 +- .../Implement a Singly Linked List.ipynb | 5 +- .../Linked List Nth to Last Node .ipynb | 44 ++++----- .../Linked List Reversal .ipynb | 89 ++++++++---------- .../Singly Linked List Cycle Check.ipynb | 31 ++++--- .../Balanced Parentheses Check .ipynb | 91 +++++++++++++------ ...Implement a Queue -Using Two Stacks .ipynb | 37 ++++---- 8 files changed, 183 insertions(+), 156 deletions(-) diff --git a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems - SOLUTIONS/Linked List Nth to Last Node - SOLUTION.ipynb b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems - SOLUTIONS/Linked List Nth to Last Node - SOLUTION.ipynb index 457e6e48..edde9610 100644 --- a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems - SOLUTIONS/Linked List Nth to Last Node - SOLUTION.ipynb +++ b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems - SOLUTIONS/Linked List Nth to Last Node - SOLUTION.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 14, "metadata": { "collapsed": true }, @@ -27,7 +27,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 15, "metadata": { "collapsed": false }, @@ -50,20 +50,18 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "execute_result", "data": { - "text/plain": [ - "4" - ] + "text/plain": "4" }, - "execution_count": 7, "metadata": {}, - "output_type": "execute_result" + "execution_count": 16 } ], "source": [ @@ -92,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 17, "metadata": { "collapsed": true }, @@ -104,7 +102,7 @@ " right_pointer = head\n", "\n", " # Set right pointer at n nodes away from head\n", - " for i in xrange(n-1):\n", + " for i in range(n-1):\n", " \n", " # Check for edge case of not having enough nodes!\n", " if not right_pointer.nextnode:\n", @@ -131,17 +129,15 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { - "name": "stdout", "output_type": "stream", - "text": [ - "ALL TEST CASES PASSED\n" - ] + "name": "stdout", + "text": "ALL TEST CASES PASSED\n" } ], "source": [ @@ -171,7 +167,7 @@ " def test(self,sol):\n", " \n", " assert_equal(sol(2,a),d)\n", - " print 'ALL TEST CASES PASSED'\n", + " print ('ALL TEST CASES PASSED')\n", " \n", "# Run tests\n", "t = TestNLast()\n", @@ -188,9 +184,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3.8.2 64-bit", "language": "python", - "name": "python2" + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" }, "language_info": { "codemirror_mode": { @@ -202,9 +198,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "3.8.2-final" } }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Implement a Doubly Linked List.ipynb b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Implement a Doubly Linked List.ipynb index 047d0421..77fff562 100644 --- a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Implement a Doubly Linked List.ipynb +++ b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Implement a Doubly Linked List.ipynb @@ -18,7 +18,9 @@ "outputs": [], "source": [ "class Node(object):\n", - " pass" + " self.value = value\n", + " self.nextnode = None\n", + " self.prevnode = None" ] }, { @@ -64,4 +66,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Implement a Singly Linked List.ipynb b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Implement a Singly Linked List.ipynb index 92b84693..797813c7 100644 --- a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Implement a Singly Linked List.ipynb +++ b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Implement a Singly Linked List.ipynb @@ -18,6 +18,9 @@ "outputs": [], "source": [ "class Node(object):\n", + " def __init__(self, value):\n", + " self.value = value\n", + " self.nextnode = None\n", " pass" ] }, @@ -65,4 +68,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Linked List Nth to Last Node .ipynb b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Linked List Nth to Last Node .ipynb index de16594d..2ea18201 100644 --- a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Linked List Nth to Last Node .ipynb +++ b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Linked List Nth to Last Node .ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 52, "metadata": { "collapsed": true }, @@ -34,7 +34,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 53, "metadata": { "collapsed": false }, @@ -57,20 +57,18 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "execute_result", "data": { - "text/plain": [ - "4" - ] + "text/plain": "4" }, - "execution_count": 7, "metadata": {}, - "output_type": "execute_result" + "execution_count": 54 } ], "source": [ @@ -87,15 +85,21 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 55, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def nth_to_last_node(n, head):\n", - "\n", - " pass" + " mk1 = head\n", + " mk2 = head\n", + " for i in range(n):\n", + " mk2 = mk2.nextnode\n", + " while mk2:\n", + " mk2 = mk2.nextnode\n", + " mk1 = mk1.nextnode\n", + " return mk1" ] }, { @@ -107,17 +111,15 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 56, "metadata": { "collapsed": false }, "outputs": [ { - "name": "stdout", "output_type": "stream", - "text": [ - "ALL TEST CASES PASSED\n" - ] + "name": "stdout", + "text": "ALL TEST CASES PASSED\n" } ], "source": [ @@ -147,7 +149,7 @@ " def test(self,sol):\n", " \n", " assert_equal(sol(2,a),d)\n", - " print 'ALL TEST CASES PASSED'\n", + " print ('ALL TEST CASES PASSED')\n", " \n", "# Run tests\n", "t = TestNLast()\n", @@ -164,9 +166,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3.8.2 64-bit", "language": "python", - "name": "python2" + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" }, "language_info": { "codemirror_mode": { @@ -178,9 +180,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.11" + "version": "3.8.2-final" } }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Linked List Reversal .ipynb b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Linked List Reversal .ipynb index d6d333eb..b8490f1f 100644 --- a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Linked List Reversal .ipynb +++ b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Linked List Reversal .ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 31, "metadata": { "collapsed": true }, @@ -40,15 +40,25 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 32, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def reverse(head):\n", - " \n", - " pass" + " prev = None\n", + " curr = head\n", + " nxt = None\n", + "\n", + " while curr:\n", + " nxt = curr.nextnode\n", + " curr.nextnode = prev\n", + " prev = curr\n", + " curr = nxt\n", + "\n", + "\n", + " " ] }, { @@ -64,7 +74,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 33, "metadata": { "collapsed": true }, @@ -91,42 +101,38 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { - "name": "stdout", "output_type": "stream", - "text": [ - "2\n", - "3\n", - "4\n" - ] + "name": "stdout", + "text": "2\n3\n4\n" } ], "source": [ - "print a.nextnode.value\n", - "print b.nextnode.value\n", - "print c.nextnode.value" + "print (a.nextnode.value)\n", + "print (b.nextnode.value)\n", + "print (c.nextnode.value)" ] }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "error", "ename": "AttributeError", "evalue": "'NoneType' object has no attribute 'value'", - "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0md\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnextnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0md\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnextnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'value'" ] } @@ -144,70 +150,55 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 36, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "<__main__.Node at 0x104bd7dd0>" - ] - }, - "execution_count": 46, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "reverse(a)" ] }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { - "name": "stdout", "output_type": "stream", - "text": [ - "3\n", - "2\n", - "1\n" - ] + "name": "stdout", + "text": "3\n2\n1\n" } ], "source": [ - "print d.nextnode.value\n", - "print c.nextnode.value\n", - "print b.nextnode.value" + "print (d.nextnode.value)\n", + "print (c.nextnode.value)\n", + "print (b.nextnode.value)" ] }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "error", "ename": "AttributeError", "evalue": "'NoneType' object has no attribute 'value'", - "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnextnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m \u001b[0;31m# This will give an error since it now points to None\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnextnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# This will give an error since it now points to None\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'value'" ] } ], "source": [ - "print a.nextnode.value # This will give an error since it now points to None" + "print (a.nextnode.value) # This will give an error since it now points to None" ] }, { @@ -222,9 +213,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3.8.2 64-bit", "language": "python", - "name": "python2" + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" }, "language_info": { "codemirror_mode": { @@ -236,9 +227,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.11" + "version": "3.8.2-final" } }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Singly Linked List Cycle Check.ipynb b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Singly Linked List Cycle Check.ipynb index 8c41dde7..a3ed968e 100644 --- a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Singly Linked List Cycle Check.ipynb +++ b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Singly Linked List Cycle Check.ipynb @@ -17,7 +17,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "metadata": { "collapsed": true }, @@ -42,15 +42,22 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def cycle_check(node):\n", + " mk1 = node\n", + " mk2 = node\n", + " while mk2 != None and mk2.nextnode != None:\n", + " mk1 = mk1.nextnode\n", + " mk2 = mk2.nextnode.nextnode\n", "\n", - " pass #Your function should return a boolean" + " if mk1 == mk2:\n", + " return True\n", + " return False" ] }, { @@ -62,17 +69,15 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { - "name": "stdout", "output_type": "stream", - "text": [ - "ALL TEST CASES PASSED\n" - ] + "name": "stdout", + "text": "ALL TEST CASES PASSED\n" } ], "source": [ @@ -107,7 +112,7 @@ " assert_equal(sol(a),True)\n", " assert_equal(sol(x),False)\n", " \n", - " print \"ALL TEST CASES PASSED\"\n", + " print (\"ALL TEST CASES PASSED\")\n", " \n", "# Run Tests\n", "\n", @@ -125,9 +130,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3.8.2 64-bit", "language": "python", - "name": "python2" + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" }, "language_info": { "codemirror_mode": { @@ -139,9 +144,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.11" + "version": "3.8.2-final" } }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Balanced Parentheses Check .ipynb b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Balanced Parentheses Check .ipynb index 274f3ace..657a344e 100644 --- a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Balanced Parentheses Check .ipynb +++ b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Balanced Parentheses Check .ipynb @@ -20,33 +20,60 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 39, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def balance_check(s):\n", + " stack = []\n", + " for p in s:\n", + " if not stack and (p == ')' or p == '}' or p==']'):\n", + " return False\n", + " if p == '(' or p =='[' or p =='{':\n", + " stack.append(p)\n", + " if p == ')':\n", + " if stack[len(stack)-1] != '(':\n", + " stack.append(p)\n", + " else:\n", + " stack.pop()\n", + " if p == ']':\n", + " if stack[len(stack)-1] != '[':\n", + " stack.append(p)\n", + " else:\n", + " stack.pop()\n", + " if p == '}':\n", + " if stack[len(stack)-1] != '{':\n", + " stack.append(p)\n", + " else:\n", + " stack.pop()\n", " \n", - " pass" + " if not stack:\n", + " return True\n", + " else:\n", + " return False\n" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "stream", + "name": "stdout", + "text": "[]\n['[']\n[]\n" + }, + { + "output_type": "execute_result", "data": { - "text/plain": [ - "True" - ] + "text/plain": "True" }, - "execution_count": 13, "metadata": {}, - "output_type": "execute_result" + "execution_count": 40 } ], "source": [ @@ -55,20 +82,23 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "stream", + "name": "stdout", + "text": "[]\n['[']\n[]\n['(']\n[]\n['{']\n['{', '(']\n['{', '(', '[']\n['{', '(', '[', '[']\n['{', '(', '[', '[', '[']\n['{', '(', '[', '[']\n['{', '(', '[']\n['{', '(']\n['{']\n[]\n" + }, + { + "output_type": "execute_result", "data": { - "text/plain": [ - "True" - ] + "text/plain": "True" }, - "execution_count": 16, "metadata": {}, - "output_type": "execute_result" + "execution_count": 41 } ], "source": [ @@ -77,20 +107,23 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "stream", + "name": "stdout", + "text": "[]\n['(']\n[]\n['(']\n[]\n['{']\n['{', ']']\n['{', ']', '}']\n" + }, + { + "output_type": "execute_result", "data": { - "text/plain": [ - "False" - ] + "text/plain": "False" }, - "execution_count": 17, "metadata": {}, - "output_type": "execute_result" + "execution_count": 42 } ], "source": [ @@ -106,17 +139,15 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { - "name": "stdout", "output_type": "stream", - "text": [ - "ALL TEST CASES PASSED\n" - ] + "name": "stdout", + "text": "[]\n['[']\n[]\n['(']\n[]\n['{']\n['{', '(']\n['{', '(', '[']\n['{', '(', '[', '[']\n['{', '(', '[', '[', '[']\n['{', '(', '[', '[']\n['{', '(', '[']\n['{', '(']\n['{']\n[]\n['(']\n[]\n['[']\n['[', '{']\n['[', '{', '{']\n['[', '{', '{', '{']\n['[', '{', '{', '{', '(']\n['[', '{', '{', '{', '(', '(']\n['[', '{', '{', '{', '(']\n['[', '{', '{', '{']\n['[', '{', '{']\n['[', '{']\n['[']\n[]\n['(']\n['(', '(']\n['(', '(', '(']\n['(', '(']\n['(']\n[]\n[]\n['[']\n['[', '[']\n['[', '[', '[']\n['[', '[']\n['[']\n['[', ')']\n['[', ')', ']']\nALL TEST CASES PASSED\n" } ], "source": [ @@ -131,7 +162,7 @@ " assert_equal(sol('[](){([[[]]])}('),False)\n", " assert_equal(sol('[{{{(())}}}]((()))'),True)\n", " assert_equal(sol('[[[]])]'),False)\n", - " print 'ALL TEST CASES PASSED'\n", + " print ('ALL TEST CASES PASSED')\n", " \n", "# Run Tests\n", "\n", @@ -149,9 +180,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3.8.2 64-bit", "language": "python", - "name": "python2" + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" }, "language_info": { "codemirror_mode": { @@ -163,9 +194,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "3.8.2-final" } }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Queue -Using Two Stacks .ipynb b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Queue -Using Two Stacks .ipynb index 907956a6..09018407 100644 --- a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Queue -Using Two Stacks .ipynb +++ b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Queue -Using Two Stacks .ipynb @@ -33,7 +33,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 29, "metadata": { "collapsed": true }, @@ -50,12 +50,15 @@ " def enqueue(self,element):\n", " \n", " # FILL OUT CODE HERE\n", - " pass\n", + " self.stack1.append(element)\n", " \n", " def dequeue(self):\n", - " \n", " # FILL OUT CODE HERE\n", - " pass " + " while self.stack1:\n", + " self.stack2.append(self.stack1.pop())\n", + " \n", + " return self.stack2.pop()\n", + " " ] }, { @@ -69,21 +72,15 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { - "name": "stdout", "output_type": "stream", - "text": [ - "0\n", - "1\n", - "2\n", - "3\n", - "4\n" - ] + "name": "stdout", + "text": "4\n3\n2\n1\n0\n" } ], "source": [ @@ -92,11 +89,11 @@ "\"\"\"\n", "q = Queue2Stacks()\n", "\n", - "for i in xrange(5):\n", + "for i in range(5):\n", " q.enqueue(i)\n", " \n", - "for i in xrange(5):\n", - " print q.dequeue()" + "for i in range(5):\n", + " print (q.dequeue())" ] }, { @@ -109,9 +106,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3.8.2 64-bit", "language": "python", - "name": "python2" + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" }, "language_info": { "codemirror_mode": { @@ -123,9 +120,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "3.8.2-final" } }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file From bb4cf58a73acc16990fe1142ef8c3c77c6329cf2 Mon Sep 17 00:00:00 2001 From: Young Jin J Date: Sat, 6 Jun 2020 17:52:46 -0400 Subject: [PATCH 4/8] Recursion Done --- ...omework Example Problems - SOLUTIONS.ipynb | 4 +- .../Recursion Homework Example Problems.ipynb | 104 ++++++++------- ...ecursion Problem 1 - Reverse String .ipynb | 36 +++--- ...rsion Problem 2 - String Permutation.ipynb | 55 +++++--- ...rsion Problem 3 - Fibonacci Sequence.ipynb | 85 ++++++------ .../Recursion Problem 4 - Coin Change.ipynb | 122 +++++++++++++++--- 6 files changed, 263 insertions(+), 143 deletions(-) diff --git a/Recursion/Recursion Homework Example Problems - SOLUTIONS.ipynb b/Recursion/Recursion Homework Example Problems - SOLUTIONS.ipynb index f7bafd8e..7f3073c9 100644 --- a/Recursion/Recursion Homework Example Problems - SOLUTIONS.ipynb +++ b/Recursion/Recursion Homework Example Problems - SOLUTIONS.ipynb @@ -280,6 +280,8 @@ " for word in list_of_words:\n", " \n", " # If the current phrase begins with the word, we have a split point!\n", + " # You can use indexing, more traditional way\n", + " # phrase[0:len(word)] == word:\n", " if phrase.startswith(word):\n", " \n", " # Add the word to the output\n", @@ -324,4 +326,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Recursion/Recursion Homework Example Problems.ipynb b/Recursion/Recursion Homework Example Problems.ipynb index ebe52213..d8479bfb 100644 --- a/Recursion/Recursion Homework Example Problems.ipynb +++ b/Recursion/Recursion Homework Example Problems.ipynb @@ -26,33 +26,33 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 69, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def rec_sum(n):\n", - " \n", - " pass" + " if n == 0:\n", + " return 0\n", + " else:\n", + " return n + rec_sum(n-1)" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 70, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "execute_result", "data": { - "text/plain": [ - "10" - ] + "text/plain": "10" }, - "execution_count": 8, "metadata": {}, - "output_type": "execute_result" + "execution_count": 70 } ], "source": [ @@ -72,32 +72,33 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 71, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def sum_func(n):\n", - " pass" + " if n == 0:\n", + " return 0\n", + " else:\n", + " return n%10 + sum_func(n//10)" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 72, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "execute_result", "data": { - "text/plain": [ - "10" - ] + "text/plain": "10" }, - "execution_count": 4, "metadata": {}, - "output_type": "execute_result" + "execution_count": 72 } ], "source": [ @@ -113,20 +114,18 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 73, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "execute_result", "data": { - "text/plain": [ - "1" - ] + "text/plain": "1" }, - "execution_count": 13, "metadata": {}, - "output_type": "execute_result" + "execution_count": 73 } ], "source": [ @@ -136,20 +135,18 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 74, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "execute_result", "data": { - "text/plain": [ - "432" - ] + "text/plain": "432.1" }, - "execution_count": 14, "metadata": {}, - "output_type": "execute_result" + "execution_count": 74 } ], "source": [ @@ -181,20 +178,18 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 75, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "execute_result", "data": { - "text/plain": [ - "['the', 'man', 'ran']" - ] + "text/plain": "['the', 'man', 'ran']" }, - "execution_count": 32, "metadata": {}, - "output_type": "execute_result" + "execution_count": 75 } ], "source": [ @@ -203,20 +198,18 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 76, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "execute_result", "data": { - "text/plain": [ - "['i', 'love', 'dogs', 'John']" - ] + "text/plain": "['i', 'love', 'dogs', 'John']" }, - "execution_count": 33, "metadata": {}, - "output_type": "execute_result" + "execution_count": 76 } ], "source": [ @@ -225,20 +218,18 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 77, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "execute_result", "data": { - "text/plain": [ - "[]" - ] + "text/plain": "[]" }, - "execution_count": 34, "metadata": {}, - "output_type": "execute_result" + "execution_count": 77 } ], "source": [ @@ -247,14 +238,21 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 78, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def word_split(phrase,list_of_words, output = None):\n", - " pass " + " if output is None:\n", + " output = []\n", + "\n", + " for word in list_of_words:\n", + " if phrase[0:len(word)] == word:\n", + " output.append(word)\n", + " return word_split(phrase[len(word):],list_of_words, output )\n", + " return output" ] }, { @@ -269,9 +267,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3.8.2 64-bit", "language": "python", - "name": "python2" + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" }, "language_info": { "codemirror_mode": { @@ -283,9 +281,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "3.8.2-final" } }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 1 - Reverse String .ipynb b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 1 - Reverse String .ipynb index 4d4308f6..c4a3e6e0 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 1 - Reverse String .ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 1 - Reverse String .ipynb @@ -17,33 +17,35 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def reverse(s):\n", - " \n", + " # Base case\n", + " if s == '':\n", + " return ''\n", + " # Recursion \n", + " return reverse(s[1:])+s[0]\n", " pass" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "execute_result", "data": { - "text/plain": [ - "'dlrow olleh'" - ] + "text/plain": "'dlrow olleh'" }, - "execution_count": 10, "metadata": {}, - "output_type": "execute_result" + "execution_count": 2 } ], "source": [ @@ -67,17 +69,15 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { - "name": "stdout", "output_type": "stream", - "text": [ - "PASSED ALL TEST CASES!\n" - ] + "name": "stdout", + "text": "PASSED ALL TEST CASES!\n" } ], "source": [ @@ -94,7 +94,7 @@ " assert_equal(solution('hello world'),'dlrow olleh')\n", " assert_equal(solution('123456789'),'987654321')\n", " \n", - " print 'PASSED ALL TEST CASES!'\n", + " print ('PASSED ALL TEST CASES!')\n", " \n", "# Run Tests\n", "test = TestReverse()\n", @@ -111,9 +111,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3.8.2 64-bit", "language": "python", - "name": "python2" + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" }, "language_info": { "codemirror_mode": { @@ -125,9 +125,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "3.8.2-final" } }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 2 - String Permutation.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 2 - String Permutation.ipynb index e2585c12..a202d780 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 2 - String Permutation.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 2 - String Permutation.ipynb @@ -24,33 +24,52 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def permute(s):\n", - " \n", - " pass" + " out = []\n", + " # Base case\n", + " if len(s) == 1:\n", + " out = [s]\n", + " # Recursion\n", + " else:\n", + " # For every letter in string\n", + " for i, let in enumerate(s):\n", + " # For every permutation resulting from step 2 and 3\n", + " # 'abc' -> i = 0, let = 'a'\n", + " # for perm in permute('' + 'bc')\n", + " # --> 'bc' -> i = 0, let = 'b'\n", + " # for perm in permute('' + 'c')\n", + " # ---> 'c' -> out['c']\n", + " # --> 'bc' -> ['b' + 'c']\n", + " # --> 'bc' -> i = 1, let = 'c'\n", + " # ---> 'b' -> out['b']\n", + " # --> 'bc' -> ['c'+'b'] ==> out ['bc','cb']\n", + " # 'abc' -> ['a'+'bc', 'a'+'cb'] ==> ['abc', 'acb']\n", + " for perm in permute(s[:i] + s[i+1:]):\n", + " # Add it to the output\n", + " out += [let+perm]\n", + " return out" ] }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "execute_result", "data": { - "text/plain": [ - "['abc', 'acb', 'bac', 'bca', 'cab', 'cba']" - ] + "text/plain": "['abc', 'acb', 'bac', 'bca', 'cab', 'cba']" }, - "execution_count": 24, "metadata": {}, - "output_type": "execute_result" + "execution_count": 3 } ], "source": [ @@ -67,17 +86,15 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { - "name": "stdout", "output_type": "stream", - "text": [ - "All test cases passed.\n" - ] + "name": "stdout", + "text": "All test cases passed.\n" } ], "source": [ @@ -94,7 +111,7 @@ " assert_equal(sorted(solution('abc')),sorted(['abc', 'acb', 'bac', 'bca', 'cab', 'cba']))\n", " assert_equal(sorted(solution('dog')),sorted(['dog', 'dgo', 'odg', 'ogd', 'gdo', 'god']) )\n", " \n", - " print 'All test cases passed.'\n", + " print ('All test cases passed.')\n", " \n", "\n", "\n", @@ -113,9 +130,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3.8.2 64-bit", "language": "python", - "name": "python2" + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" }, "language_info": { "codemirror_mode": { @@ -127,9 +144,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "3.8.2-final" } }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 3 - Fibonacci Sequence.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 3 - Fibonacci Sequence.ipynb index e85c24d0..96f70909 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 3 - Fibonacci Sequence.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 3 - Fibonacci Sequence.ipynb @@ -41,33 +41,35 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def fib_rec(n):\n", - " \n", - " pass" + " if n == 0:\n", + " return 0\n", + " if n == 1:\n", + " return 1\n", + " else:\n", + " return fib_rec(n-1) + fib_rec(n-2)" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "execute_result", "data": { - "text/plain": [ - "55" - ] + "text/plain": "55" }, - "execution_count": 6, "metadata": {}, - "output_type": "execute_result" + "execution_count": 22 } ], "source": [ @@ -85,7 +87,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 29, "metadata": { "collapsed": false }, @@ -97,26 +99,36 @@ "\n", "\n", "def fib_dyn(n):\n", + " if n == 0:\n", + " return 0\n", + " if n == 1:\n", + " return 1\n", + "\n", + " # Look up at the cache if nth slot is not None (empty)\n", + " # nth slot contains the n Fibonacci sequence (etc cache[5] = 5)\n", + " if cache[n] != None:\n", + " return cache[n]\n", " \n", - " pass" + " # Compute Fib number and store them in cache\n", + " cache[n] = fib_dyn(n-1) + fib_dyn(n-2)\n", + " \n", + " return cache[n]\n" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "execute_result", "data": { - "text/plain": [ - "55" - ] + "text/plain": "55" }, - "execution_count": 9, "metadata": {}, - "output_type": "execute_result" + "execution_count": 30 } ], "source": [ @@ -134,37 +146,38 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 33, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def fib_iter(n):\n", - " \n", - " pass" + " a, b = 0, 1\n", + "\n", + " for i in range(n):\n", + " a, b = b, a+b\n", + " return a" ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "execute_result", "data": { - "text/plain": [ - "28657" - ] + "text/plain": "3" }, - "execution_count": 14, "metadata": {}, - "output_type": "execute_result" + "execution_count": 35 } ], "source": [ - "fib_iter(23)" + "fib_iter(4)" ] }, { @@ -178,17 +191,15 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { - "name": "stdout", "output_type": "stream", - "text": [ - "Passed all tests.\n" - ] + "name": "stdout", + "text": "Passed all tests.\n" } ], "source": [ @@ -205,7 +216,7 @@ " assert_equal(solution(10),55)\n", " assert_equal(solution(1),1)\n", " assert_equal(solution(23),28657)\n", - " print 'Passed all tests.'\n", + " print ('Passed all tests.')\n", "# UNCOMMENT FOR CORRESPONDING FUNCTION\n", "t = TestFib()\n", "\n", @@ -226,9 +237,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3.8.2 64-bit", "language": "python", - "name": "python2" + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" }, "language_info": { "codemirror_mode": { @@ -240,9 +251,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "3.8.2-final" } }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 4 - Coin Change.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 4 - Coin Change.ipynb index f8785dfb..baf392d7 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 4 - Coin Change.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 4 - Coin Change.ipynb @@ -39,39 +39,118 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ + "# SIMPLE RECURSION ONLY W/O DP\n", + "# TAKING SUPER LONG BECAUSE IT KEPT MAKING REPEATED RECURSIVE CALL FOR RESULTS\n", "def rec_coin(target,coins):\n", " \n", - " pass" + " # DEFAULT VALUE SET TO TARGET\n", + " min_coins = target\n", + "\n", + " # Base case\n", + " # Single coin match\n", + " if target in coins:\n", + " return 1\n", + " # Recursion\n", + " else:\n", + " # For every coin value that is <= my target\n", + " for i in [c for c in coins if c<= target]:\n", + " # ADD A COIN COUNT (1) + RECURSIVE WITH NEW TARGET (target-i)\n", + " num_coins = 1 + rec_coin(target-i, coins)\n", + "\n", + " # Reset minimum if new num_coins < min_coins\n", + " if num_coins < min_coins:\n", + " min_coins = num_coins\n", + "\n", + "\n", + " return min_coins\n", + " \n", + "\n", + " \n", + " " ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { + "output_type": "execute_result", "data": { - "text/plain": [ - "2" - ] + "text/plain": "2" }, - "execution_count": 3, "metadata": {}, - "output_type": "execute_result" + "execution_count": 2 } ], "source": [ "rec_coin(10,[1,5])" ] }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# RECURSION WITH DP\n", + "def rec_coin_dynam(target, coins, known_results):\n", + " # Default output to target\n", + " min_coins = target\n", + "\n", + " # Base case\n", + " # If I kept doing the recursive call and suddenly I find \n", + " # that the target number is actually in coins\n", + " # then set known_results[target] = 1\n", + " if target in coins:\n", + " known_results[target] = 1\n", + " return 1\n", + " # Return a known result if it happens to be greater than 1\n", + " elif known_results[target] > 0:\n", + " return known_results[target]\n", + " else:\n", + " # For every coin value that is <= target\n", + " for i in [c for c in coins if c <= target]:\n", + " num_coins = 1 + rec_coin_dynam(target-i, coins, known_results)\n", + " if num_coins < min_coins:\n", + " min_coins = num_coins\n", + "\n", + " # Reset that known result\n", + " known_results[target] = min_coins\n", + "\n", + " return min_coins" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "8" + }, + "metadata": {}, + "execution_count": 7 + } + ], + "source": [ + "target = 74\n", + "coins = [1,5,10,25]\n", + "known_results = [0] * (target+1)\n", + "\n", + "rec_coin_dynam(target, coins, known_results)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -85,11 +164,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "output_type": "error", + "ename": "TypeError", + "evalue": "rec_coin_dynam() missing 1 required positional argument: 'known_results'", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0mtest\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mTestCoins\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 20\u001b[0;31m \u001b[0mtest\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcheck\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrec_coin_dynam\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mcheck\u001b[0;34m(self, solution)\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcheck\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0msolution\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0mcoins\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m25\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0massert_equal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msolution\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m45\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mcoins\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 14\u001b[0m \u001b[0massert_equal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msolution\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m23\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mcoins\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0massert_equal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msolution\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m74\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mcoins\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: rec_coin_dynam() missing 1 required positional argument: 'known_results'" + ] + } + ], "source": [ "\"\"\"\n", "RUN THIS CELL TO TEST YOUR FUNCTION.\n", @@ -106,7 +198,7 @@ " assert_equal(solution(45,coins),3)\n", " assert_equal(solution(23,coins),5)\n", " assert_equal(solution(74,coins),8)\n", - " print 'Passed all tests.'\n", + " print ('Passed all tests.')\n", "# Run Test\n", "\n", "test = TestCoins()\n", @@ -125,9 +217,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3.8.2 64-bit", "language": "python", - "name": "python2" + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" }, "language_info": { "codemirror_mode": { @@ -139,9 +231,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "3.8.2-final" } }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file From 11f84dba54c1447b5ce6671bc729905c4ba89d58 Mon Sep 17 00:00:00 2001 From: Young Jin J Date: Sun, 7 Jun 2020 18:30:40 -0400 Subject: [PATCH 5/8] Trees Done --- Trees/Binary Heap Implementation.ipynb | 66 +++- Trees/Binary Search Trees.ipynb | 52 +++- Trees/Tree List Implementation Lecture.ipynb | 220 ++++++++++++++ Trees/Tree Node Reference Lecture.ipynb | 286 ++++++++++++++++++ .../Binary Search Tree Check.ipynb | 21 +- .../Tree Level Order Print.ipynb | 175 ++++++++++- .../Trim a Binary Search Tree .ipynb | 125 +++++++- 7 files changed, 910 insertions(+), 35 deletions(-) create mode 100644 Trees/Tree List Implementation Lecture.ipynb create mode 100644 Trees/Tree Node Reference Lecture.ipynb diff --git a/Trees/Binary Heap Implementation.ipynb b/Trees/Binary Heap Implementation.ipynb index e5630cee..a9fd03b5 100644 --- a/Trees/Binary Heap Implementation.ipynb +++ b/Trees/Binary Heap Implementation.ipynb @@ -6,7 +6,23 @@ "source": [ "# Binary Heap Implementation\n", "\n", - "Here is the reference code for the Binary Heap Implementation. Make sure to refer to the video lecture for the full explanation!" + "Here is the reference code for the Binary Heap Implementation. Make sure to refer to the video lecture for the full explanation!\n", + "\n", + "* -> ***Complete Binary Tree***: all the levels are completely filled except possibly the last level and the last level has all keys as left as possible.\n", + "\n", + "* ***Full Binary Tree***: a full binary tree if every node has 0 or 2 children.\n", + "\n", + "* ***Perfect Binary Tree***: all the internal nodes have two children and all leaf nodes are at the same level.\n", + "\n", + "\n", + "A complete BInary tree can be represented in a single list. For a node list\\[p\\], to find left child of the node simply look for list\\[2*p\\]. For right child, list\\[2*p+1\\]\n", + "\n", + "\n", + "***Heap Property***:\n", + "\n", + "For any given node C, if P is a parent node of C, \n", + "* Max Heap: the key (the value) of P.key >= C.key (Top heavy)\n", + "* Min Heap: the key of P.key <= C.key (Bottom heavy)" ] }, { @@ -35,26 +51,34 @@ "source": [ "class BinHeap:\n", " def __init__(self):\n", - " self.heapList = [0]\n", + " self.heapList = [0] # Not used, just the placeholder for integer division\n", " self.currentSize = 0\n", "\n", "\n", " def percUp(self,i):\n", " \n", + " # Check out of bound\n", " while i // 2 > 0:\n", " \n", + " # If currentNode.key < parentNode.key\n", " if self.heapList[i] < self.heapList[i // 2]:\n", " \n", - " \n", + " # Set temp = parentNode.key\n", " tmp = self.heapList[i // 2]\n", + " # ParentNode = currentNode (switch parent and curr)\n", " self.heapList[i // 2] = self.heapList[i]\n", + " # Reposition parentNode to current place \n", " self.heapList[i] = tmp\n", + " \n", + " # Update the index\n", " i = i // 2\n", "\n", " def insert(self,k):\n", - " \n", + " # Simply append new item to the heaplist\n", " self.heapList.append(k)\n", + " # Increment size\n", " self.currentSize = self.currentSize + 1\n", + " # Give thing to percUp method to take care of the rest\n", " self.percUp(self.currentSize)\n", "\n", " def percDown(self,i):\n", @@ -81,19 +105,45 @@ " else:\n", " return i * 2 + 1\n", "\n", + " # 1. root is the minimum val so remove it. \n", + " # 2. need to restore the heap\n", + " # - 1. restore the root item by taking the last item in the list and moving it to the root position\n", + " # - 2. pushing the new root node down the tree to its proper position\n", " def delMin(self):\n", + " # Get return value (minimum value which is the root)\n", " retval = self.heapList[1]\n", + " # Before removing it, replace the root with the last item of the heap\n", " self.heapList[1] = self.heapList[self.currentSize]\n", + " # Decrement currentSize\n", " self.currentSize = self.currentSize - 1\n", + " # Pop out the last item of the heap because now it's moved to the root\n", " self.heapList.pop()\n", + " # percDown the new root to its correct position\n", " self.percDown(1)\n", + " # return the prevRoot \n", " return retval\n", "\n", + "\n", + " # Build an entire heap from a list of keys\n", + " # Possible method 1:\n", + " # - Build a heap by inserting each key one at a time\n", + " # - This will take O(logn), since the list only contains one key, and use binary search to find right position\n", + " # - But inserting an item in the middle of the list may require O(n). Therefore to insert n keys into the heap would take O(nlogn)\n", + " # Possible method 2: <--\n", + " # - Start with an eitre list\n", + " # - O(n) operations\n", " def buildHeap(self,alist):\n", + "\n", + " # Starts from the middle of the list\n", " i = len(alist) // 2\n", " self.currentSize = len(alist)\n", + "\n", + " # Insert 0 in 0th index of the alist\n", + " # [9,6,5,2,3] -> [0,9,6,5,2,3]\n", " self.heapList = [0] + alist[:]\n", + "\n", " while (i > 0):\n", + " # perDown to find i right position\n", " self.percDown(i)\n", " i = i - 1" ] @@ -110,9 +160,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3.8.2 64-bit", "language": "python", - "name": "python2" + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" }, "language_info": { "codemirror_mode": { @@ -124,9 +174,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.11" + "version": "3.8.2-final" } }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Trees/Binary Search Trees.ipynb b/Trees/Binary Search Trees.ipynb index c6679bb0..c17dafea 100644 --- a/Trees/Binary Search Trees.ipynb +++ b/Trees/Binary Search Trees.ipynb @@ -11,7 +11,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This is the code ot go along with the video explanation. Check out the video lecture for full details!" + "This is the code ot go along with the video explanation. Check out the video lecture for full details!\n", + "\n", + "***Binary Search Tree Property***: \n", + "* C.key < P.key, C is left subtree of P\n", + "* C.key > P.key, C is right subtree of P" ] }, { @@ -78,6 +82,11 @@ " def __len__(self):\n", " return self.size\n", "\n", + " # Starting at the root of the tree, search the binary tree comparing the new key to the key\n", + " # in the current node.\n", + " # If newKey < currKey -> search the left subtree \n", + " # If newKey > currKey -> search the right subtree\n", + " # If there's no left(or right) subtree, found the position in the tree where the newKey should be\n", " def put(self,key,val):\n", " if self.root:\n", " self._put(key,val,self.root)\n", @@ -100,6 +109,9 @@ " def __setitem__(self,k,v):\n", " self.put(k,v)\n", "\n", + "\n", + " # Search the tree recursively until it gets to a non-matching leaf node or finds a matching key\n", + " # When a matching key is found, the value stored in the payload of the node is returned\n", " def get(self,key):\n", " if self.root:\n", " res = self._get(key,self.root)\n", @@ -131,17 +143,24 @@ " else:\n", " return False\n", "\n", + "\n", + " \n", " def delete(self,key):\n", - " \n", + " # First, find the node to delete by searching the tree using _get()\n", + " # Check to see if the size is greater than 1 (has more than root)\n", " if self.size > 1:\n", " \n", + " # Search the nodeToRemove using _get() with key to look for and the root\n", " nodeToRemove = self._get(key,self.root)\n", + " # When found, remove the node and decrement the size of the tree\n", " if nodeToRemove:\n", " self.remove(nodeToRemove)\n", " self.size = self.size-1\n", " else:\n", " raise KeyError('Error, key not in tree')\n", + " # If tree only has root\n", " elif self.size == 1 and self.root.key == key:\n", + " # Simply set root to be None and decrement the size of the tree\n", " self.root = None\n", " self.size = self.size - 1\n", " else:\n", @@ -177,6 +196,8 @@ " self.parent.rightChild = self.rightChild\n", " self.rightChild.parent = self.parent\n", "\n", + " # The successor is guaranteed to have no more than one child, so we know how to remove it using the two cases for deletion\n", + " # Once the successor has been removed, we simply put it in the tree in place of the node to be deleted\n", " def findSuccessor(self):\n", " \n", " succ = None\n", @@ -202,12 +223,19 @@ " return current\n", "\n", " def remove(self,currentNode):\n", - " \n", + " # If the current node is leaf (no children)\n", + " # all need to dois delete the node and remove the reference to this node in the parent\n", " if currentNode.isLeaf(): #leaf\n", " if currentNode == currentNode.parent.leftChild:\n", " currentNode.parent.leftChild = None\n", " else:\n", " currentNode.parent.rightChild = None\n", + "\n", + " # If the current node has both children\n", + " # It is unlikely that it can simply promote one of children to take the node's place\n", + " # Search the tree for a node that can be used to replace the one scheduled for deletion\n", + " # - need a node that will preserve the binary search tree relationship for both of the existing left and right subtree\n", + " # - the node that will do this is the node that has the next-largest key in the tree, \"successor\"\n", " elif currentNode.hasBothChildren(): #interior\n", " \n", " succ = currentNode.findSuccessor()\n", @@ -215,22 +243,32 @@ " currentNode.key = succ.key\n", " currentNode.payload = succ.payload\n", "\n", + " # If the current node has single child\n", + " # promote the child to tkae the place of its parent\n", " else: # this node has one child\n", + " # If the child is the current node's left child\n", " if currentNode.hasLeftChild():\n", + " # If the current node is its parent's left child\n", " if currentNode.isLeftChild():\n", + " # Bring current node's left childup , updating current node's left child's parent to be current node's parent\n", " currentNode.leftChild.parent = currentNode.parent\n", + " # Update the parent's left child to be current node's left child\n", " currentNode.parent.leftChild = currentNode.leftChild\n", + " # If the current node is its parent's right child\n", " elif currentNode.isRightChild():\n", + " # Bring current node's left child up, updating current node's left child's parent to be current node's parent\n", " currentNode.leftChild.parent = currentNode.parent\n", + " # Update the parent's right child to be current node's left child\n", " currentNode.parent.rightChild = currentNode.leftChild\n", + " # current node is actually the root\n", " else:\n", - " \n", " currentNode.replaceNodeData(currentNode.leftChild.key,\n", " currentNode.leftChild.payload,\n", " currentNode.leftChild.leftChild,\n", " currentNode.leftChild.rightChild)\n", - " else:\n", - " \n", + "\n", + " # If the child is the current node's right child\n", + " else: \n", " if currentNode.isLeftChild():\n", " currentNode.rightChild.parent = currentNode.parent\n", " currentNode.parent.leftChild = currentNode.rightChild\n", @@ -302,4 +340,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Trees/Tree List Implementation Lecture.ipynb b/Trees/Tree List Implementation Lecture.ipynb new file mode 100644 index 00000000..4a600d4f --- /dev/null +++ b/Trees/Tree List Implementation Lecture.ipynb @@ -0,0 +1,220 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Tree List Implementation Lecture\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Constructs a list with root node r and empty sublists for children [root, [left], [right]]\n", + "def BinaryTree(r):\n", + " return [r, [],[]]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "def insertLeft(root, newBranch):\n", + " t = root.pop(1) # Current left child\n", + "\n", + " # Left is already occupied\n", + " if len(t) > 1:\n", + " # Push already exsiting one down to insert new branch to be new left of the root\n", + " # t (already existing one) becomes leftchild of newBranch\n", + " root.insert(1, [newBranch, t, []])\n", + " else:\n", + " root.insert(1, [newBranch, [],[]])\n", + " return root" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "def insertRight(root, newBranch):\n", + " t = root.pop(2) # Current right child\n", + "\n", + " # Right is already occupied\n", + " if len(t) > 1:\n", + " # Push already exsiting one down to insert new branch to be new left of the root\n", + " # t (already existing one) becomes leftchild of newBranch\n", + " root.insert(2, [newBranch, [], t])\n", + " else:\n", + " root.insert(2, [newBranch, [],[]])\n", + " return root" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "def getRootVal(root):\n", + " return root[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "def setRootVal(root, newVal):\n", + " root[0] = newVal\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "def getLeftChild(root):\n", + " return root[1]\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "def getRightChild(root):\n", + " return root[2]" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "r = BinaryTree(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "[3, [4, [], []], []]" + }, + "metadata": {}, + "execution_count": 18 + } + ], + "source": [ + "insertLeft(r, 4)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "[3, [5, [4, [], []], []], []]" + }, + "metadata": {}, + "execution_count": 19 + } + ], + "source": [ + "insertLeft(r, 5)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "[3, [5, [4, [], []], []], [6, [], []]]" + }, + "metadata": {}, + "execution_count": 20 + } + ], + "source": [ + "insertRight(r, 6)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "l = getLeftChild(r)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "[5, [4, [], []], []]\n" + } + ], + "source": [ + "print(l)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.8.2 64-bit", + "language": "python", + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "3.8.2-final" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file diff --git a/Trees/Tree Node Reference Lecture.ipynb b/Trees/Tree Node Reference Lecture.ipynb new file mode 100644 index 00000000..a638153e --- /dev/null +++ b/Trees/Tree Node Reference Lecture.ipynb @@ -0,0 +1,286 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Nodes and References Implementation of a Tree Lecture\n" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class BinaryTree(object):\n", + " def __init__(self, rootObj):\n", + " self.key = rootObj\n", + " self.leftChild = None\n", + " self.rightChild = None\n", + "\n", + " # To add a left child to the tree, create a new binary tree object and set the left attribute \n", + " # of the root to refer to this new object\n", + " def insertLeft(self, newNode):\n", + " # No existing left child\n", + " if self.leftChild == None:\n", + " # Simply set new BinaryTree object with newNode to self.leftChild\n", + " self.leftChild = BinaryTree(newNode)\n", + " # Existing left child\n", + " # Create a new tree with newNode and push the current leftchild down to be new tree's leftchild\n", + " else:\n", + " # Create a new BinaryTree with newNode\n", + " t = BinaryTree(newNode)\n", + " # Set current leftchild to be t's leftchild \n", + " t.leftChild = self.leftChild\n", + " # And update self.leftchild to t\n", + " self.leftChild = t\n", + " def insertRight(self, newNode):\n", + " if self.rightChild == None:\n", + " self.rightChild = BinaryTree(newNode)\n", + " \n", + " else:\n", + " t = BinaryTree(newNode)\n", + " t.rightChild = self.reftChild\n", + " self.rightChild = t\n", + "\n", + "\n", + "\n", + " def getRightChild(self):\n", + " return self.rightChild\n", + " \n", + " def getLeftChild(self):\n", + " return self.leftChild\n", + " \n", + " def setRootVal(self, obj):\n", + " self.key = obj\n", + " \n", + " def getRootVal(self):\n", + " return self.key\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [], + "source": [ + "def preorder(tree):\n", + " if tree:\n", + " print(tree.getRootVal())\n", + " preorder(tree.getLeftChild())\n", + " preorder(tree.getRightChild())" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + "def inorder(tree):\n", + " if tree:\n", + " inorder(tree.getLeftChild())\n", + " print(tree.getRootVal())\n", + " inorder(tree.getRightChild())\n" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [], + "source": [ + "def postorder(tree):\n", + " if tree:\n", + " postorder(tree.getLeftChild())\n", + " postorder(tree.getRightChild())\n", + " print(tree.getRootVal())" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [], + "source": [ + "r = BinaryTree('a')\n" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "'a'" + }, + "metadata": {}, + "execution_count": 44 + } + ], + "source": [ + "r.getRootVal()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [], + "source": [ + "r.getLeftChild()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [], + "source": [ + "r.insertLeft('b')" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": ">" + }, + "metadata": {}, + "execution_count": 47 + } + ], + "source": [ + "r.getLeftChild" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "<__main__.BinaryTree at 0x7f8cd67bfbb0>" + }, + "metadata": {}, + "execution_count": 48 + } + ], + "source": [ + "r.getLeftChild()" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "'b'" + }, + "metadata": {}, + "execution_count": 49 + } + ], + "source": [ + "r.getLeftChild().getRootVal()" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [], + "source": [ + "r.insertRight('c')\n" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "a\nb\nc\n" + } + ], + "source": [ + "preorder(r)" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "b\na\nc\n" + } + ], + "source": [ + "inorder(r)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "b\nc\na\n" + } + ], + "source": [ + "postorder(r)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.8.2 64-bit", + "language": "python", + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "3.8.2-final" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file diff --git a/Trees/Trees Interview Problems/Binary Search Tree Check.ipynb b/Trees/Trees Interview Problems/Binary Search Tree Check.ipynb index 74a68fe1..e6606da2 100644 --- a/Trees/Trees Interview Problems/Binary Search Tree Check.ipynb +++ b/Trees/Trees Interview Problems/Binary Search Tree Check.ipynb @@ -25,7 +25,24 @@ }, "outputs": [], "source": [ - "# Code goes Here" + "# Code goes Here\n", + "\n", + "# Binary Tree is simply a non linear data structure where each node can have at most 2 child nodes.\n", + "# Binary Search Tree is the tree that has leftchild < current and rightchild > current\n", + "\n", + "# If a tree is a binary search tree, then traversing the tree INORDER should lead to the SORTED ORDER of the values in the tree.\n", + "\n", + "values = []\n", + "def inorder(tree):\n", + " if tree:\n", + " inorder.(tree.getLeftChild())\n", + " values.append(tree.getRootVal())\n", + " inorder.(tree.getRightChild())\n", + "\n", + "if sorted(tree) == values:\n", + " return True\n", + "return False\n", + "\n" ] }, { @@ -57,4 +74,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Trees/Trees Interview Problems/Tree Level Order Print.ipynb b/Trees/Trees Interview Problems/Tree Level Order Print.ipynb index 7d44b4d9..509f5c2a 100644 --- a/Trees/Trees Interview Problems/Tree Level Order Print.ipynb +++ b/Trees/Trees Interview Problems/Tree Level Order Print.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "metadata": { "collapsed": true }, @@ -43,23 +43,182 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ + "\n", + "# Recursive Python program for level order traversal of Binary Tree \n", "def levelOrderPrint(tree):\n", - " #Code here\n", - " pass" + " for d in range(height(tree)):\n", + " GivenLevelPrint(tree, d)\n", + " \n", + "\n", + "def GivenLevelPrint(tree, level):\n", + " if tree:\n", + " if level == 1:\n", + " print(tree.val)\n", + " elif level > 1:\n", + " GivenLevelPrint(tree.left, level-1)\n", + " GivenLevelPrint(tree.right, level-1)\n", + "\n", + "# Compute the height of a tree--the number of nodes along the longest path from the root node down to the farthest leaf node \n", + "def height(node):\n", + " if node is None:\n", + " return 0\n", + " else:\n", + " # Compute the height of each subtree\n", + " lheight = height(node.left)\n", + " rheight = height(node.right)\n", + " if lheight > rheight:\n", + " return lheight+1\n", + " else:\n", + " return rheight+1" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "# Breadth Traversal using Queue\n", + "# 1) Create an empty queue q\n", + "# 2) temp_node = root /*start from root*/\n", + "# 3) Loop while temp_node is not NULL\n", + "# a) print temp_node->data.\n", + "# b) Enqueue temp_node’s children (first left then right children) to q\n", + "# c) Dequeue a node from q and assign it’s value to temp_node\n", + "\n", + "\n", + "\n", + "def levelOrderPrint_Queue(tree):\n", + " \n", + " if tree:\n", + " queue = []\n", + " queue.append(tree)\n", + " while len(queue):\n", + " print(queue[len(queue)-1].val)\n", + " temp = queue.pop()\n", + " if temp.left:\n", + " queue.insert(0, temp.left)\n", + " if temp.right:\n", + " queue.insert(0, temp.right)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "root = Node(1) \n", + "root.left = Node(2) \n", + "root.right = Node(3) \n", + "root.left.left = Node(4) \n", + "root.left.right = Node(5) " + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "1\n2\n3\n4\n5\n" + } + ], + "source": [ + "levelOrderPrint_Queue(root)" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [], + "source": [ + "# SOLUTION\n", + "import collections\n", + "def levelOrderPrint_Sol(tree):\n", + " if not tree:\n", + " return\n", + " nodes = collections.deque([tree])\n", + " currentCount = 1\n", + " nextCount = 0\n", + " while len(nodes) != 0:\n", + " currentNode = nodes.popleft()\n", + " currentCount -= 1\n", + "\n", + " print(currentNode.val, end=\" \")\n", + " if currentNode.left:\n", + " nodes.append(currentNode.left)\n", + " nextCount += 1\n", + " if currentNode.right:\n", + " nodes.append(currentNode.right)\n", + " nextCount += 1\n", + "\n", + " # Printing current level is done\n", + " if currentCount == 0:\n", + " # Print new line\n", + " print ('\\n')\n", + " # Switch currentCount and nextCount\n", + " currentCount, nextCount = nextCount, currentCount\n" ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [], + "source": [ + "root = Node(1) \n", + "root.left = Node(2) \n", + "root.right = Node(3) \n", + "root.left.left = Node(4) \n", + "root.left.right = Node(5) " + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "1 \n\n2 3 \n\n4 5 \n\n" + } + ], + "source": [ + "levelOrderPrint_Sol(root)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3.8.2 64-bit", "language": "python", - "name": "python2" + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" }, "language_info": { "codemirror_mode": { @@ -71,9 +230,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.11" + "version": "3.8.2-final" } }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Trees/Trees Interview Problems/Trim a Binary Search Tree .ipynb b/Trees/Trees Interview Problems/Trim a Binary Search Tree .ipynb index b3aa23dd..3f484fc9 100644 --- a/Trees/Trees Interview Problems/Trim a Binary Search Tree .ipynb +++ b/Trees/Trees Interview Problems/Trim a Binary Search Tree .ipynb @@ -25,7 +25,20 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 67, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self, val=None):\n", + " self.left = None\n", + " self.right = None\n", + " self.val = val" + ] + }, + { + "cell_type": "code", + "execution_count": 68, "metadata": { "collapsed": true }, @@ -33,15 +46,107 @@ "source": [ "def trimBST(tree,minVal,maxVal):\n", " \n", - " print tree.left # LeftChild\n", - " print tree.right # Right Child\n", - " print tree.val # Node's value\n", - " \n", - " pass\n", + " # PostOrder = L -> R -> V\n", + " # Doesn't work in other ways it seems (No Inorder nor preorder)\n", + " # Process the left first and then the right\n", + " # building new tree bottom up from the leaves towards the root \n", + " # As a result, while processing the node itself, both its left and right subtrees are valid trimmed BST\n", + "\n", + " if not tree:\n", + " return\n", + " # L \n", + " tree.left = trimBST(tree.left, minVal, maxVal)\n", "\n", + " # R\n", + " tree.right = trimBST(tree.right, minVal, maxVal)\n", + "\n", + " # V\n", + " # Return a reference based on its value\n", + " # which will then be assigned to its parent's left or right child pointer \n", + " # depending on whether the current node is the left or right child of the parent.\n", + "\n", + " # If current nodes value is minVal <= val <= maxVal\n", + " if minVal <= tree.val <= maxVal:\n", + " # Just return the reference to the node itself\n", + " # With the recursive calls above, it will take care of all the branches\n", + " return tree\n", + "\n", + " # If current nodes value is < minVal\n", + " if tree.val < minVal:\n", + " # Return the reference to its right subtree (and discard the left subtree)\n", + " return tree.right\n", + "\n", + " # If current nodes value is > maxVal\n", + " if tree.val > maxVal:\n", + " # Return the reference to its left subtree (and discard the right subtree)\n", + " return tree.left\n", + " # # R\n", + " \n", "# Use tree.left , tree.right , and tree.val as your methods to call" ] }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [], + "source": [ + "import collections\n", + "def levelOrderPrint_Sol(tree):\n", + " if not tree:\n", + " return\n", + " nodes = collections.deque([tree])\n", + " currentCount = 1\n", + " nextCount = 0\n", + " while len(nodes) != 0:\n", + " currentNode = nodes.popleft()\n", + " currentCount -= 1\n", + "\n", + " print(currentNode.val, end=\" \")\n", + " if currentNode.left:\n", + " nodes.append(currentNode.left)\n", + " nextCount += 1\n", + " if currentNode.right:\n", + " nodes.append(currentNode.right)\n", + " nextCount += 1\n", + "\n", + " # Printing current level is done\n", + " if currentCount == 0:\n", + " # Print new line\n", + " print ('\\n')\n", + " # Switch currentCount and nextCount\n", + " currentCount, nextCount = nextCount, currentCount" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [], + "source": [ + "root = Node(1) \n", + "root.left = Node(2) \n", + "root.right = Node(3) \n", + "root.left.left = Node(4) \n", + "root.left.right = Node(5)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "1 \n\n2 3 \n\n" + } + ], + "source": [ + "a = trimBST(root, 1, 3)\n", + "levelOrderPrint_Sol(a)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -54,9 +159,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3.8.2 64-bit", "language": "python", - "name": "python2" + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" }, "language_info": { "codemirror_mode": { @@ -68,9 +173,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.11" + "version": "3.8.2-final" } }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file From 35b48150d73c30a8a86913973e78ed87013a5c79 Mon Sep 17 00:00:00 2001 From: Young Jin J Date: Mon, 8 Jun 2020 16:15:21 -0400 Subject: [PATCH 6/8] Sorting and bin search done --- .../Binary Search Lecture.ipynb | 132 +++++++++++ .../Bubble Sort Lecture.ipynb | 89 ++++++++ .../Hash Table Lecture.ipynb | 209 ++++++++++++++++++ .../Insertion Sort Lecture.ipynb | 94 ++++++++ .../Merge Sort Lecture.ipynb | 126 +++++++++++ .../Quick Sort Lecture.ipynb | 76 +++++++ .../Selection Sort Lecture.ipynb | 107 +++++++++ .../Sequential Search Lecture.ipynb | 121 ++++++++++ .../Shell Sort Lecture.ipynb | 110 +++++++++ 9 files changed, 1064 insertions(+) create mode 100644 Sorting and Searching/Binary Search Lecture.ipynb create mode 100644 Sorting and Searching/Bubble Sort Lecture.ipynb create mode 100644 Sorting and Searching/Hash Table Lecture.ipynb create mode 100644 Sorting and Searching/Insertion Sort Lecture.ipynb create mode 100644 Sorting and Searching/Merge Sort Lecture.ipynb create mode 100644 Sorting and Searching/Quick Sort Lecture.ipynb create mode 100644 Sorting and Searching/Selection Sort Lecture.ipynb create mode 100644 Sorting and Searching/Sequential Search Lecture.ipynb create mode 100644 Sorting and Searching/Shell Sort Lecture.ipynb diff --git a/Sorting and Searching/Binary Search Lecture.ipynb b/Sorting and Searching/Binary Search Lecture.ipynb new file mode 100644 index 00000000..12300829 --- /dev/null +++ b/Sorting and Searching/Binary Search Lecture.ipynb @@ -0,0 +1,132 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Binary Search Lecture" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def binary_search(arr, ele):\n", + " # Idxs\n", + " first = 0\n", + " last = len(arr)-1\n", + "\n", + " found = False\n", + "\n", + " # First idx <= last and ele not found yet\n", + " while first <= last and not found:\n", + " mid = (first + last)//2\n", + " if arr[mid] == ele:\n", + " found = True\n", + " # Deciding which half of the array the idxs should be moved to\n", + " else:\n", + " # If element is < arr[mid]\n", + " # Move the last index to be mid-1\n", + " # so second half of the original array is ignored\n", + " if ele < arr[mid]:\n", + " last = mid-1\n", + " # Else if element is > arr[mid]\n", + " # Move the first index to be mid+1\n", + " # so first half of the original array is ignored\n", + " else: \n", + " first = mid+1\n", + "\n", + " return found\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "False" + }, + "metadata": {}, + "execution_count": 3 + } + ], + "source": [ + "arr = [1,2,3,4,5,6,7,8,9,10]\n", + "binary_search(arr, 11)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "def rec_bin_search(arr, ele):\n", + " if len(arr) == 0:\n", + " return False\n", + " \n", + " else:\n", + " mid = len(arr)//2\n", + " if arr[mid] == ele:\n", + " return True\n", + " else:\n", + " if ele < arr[mid]:\n", + " return rec_bin_search(arr[:mid], ele)\n", + " else:\n", + " return rec_bin_search(arr[mid+1:], ele)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "True" + }, + "metadata": {}, + "execution_count": 7 + } + ], + "source": [ + "arr = [1,2,3,4,5,6,7,8,9,10]\n", + "binary_search(arr, 10)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.2-final" + }, + "orig_nbformat": 2, + "kernelspec": { + "name": "python38264bite57af90baf4c458aa9d139163dfc9007", + "display_name": "Python 3.8.2 64-bit" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file diff --git a/Sorting and Searching/Bubble Sort Lecture.ipynb b/Sorting and Searching/Bubble Sort Lecture.ipynb new file mode 100644 index 00000000..74bcf3f1 --- /dev/null +++ b/Sorting and Searching/Bubble Sort Lecture.ipynb @@ -0,0 +1,89 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bubble Sort Lecture\n", + "\n", + "The bubble sort makes multiple passes through a list. It compares adjacent items and exchanges those that are out of order. Each pass through the list places the next largest value in its proper place. In essence, each item “bubbles” up to the location where it belongs.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "def bubble_sort(arr):\n", + " # Going in reverse order from last index to 1 of arr\n", + " # Because the last element of the arr should be sorted as bubbling up the array\n", + " # so skip them\n", + " for n in range(len(arr)-1, 0, -1):\n", + " # For k from 0 to n\n", + " print('This is the n:',n)\n", + " for k in range(n):\n", + " print('This is the k index check:',k)\n", + " # If current element is > next element\n", + " if arr[k] > arr[k+1]:\n", + " # Swap the two\n", + " temp = arr[k]\n", + " arr[k] = arr[k+1]\n", + " arr[k+1] = temp" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "This is the n: 5\nThis is the k index check: 0\nThis is the k index check: 1\nThis is the k index check: 2\nThis is the k index check: 3\nThis is the k index check: 4\nThis is the n: 4\nThis is the k index check: 0\nThis is the k index check: 1\nThis is the k index check: 2\nThis is the k index check: 3\nThis is the n: 3\nThis is the k index check: 0\nThis is the k index check: 1\nThis is the k index check: 2\nThis is the n: 2\nThis is the k index check: 0\nThis is the k index check: 1\nThis is the n: 1\nThis is the k index check: 0\n" + }, + { + "output_type": "execute_result", + "data": { + "text/plain": "[1, 2, 3, 4, 6, 8]" + }, + "metadata": {}, + "execution_count": 12 + } + ], + "source": [ + "arr = [3,6,1,8,4,2]\n", + "bubble_sort(arr)\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.2-final" + }, + "orig_nbformat": 2, + "kernelspec": { + "name": "python38264bite57af90baf4c458aa9d139163dfc9007", + "display_name": "Python 3.8.2 64-bit" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file diff --git a/Sorting and Searching/Hash Table Lecture.ipynb b/Sorting and Searching/Hash Table Lecture.ipynb new file mode 100644 index 00000000..d9f22b21 --- /dev/null +++ b/Sorting and Searching/Hash Table Lecture.ipynb @@ -0,0 +1,209 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Hash Table Lecture" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "class HashTable(object):\n", + "\n", + " def __init__(self, size):\n", + " self.size = size\n", + " self.slots = [None] * self.size\n", + " self.data = [None] * self.size\n", + "\n", + " # Putting key-data pair\n", + " # Key in slots[] and data in data[]\n", + " def put(self, key, data):\n", + " # key%size\n", + " hashvalue = self.hashfunction(key, len(self.slots))\n", + "\n", + " # If the slots[hasvalue] is None, \n", + " # Put key and data in corresponding slots[] and data[]\n", + " if self.slots[hashvalue] == None:\n", + " self.slots[hashvalue] = key\n", + " self.data[hashvalue] = data\n", + " else: \n", + " # If the slots[hashvalue] is not None but the key is the same,\n", + " # Overwrite the data\n", + " if self.slots[hashvalue] == key:\n", + " self.data[hashvalue] = data\n", + " else:\n", + " # If the slots[hashvalue] is not None and the key is not the same,\n", + " # rehashing is necessary\n", + " nextslot = self.rehash(hashvalue, len(self.slots))\n", + " # Keey rehashing until it finds next empty slot\n", + " while self.slots[nextslot] != None and self.slots[nextslot] != key:\n", + " nextslot = self.rehash(nextslot, len(self.slots))\n", + " \n", + " # If next slot is empty\n", + " # Put key and data in corresponding slots[] and data[]\n", + " if self.slots[nextslot] == None:\n", + " self.slots[nextslot] = key\n", + " self.data[nextslot] = data\n", + " # Else, meaning that the key is the same in slot[nextslot]\n", + " # overwrite the data\n", + " else: \n", + " self.data[nextslot] = data\n", + " \n", + " def hashfunction(self, key, size):\n", + " return key%size\n", + "\n", + " # When slots[hashvalue] is not None\n", + " def rehash(self, oldhash, size):\n", + " # Simply move along the slots and look for the next available slot\n", + " return (oldhash+1)%size\n", + "\n", + "\n", + " def get(self, key):\n", + " startslot = self.hashfunction(key, len(self.slots))\n", + " data = None\n", + " stop = False\n", + " found = False\n", + " position = startslot\n", + "\n", + " # While the self.slots[position] is not empty and didn't find, nor stopped, do:\n", + " while self.slots[position] != None and not found and not stop:\n", + " # If slots[position] == key\n", + " # matching key found\n", + " # retreive data (data = self.data[position])\n", + " if self.slots[position] == key:\n", + " found = True\n", + " data = self.data[position]\n", + " else:\n", + " # If slots[position] != key\n", + " # update the position by rehashing so it can go over to the next slot that key may be stored\n", + " position = self.rehash(position, len(self.slots))\n", + " # If iteration ended up doing the circle back,\n", + " # flag it to stop\n", + " if position == startslot:\n", + " stop = True\n", + " return data\n", + "\n", + " def __getitem__(self, key):\n", + " return self.get(key)\n", + " \n", + " def __setitem__(self,key, data):\n", + " return self.put(key, data)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "h = HashTable(5)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "h[1] = 'one'" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "h[2] = 'two'\n", + "h[3] = 'three'" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "'one'" + }, + "metadata": {}, + "execution_count": 5 + } + ], + "source": [ + "h[1]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "'two'" + }, + "metadata": {}, + "execution_count": 6 + } + ], + "source": [ + "h[2]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "'three'" + }, + "metadata": {}, + "execution_count": 7 + } + ], + "source": [ + "h[3]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.2-final" + }, + "orig_nbformat": 2, + "kernelspec": { + "name": "python38264bite57af90baf4c458aa9d139163dfc9007", + "display_name": "Python 3.8.2 64-bit" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file diff --git a/Sorting and Searching/Insertion Sort Lecture.ipynb b/Sorting and Searching/Insertion Sort Lecture.ipynb new file mode 100644 index 00000000..1b8bf5fa --- /dev/null +++ b/Sorting and Searching/Insertion Sort Lecture.ipynb @@ -0,0 +1,94 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Insertion Sort Lecture\n", + "\n", + "Insertion Sort builds the final sorted array one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def insertion_sort(arr):\n", + " # n-1 passes to sort n items\n", + " # from 1 to n-1\n", + " for i in range(1, len(arr)):\n", + " currentvalue = arr[i]\n", + " position = i\n", + "\n", + " # While index > 0 and arr[position-1] which is the last element of the sorted sublist > currentvalue\n", + " while position > 0 and arr[position-1] > currentvalue:\n", + "\n", + " # Move elements in sorted sublist to the right\n", + " arr[position] = arr[position-1]\n", + " # Decrement the index to mark index after shifting right because that's the place for the currentvalue\n", + " position = position-1\n", + " # Place currentvalue in the right spot of the sublist \n", + " arr[position] = currentvalue\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "arr = [4,7,3,1,67]" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "[1, 3, 4, 7, 67]" + }, + "metadata": {}, + "execution_count": 3 + } + ], + "source": [ + "insertion_sort(arr)\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.2-final" + }, + "orig_nbformat": 2, + "kernelspec": { + "name": "python38264bite57af90baf4c458aa9d139163dfc9007", + "display_name": "Python 3.8.2 64-bit" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file diff --git a/Sorting and Searching/Merge Sort Lecture.ipynb b/Sorting and Searching/Merge Sort Lecture.ipynb new file mode 100644 index 00000000..4784044a --- /dev/null +++ b/Sorting and Searching/Merge Sort Lecture.ipynb @@ -0,0 +1,126 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Merge Sort Lecture\n", + "Merge sort is a recursive algorithm that continually splits a list in half. If the list is empty or has one item, it is sorted by definition (the base case). If the list has more than one item, we split the list and recursively invoke a merge sort on both halves. Once the two halves are sorted, the fundamental operation, called a merge, is performed. Merging is the process of taking two smaller sorted lists and combining them together into a single, sorted, new list. \n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "def merge_sort(arr):\n", + " \n", + " if len(arr)>1:\n", + " mid = len(arr)//2\n", + " # Slicing left half\n", + " lefthalf = arr[:mid]\n", + " # Slicing right half\n", + " righthalf = arr[mid:]\n", + "\n", + " # Keep dividing lefthalf of the array of entire array\n", + " # Until its length is 2, where it has 1 lefthalf and 1 righthalf\n", + " merge_sort(lefthalf)\n", + " merge_sort(righthalf)\n", + "\n", + " i=0 # Left array idx tracker\n", + " j=0 # Right array idx tracker\n", + " k=0 # Final array idx tracker\n", + " print('Merging ', arr)\n", + "\n", + " # While i and j are both still in the range\n", + " # putting sorted smaller pieces of list back to arr \n", + " while i < len(lefthalf) and j < len(righthalf):\n", + " # If left element is less than right element\n", + " if lefthalf[i] < righthalf[j]:\n", + " # Set current arr[k] to the lefthalf value \n", + " arr[k]=lefthalf[i]\n", + " # Increment the left half idx\n", + " i=i+1\n", + " else:\n", + " arr[k]=righthalf[j]\n", + " j=j+1\n", + " # Increment final array idx\n", + " k=k+1\n", + "\n", + " # Making sure to check lefthalf and righthalf\n", + " while i < len(lefthalf):\n", + " arr[k]=lefthalf[i]\n", + " i=i+1\n", + " k=k+1\n", + "\n", + " while j < len(righthalf):\n", + " arr[k]=righthalf[j]\n", + " j=j+1\n", + " k=k+1\n", + " print('Merged to ', arr)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "Merging [11, 2]\nMerged to [2, 11]\nMerging [5, 4]\nMerged to [4, 5]\nMerging [11, 2, 5, 4]\nMerged to [2, 4, 5, 11]\nMerging [7, 56]\nMerged to [7, 56]\nMerging [12, 23]\nMerged to [12, 23]\nMerging [2, 12, 23]\nMerged to [2, 12, 23]\nMerging [7, 56, 2, 12, 23]\nMerged to [2, 7, 12, 23, 56]\nMerging [11, 2, 5, 4, 7, 56, 2, 12, 23]\nMerged to [2, 2, 4, 5, 7, 11, 12, 23, 56]\n" + }, + { + "output_type": "execute_result", + "data": { + "text/plain": "[2, 2, 4, 5, 7, 11, 12, 23, 56]" + }, + "metadata": {}, + "execution_count": 19 + } + ], + "source": [ + "arr = [11, 2, 5, 4, 7, 56, 2, 12, 23]\n", + "merge_sort(arr)\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.2-final" + }, + "orig_nbformat": 2, + "kernelspec": { + "name": "python38264bite57af90baf4c458aa9d139163dfc9007", + "display_name": "Python 3.8.2 64-bit" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file diff --git a/Sorting and Searching/Quick Sort Lecture.ipynb b/Sorting and Searching/Quick Sort Lecture.ipynb new file mode 100644 index 00000000..e38784c5 --- /dev/null +++ b/Sorting and Searching/Quick Sort Lecture.ipynb @@ -0,0 +1,76 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Quick Sort Lecture\n", + "\n", + "A quick sort first selects a value, which is called the pivot value. Although there are many different ways to choose the pivot value, we will simply use the first item in the list. The role of the pivot value is to assist with splitting the list. The actual position where the pivot value belongs in the final sorted list, commonly called the split point, will be used to divide the list for subsequent calls to the quick sort.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def quick_sort(arr):\n", + " quick_sort_help(arr, 0, len(arr)-1)\n", + "\n", + "\n", + "def quick_sort_help(arr, first, last):\n", + " if first < last:\n", + " splitpoint = partition(arr, first, last)\n", + "\n", + " quick_sort_help(arr, first, splitpoint-1)\n", + " quick_sort_help(arr, splitpoint+1, last)\n", + " \n", + "def partition(arr, first, last):\n", + " pivotvalue = arr[first]\n", + "\n", + " leftmark = first + 1\n", + " rightmark = last\n", + " \n", + " done = False\n", + "\n", + " while not done:\n", + " while leftmark <= rightmark and arr[leftmark] <= pivotvalue:\n", + " leftmark += 1\n", + " \n", + " while arr[rightmark] >= pivotvalue and rightmark >= leftmark:\n", + " rightmark -= 1\n", + "\n", + " if rightmark < leftmark:\n", + " done = True\n", + " else:\n", + " temp = arr[leftmark]\n", + " arr[leftmark] = arr[rightmark]\n", + " arr[rightmark] = temp\n", + "\n", + " temp = arr[first]\n", + " arr[first] = arr[rightmark]\n", + " arr[rightmark] = temp\n", + "\n", + " return rightmark" + ] + } + ], + "metadata": { + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": 3 + }, + "orig_nbformat": 2 + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file diff --git a/Sorting and Searching/Selection Sort Lecture.ipynb b/Sorting and Searching/Selection Sort Lecture.ipynb new file mode 100644 index 00000000..0d50b4a8 --- /dev/null +++ b/Sorting and Searching/Selection Sort Lecture.ipynb @@ -0,0 +1,107 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Selection Sort Lecture\n", + "The selection sort improves on the bubble sort by making only one exchange for every pass through the list. In order to do this, a selection sort looks for the largest value as it makes a pass and, after completing the pass, places it in the proper location. As with a bubble sort, after the first pass, the largest item is in the correct place. After the second pass, the next largest is in place. This process continues and requires n−1 passes to sort n items, since the final item must be in place after the (n−1) st pass.\n", + "\n", + "*** vs Bubble Sort ***\n", + "\n", + "Still multiple iteration, but selection sort just do 1 swapping per iteration\n", + "while bubble sort does multiple swapping per iteration" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "def selection_sort(arr):\n", + "\n", + " # Like bubble sort, going backward index of the given array until idx 1\n", + " # So it's filling the array from its end, by finding the maximums\n", + " for fillslot in range(len(arr)-1, 0, -1):\n", + " # Index of the max\n", + " positionOfMax = 0\n", + " # While iterating through 1 to fillslot,\n", + " for location in range(1, fillslot+1):\n", + " # If arr[location] > arr[positionOfMax]\n", + " if arr[location] > arr[positionOfMax]:\n", + " # Update the positionOfMax to that index\n", + " positionOfMax = location\n", + "\n", + " # After the iteration\n", + " # swap arr[fillslot] and arr[positionOfMax]\n", + " temp = arr[fillslot]\n", + " arr[fillslot] = arr[positionOfMax]\n", + " arr[positionOfMax] = temp" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "arr = [5,8,3,10,1]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "[1, 3, 5, 8, 10]" + }, + "metadata": {}, + "execution_count": 7 + } + ], + "source": [ + "selection_sort(arr)\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.2-final" + }, + "orig_nbformat": 2, + "kernelspec": { + "name": "python38264bite57af90baf4c458aa9d139163dfc9007", + "display_name": "Python 3.8.2 64-bit" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file diff --git a/Sorting and Searching/Sequential Search Lecture.ipynb b/Sorting and Searching/Sequential Search Lecture.ipynb new file mode 100644 index 00000000..5920fd54 --- /dev/null +++ b/Sorting and Searching/Sequential Search Lecture.ipynb @@ -0,0 +1,121 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Sequential Search Implementation Lecture " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def seq_search(arr, ele):\n", + " # Unordered list\n", + " # Have to go through entire arr\n", + " pos = 0\n", + " found = False\n", + "\n", + " while pos < len(arr) and not found:\n", + " if arr[pos] == ele:\n", + " found = True\n", + " else:\n", + " pos += 1\n", + " return found" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "True" + }, + "metadata": {}, + "execution_count": 3 + } + ], + "source": [ + "arr = [1,2,3,4,5]\n", + "seq_search(arr, 3)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "def ordered_seq_search(arr, ele):\n", + " \"\"\"\n", + " Input array must be sorted\n", + " \"\"\"\n", + " pos = 0\n", + " found = False\n", + " stopped = False\n", + "\n", + " while pos < len(arr) and not found and not stopped:\n", + " if arr[pos] == ele:\n", + " found = True\n", + " else:\n", + " if arr[pos] > ele:\n", + " stopped = True\n", + " else: pos += 1\n", + " return found\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "True" + }, + "metadata": {}, + "execution_count": 6 + } + ], + "source": [ + "arr = [1,2,3,4,5]\n", + "ordered_seq_search_(arr, 3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.2-final" + }, + "orig_nbformat": 2, + "kernelspec": { + "name": "python38264bite57af90baf4c458aa9d139163dfc9007", + "display_name": "Python 3.8.2 64-bit" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file diff --git a/Sorting and Searching/Shell Sort Lecture.ipynb b/Sorting and Searching/Shell Sort Lecture.ipynb new file mode 100644 index 00000000..63b7e5ff --- /dev/null +++ b/Sorting and Searching/Shell Sort Lecture.ipynb @@ -0,0 +1,110 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Shell Sort Lecture\n", + "\n", + "The shell sort improves on the insertion sort by breaking the original list into a number of smaller sublists. The unique way that these sublists are chosen in the key to the shell sort.\n", + "Instead of breaking the list into sublists of contiguous items, the shell sort uses an icrement \"i\" to create a sublist by choosing all items that are \"i\" items apart.\n", + "\n", + "*** vs Insertion Sort ***\n", + "\n", + "Slightly better than normal insertion sort because shell sort takes less swapping compared to the insertion sort" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "# Basic shell sort\n", + "\n", + "def shell_sort(arr):\n", + " sublistcount = len(arr) // 2\n", + " while sublistcount > 0:\n", + " for start in range(sublistcount):\n", + "\n", + " gap_insertion_sort(arr, start, sublistcount)\n", + "\n", + " sublistcount = sublistcount//2\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "def gap_insertion_sort(arr, start, gap):\n", + " for i in range(start+gap, len(arr), gap):\n", + "\n", + " currentvalue = arr[i]\n", + " position = i\n", + "\n", + " while position >= gap and arr[position-gap] > currentvalue:\n", + " arr[position] = arr[position-gap]\n", + " position = position - gap\n", + "\n", + " arr[position] = currentvalue" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "[21, 23, 45, 67, 90, 91]" + }, + "metadata": {}, + "execution_count": 16 + } + ], + "source": [ + "arr = [45,67,23,21,90, 91]\n", + "shell_sort(arr)\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.2-final" + }, + "orig_nbformat": 2, + "kernelspec": { + "name": "python38264bite57af90baf4c458aa9d139163dfc9007", + "display_name": "Python 3.8.2 64-bit" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file From 04e56fef0cfa256b5fe2a71cd7bd70ea8b85d25f Mon Sep 17 00:00:00 2001 From: Young Jin J Date: Tue, 9 Jun 2020 16:08:55 -0400 Subject: [PATCH 7/8] Graphs done --- Graphs/Adjacency List Lecture.ipynb | 209 ++++++++++++++++++ Graphs/DFS Lecture.ipynb | 172 ++++++++++++++ ...ement Breadth First Search Algorithm.ipynb | 18 +- ...plement Depth First Search Algorithm.ipynb | 20 +- .../Implement a Graph.ipynb | 46 +++- Graphs/Graph Lecture.ipynb | 0 ...plementation of Breadth First Search.ipynb | 2 +- 7 files changed, 460 insertions(+), 7 deletions(-) create mode 100644 Graphs/Adjacency List Lecture.ipynb create mode 100644 Graphs/DFS Lecture.ipynb create mode 100644 Graphs/Graph Lecture.ipynb diff --git a/Graphs/Adjacency List Lecture.ipynb b/Graphs/Adjacency List Lecture.ipynb new file mode 100644 index 00000000..14c3805a --- /dev/null +++ b/Graphs/Adjacency List Lecture.ipynb @@ -0,0 +1,209 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Adjacency List Lecture\n", + "\n", + "\n", + "Using dictionaries, it is easy to implement the adjacency list in Python. In our implementation of the Graph abstract data type we will create two classes: **Graph**, which holds the master list of vertices, and **Vertex**, which will represent each vertex in the graph.\n", + "\n", + "Each Vertex uses a dictionary to keep track of the vertices to which it is connected, and the weight of each edge. This dictionary is called **connectedTo**. The constructor simply initializes the id, which will typically be a string, and the **connectedTo** dictionary. The **addNeighbor** method is used add a connection from this vertex to another. The **getConnections** method returns all of the vertices in the adjacency list, as represented by the **connectedTo** instance variable. The **getWeight** method returns the weight of the edge from this vertex to the vertex passed as a parameter." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class Vertex:\n", + " def __init__(self,key):\n", + " self.id = key\n", + " self.connectedTo = {}\n", + " \n", + " def addNeighbor(self, nbr, weight = 0):\n", + " self.connectedTo[nbr] = weight\n", + "\n", + " def getConnections(self):\n", + " return self.connectedTo.keys()\n", + "\n", + " def getId(self):\n", + " return self.id\n", + " \n", + " def getWeight(self, nbr):\n", + " return self.connectedTo[nbr]\n", + "\n", + " def __str__(self):\n", + " return str(self.id) + ' connected to: ' + str([x.id for x in self.connectedTo])\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "class Graph:\n", + " def __init__(self):\n", + " self.vertList = {}\n", + " self.numVerticies = 0\n", + "\n", + " def addVertex(self, key):\n", + " self.numVerticies += 1\n", + " newVertex = Vertex(key)\n", + " self.vertList[key] = newVertex\n", + " return newVertex\n", + "\n", + " def getVertex(self,n):\n", + " if n in self.vertList:\n", + " return self.vertList[n]\n", + " else:\n", + " return None\n", + " \n", + " def addEdge(self, f, t, cost=0):\n", + " if f not in self.vertList:\n", + " nv = self.addVertex(f)\n", + "\n", + " if t not in self.vertList:\n", + " nv = self.addVertex(t)\n", + " \n", + " self.vertList[f].addNeighbor(self.vertList[t], cost)\n", + "\n", + " def getVerticies(self):\n", + " return self.vertList.keys()\n", + "\n", + " def __iter__(self):\n", + " return iter(self.vertList.values())\n", + "\n", + " def __contains__(self, n):\n", + " return n in self.vertList\n", + "\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "g = Graph()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "for i in range(6):\n", + " g.addVertex(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "{0: <__main__.Vertex at 0x7fbc9413fa90>,\n 1: <__main__.Vertex at 0x7fbc9413f790>,\n 2: <__main__.Vertex at 0x7fbc93e28580>,\n 3: <__main__.Vertex at 0x7fbc93e28280>,\n 4: <__main__.Vertex at 0x7fbc93e285e0>,\n 5: <__main__.Vertex at 0x7fbc93e28760>}" + }, + "metadata": {}, + "execution_count": 6 + } + ], + "source": [ + "g.vertList" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "g.addEdge(0,1,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "0connected to: [1]\ndict_keys([<__main__.Vertex object at 0x7fbc9413f790>])\n1connected to: []\ndict_keys([])\n2connected to: []\ndict_keys([])\n3connected to: []\ndict_keys([])\n4connected to: []\ndict_keys([])\n5connected to: []\ndict_keys([])\n" + } + ], + "source": [ + "for vertex in g:\n", + " print(vertex)\n", + " print(vertex.getConnections())" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "g.addEdge(0, 2, 3)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "0connected to: [1, 2]\ndict_keys([<__main__.Vertex object at 0x7fbc9413f790>, <__main__.Vertex object at 0x7fbc93e28580>])\n1connected to: []\ndict_keys([])\n2connected to: []\ndict_keys([])\n3connected to: []\ndict_keys([])\n4connected to: []\ndict_keys([])\n5connected to: []\ndict_keys([])\n" + } + ], + "source": [ + "for vertex in g:\n", + " print(vertex)\n", + " print(vertex.getConnections())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.2-final" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} \ No newline at end of file diff --git a/Graphs/DFS Lecture.ipynb b/Graphs/DFS Lecture.ipynb new file mode 100644 index 00000000..e9a5373e --- /dev/null +++ b/Graphs/DFS Lecture.ipynb @@ -0,0 +1,172 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Graph Lecture\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "from enum import Enum\n", + "\n", + "class State(Enum):\n", + " unvisited = 1\n", + " visited = 2\n", + " visiting = 3" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "from collections import OrderedDict\n", + "class Node:\n", + " def __init__(self, num):\n", + " self.num = num\n", + " self.visit_state = State.unvisited\n", + " self.adjacent = OrderedDict() # key = node, value = weight\n", + "\n", + " def __str__(self):\n", + " return str(self.num)\n", + "\n", + "\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "class Graph:\n", + " def __init__(self):\n", + " self.nodes = OrderedDict()\n", + " def add_node(self, num):\n", + " node = Node(num)\n", + " self.nodes[num] = node\n", + " return node\n", + " def add_edge(self, source, dest, weight=0):\n", + " if source not in self.nodes:\n", + " self.add_node(source)\n", + " if dest not in self.nodes:\n", + " self.add_node(dest)\n", + "\n", + " self.nodes[source].adjacent[self.nodes[dest]] = weight" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "g = Graph()\n", + "g.add_edge(0, 1, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "OrderedDict([(0, <__main__.Node at 0x7fb3896a7640>),\n (1, <__main__.Node at 0x7fb3896a7a00>)])" + }, + "metadata": {}, + "execution_count": 7 + } + ], + "source": [ + "g.nodes\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "g.add_edge(1, 2, 3)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": "OrderedDict([(0, <__main__.Node at 0x7fb3896a7640>),\n (1, <__main__.Node at 0x7fb3896a7a00>),\n (2, <__main__.Node at 0x7fb388a5b5e0>)])" + }, + "metadata": {}, + "execution_count": 9 + } + ], + "source": [ + "g.nodes" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "def dfs(graph, start):\n", + " visited = set()\n", + " stack = [start]\n", + "\n", + " while stack:\n", + " vertex = stack.pop()\n", + " if vertex not in visited:\n", + " visited.add(vertex)\n", + " stack.extend(graph[vertex] - visited)\n", + "\n", + " return visited\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.2-final" + }, + "orig_nbformat": 2, + "kernelspec": { + "name": "python38264bite57af90baf4c458aa9d139163dfc9007", + "display_name": "Python 3.8.2 64-bit" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file diff --git a/Graphs/Graph Interview Questions/Implement Breadth First Search Algorithm.ipynb b/Graphs/Graph Interview Questions/Implement Breadth First Search Algorithm.ipynb index 55bd824b..51a0afb5 100644 --- a/Graphs/Graph Interview Questions/Implement Breadth First Search Algorithm.ipynb +++ b/Graphs/Graph Interview Questions/Implement Breadth First Search Algorithm.ipynb @@ -30,6 +30,22 @@ " 'F': set(['C', 'E'])}" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def bfs(graph, start):\n", + " visited, queue = set(), [start]\n", + " while queue:\n", + " vertex = queue.pop(0)\n", + " if vertex not in visited:\n", + " visited.add(vertex)\n", + " queue.extend(graph[vertex] - visited)\n", + " return visited" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -59,4 +75,4 @@ }, "nbformat": 4, "nbformat_minor": 1 -} +} \ No newline at end of file diff --git a/Graphs/Graph Interview Questions/Implement Depth First Search Algorithm.ipynb b/Graphs/Graph Interview Questions/Implement Depth First Search Algorithm.ipynb index 94da601e..d3d976ab 100644 --- a/Graphs/Graph Interview Questions/Implement Depth First Search Algorithm.ipynb +++ b/Graphs/Graph Interview Questions/Implement Depth First Search Algorithm.ipynb @@ -30,6 +30,24 @@ " 'F': set(['C', 'E'])}" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def dfs(graph, start):\n", + " visited = set()\n", + " stack = [start]\n", + "\n", + " while stack:\n", + " vertex = stack.pop()\n", + " if vertex not in visited:\n", + " visited.add(vertex)\n", + " stack.extend(graph[vertex] - visited)\n", + " return visited\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -59,4 +77,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Graphs/Graph Interview Questions/Implement a Graph.ipynb b/Graphs/Graph Interview Questions/Implement a Graph.ipynb index b6362791..94591814 100644 --- a/Graphs/Graph Interview Questions/Implement a Graph.ipynb +++ b/Graphs/Graph Interview Questions/Implement a Graph.ipynb @@ -13,6 +13,44 @@ "You have to fully worked out implementations in the lectures, so make sure to refer to them!" ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "class Graph(object):\n", + " def __init__(self):\n", + " self.verticies = {}\n", + " self.vertextCount = 0\n", + "\n", + " def addVertex(self, key):\n", + " nv = Vertex(key)\n", + " self.verticies[key] = nv\n", + " self.vertextCount += 1\n", + " return nv\n", + "\n", + " def getVertex(self, n):\n", + " if n in self.verticies:\n", + " return self.verticies[n]\n", + "\n", + " def addEdge(self, f, t, weight = 0):\n", + " if f not in self.verticies:\n", + " nv = addVertex(f)\n", + " if t not in self.verticies:\n", + " nv = addVertex(t)\n", + "\n", + " self.verticies[f].addNeighbor(self.verticies[t], cost)\n", + "\n", + "\n", + " def getVerticies(self):\n", + " return self.verticies.keys()\n", + "\n", + " def __iter__(self):\n", + " return iter(self.verticies.values())\n", + " \n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -23,9 +61,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3.8.2 64-bit", "language": "python", - "name": "python2" + "name": "python38264bite57af90baf4c458aa9d139163dfc9007" }, "language_info": { "codemirror_mode": { @@ -37,9 +75,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.11" + "version": "3.8.2-final" } }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/Graphs/Graph Lecture.ipynb b/Graphs/Graph Lecture.ipynb new file mode 100644 index 00000000..e69de29b diff --git a/Graphs/Implementation of Breadth First Search.ipynb b/Graphs/Implementation of Breadth First Search.ipynb index 185b0183..3ba20370 100644 --- a/Graphs/Implementation of Breadth First Search.ipynb +++ b/Graphs/Implementation of Breadth First Search.ipynb @@ -173,4 +173,4 @@ }, "nbformat": 4, "nbformat_minor": 1 -} +} \ No newline at end of file From be6b2db84e97d9cbd2740227d156937964aa4866 Mon Sep 17 00:00:00 2001 From: Young Jin J Date: Fri, 19 Jun 2020 16:24:15 -0400 Subject: [PATCH 8/8] Added minor comments on sorting --- Sorting and Searching/Implementation of Merge Sort.ipynb | 4 +++- Sorting and Searching/Implementation of Quick Sort.ipynb | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Sorting and Searching/Implementation of Merge Sort.ipynb b/Sorting and Searching/Implementation of Merge Sort.ipynb index 0ad3b17d..24c65497 100644 --- a/Sorting and Searching/Implementation of Merge Sort.ipynb +++ b/Sorting and Searching/Implementation of Merge Sort.ipynb @@ -30,6 +30,8 @@ }, "outputs": [], "source": [ + "# Split into half and\n", + "# Swap each element to the correct manner\n", "def merge_sort(arr):\n", " \n", " if len(arr)>1:\n", @@ -114,4 +116,4 @@ }, "nbformat": 4, "nbformat_minor": 1 -} +} \ No newline at end of file diff --git a/Sorting and Searching/Implementation of Quick Sort.ipynb b/Sorting and Searching/Implementation of Quick Sort.ipynb index bf2e99c0..ca788230 100644 --- a/Sorting and Searching/Implementation of Quick Sort.ipynb +++ b/Sorting and Searching/Implementation of Quick Sort.ipynb @@ -30,6 +30,12 @@ }, "outputs": [], "source": [ + "# Pivot::\n", + "# Correct position in final, sorted array\n", + "# Items to the left are smaller\n", + "# Imtens to the right are larger\n", + "# Θ(n log(n))\n", + "\n", "def quick_sort(arr):\n", " \n", " quick_sort_help(arr,0,len(arr)-1)\n", @@ -127,4 +133,4 @@ }, "nbformat": 4, "nbformat_minor": 1 -} +} \ No newline at end of file