├── 01.Numerical Integration.ipynb ├── 02.Random Variable for Asset.ipynb ├── 03.Fibonacci.ipynb ├── 04.Missing Number Out of 10000.ipynb ├── 05.Increment Date.ipynb ├── 06.Exponentiating Matrix.ipynb ├── 07.Code Up Histogram Maker.ipynb ├── 08.f(x) = sin(x) divided by x.ipynb ├── 09.Grandma Plane Problem.ipynb ├── 10.Maximum Sum Subarray.ipynb ├── 11.Sorting Algorithms.ipynb ├── 12.Permute Integers from 1-100.ipynb ├── 13.Factorial Function.ipynb ├── 14.2D Binary Search.ipynb ├── 15.Check for Duplicates.ipynb ├── 16.Linear Interpolation.ipynb ├── 17.Submarine Puzzle.ipynb ├── 18.Make Robots Collide with the Same Code.ipynb ├── 19.Sum of Tree Nodes.ipynb ├── 20.Price Digital Call using Monte Carlo.ipynb ├── 21.Number Swap with O(1) Space.ipynb ├── 22.Unique Elements.ipynb ├── 23.Horner's Algorithm.ipynb ├── 24.Moving Average.ipynb ├── 25.Sorting Algorithms.ipynb ├── 26.Random Permutation.ipynb ├── 27.Maximum Sum Contiguous Subarray.ipynb ├── 28.Millenium Brainteaser.ipynb ├── 29.Umbrella Q.ipynb └── README.md /01.Numerical Integration.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 25, 6 | "id": "870fc77c", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "#Numerical Integration using the midpoint rule\n", 11 | "\n", 12 | "def func(x):\n", 13 | " return x**3\n", 14 | "\n", 15 | "def integrate(a,b,n):\n", 16 | " delta = (b-a) // n\n", 17 | " diff = delta // 2\n", 18 | " total = 0\n", 19 | " for i in range(a + diff,b+1,delta):\n", 20 | " print(i)\n", 21 | " total += func(i)\n", 22 | " \n", 23 | " return delta*total\n", 24 | " " 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 30, 30 | "id": "4819ab94", 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "1\n", 38 | "3\n", 39 | "5\n", 40 | "7\n" 41 | ] 42 | }, 43 | { 44 | "data": { 45 | "text/plain": [ 46 | "992" 47 | ] 48 | }, 49 | "execution_count": 30, 50 | "metadata": {}, 51 | "output_type": "execute_result" 52 | } 53 | ], 54 | "source": [ 55 | "integrate(0,8,4)" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": null, 61 | "id": "9385b385", 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [] 65 | } 66 | ], 67 | "metadata": { 68 | "kernelspec": { 69 | "display_name": "Python 3", 70 | "language": "python", 71 | "name": "python3" 72 | }, 73 | "language_info": { 74 | "codemirror_mode": { 75 | "name": "ipython", 76 | "version": 3 77 | }, 78 | "file_extension": ".py", 79 | "mimetype": "text/x-python", 80 | "name": "python", 81 | "nbconvert_exporter": "python", 82 | "pygments_lexer": "ipython3", 83 | "version": "3.8.8" 84 | } 85 | }, 86 | "nbformat": 4, 87 | "nbformat_minor": 5 88 | } 89 | -------------------------------------------------------------------------------- /02.Random Variable for Asset.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "id": "d05e80e7", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def simulate(p,v,u):\n", 11 | " i = 0\n", 12 | " while ( u > p[i]):\n", 13 | " u -= p[i]\n", 14 | " i += 1\n", 15 | " \n", 16 | " return v[i]" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "id": "204b8b26", 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [] 26 | } 27 | ], 28 | "metadata": { 29 | "kernelspec": { 30 | "display_name": "Python 3", 31 | "language": "python", 32 | "name": "python3" 33 | }, 34 | "language_info": { 35 | "codemirror_mode": { 36 | "name": "ipython", 37 | "version": 3 38 | }, 39 | "file_extension": ".py", 40 | "mimetype": "text/x-python", 41 | "name": "python", 42 | "nbconvert_exporter": "python", 43 | "pygments_lexer": "ipython3", 44 | "version": "3.8.8" 45 | } 46 | }, 47 | "nbformat": 4, 48 | "nbformat_minor": 5 49 | } 50 | -------------------------------------------------------------------------------- /03.Fibonacci.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 16, 6 | "id": "9b4f974c", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "#O(2^n)\n", 11 | "def recursive_fib(n):\n", 12 | " if n <= 2:\n", 13 | " return 1\n", 14 | " return recursive_fib(n-1) + recursive_fib(n-2)" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 17, 20 | "id": "3de70adf", 21 | "metadata": {}, 22 | "outputs": [ 23 | { 24 | "data": { 25 | "text/plain": [ 26 | "55" 27 | ] 28 | }, 29 | "execution_count": 17, 30 | "metadata": {}, 31 | "output_type": "execute_result" 32 | } 33 | ], 34 | "source": [ 35 | "recursive_fib(10)" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 18, 41 | "id": "d2cf7463", 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "#O(n)\n", 46 | "def memo_fib(n,cache):\n", 47 | " if n<= 1:\n", 48 | " return n \n", 49 | " if n in cache:\n", 50 | " return cache[n]\n", 51 | " \n", 52 | " cache[n] = memo_fib(n-1,cache) + memo_fib(n-2,cache)\n", 53 | " return cache[n]" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 19, 59 | "id": "e8c9528e", 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "data": { 64 | "text/plain": [ 65 | "55" 66 | ] 67 | }, 68 | "execution_count": 19, 69 | "metadata": {}, 70 | "output_type": "execute_result" 71 | } 72 | ], 73 | "source": [ 74 | "memo_fib(10,{})" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 20, 80 | "id": "32862843", 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "#O(1)\n", 85 | "def golden_fib(N):\n", 86 | " golden_ratio = (1 + 5 ** 0.5) / 2\n", 87 | " return int((golden_ratio ** N + 1) / 5 ** 0.5)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 21, 93 | "id": "3d518d0c", 94 | "metadata": {}, 95 | "outputs": [ 96 | { 97 | "data": { 98 | "text/plain": [ 99 | "55" 100 | ] 101 | }, 102 | "execution_count": 21, 103 | "metadata": {}, 104 | "output_type": "execute_result" 105 | } 106 | ], 107 | "source": [ 108 | "golden_fib(10)" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "id": "bf1fec11", 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [] 118 | } 119 | ], 120 | "metadata": { 121 | "kernelspec": { 122 | "display_name": "Python 3", 123 | "language": "python", 124 | "name": "python3" 125 | }, 126 | "language_info": { 127 | "codemirror_mode": { 128 | "name": "ipython", 129 | "version": 3 130 | }, 131 | "file_extension": ".py", 132 | "mimetype": "text/x-python", 133 | "name": "python", 134 | "nbconvert_exporter": "python", 135 | "pygments_lexer": "ipython3", 136 | "version": "3.8.8" 137 | } 138 | }, 139 | "nbformat": 4, 140 | "nbformat_minor": 5 141 | } 142 | -------------------------------------------------------------------------------- /04.Missing Number Out of 10000.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "55e26bb6", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def find(nums):\n", 11 | " grandTotal = (10000*10001)/2\n", 12 | " return grandTotal - sum(nums)" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 6, 18 | "id": "8d28c3f6", 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "name": "stdout", 23 | "output_type": "stream", 24 | "text": [ 25 | "3179\n" 26 | ] 27 | } 28 | ], 29 | "source": [ 30 | "import random\n", 31 | "numbers = []\n", 32 | "for i in range(1,10001):\n", 33 | " numbers.append(i)\n", 34 | "toRemove = random.randint(1,10000)\n", 35 | "print(toRemove)\n", 36 | "numbers.remove(toRemove)" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "id": "1b6e43e1", 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [] 46 | } 47 | ], 48 | "metadata": { 49 | "kernelspec": { 50 | "display_name": "Python 3", 51 | "language": "python", 52 | "name": "python3" 53 | }, 54 | "language_info": { 55 | "codemirror_mode": { 56 | "name": "ipython", 57 | "version": 3 58 | }, 59 | "file_extension": ".py", 60 | "mimetype": "text/x-python", 61 | "name": "python", 62 | "nbconvert_exporter": "python", 63 | "pygments_lexer": "ipython3", 64 | "version": "3.8.8" 65 | } 66 | }, 67 | "nbformat": 4, 68 | "nbformat_minor": 5 69 | } 70 | -------------------------------------------------------------------------------- /05.Increment Date.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 13, 6 | "id": "366dc046", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "2003-08-06 12:04:05\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "import datetime\n", 19 | "\n", 20 | "date = datetime.datetime(2003,8,1,12,4,5)\n", 21 | "for i in range(5): \n", 22 | " date += datetime.timedelta(days=1)\n", 23 | "print(date) " 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": null, 29 | "id": "40d4a684", 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [] 33 | } 34 | ], 35 | "metadata": { 36 | "kernelspec": { 37 | "display_name": "Python 3", 38 | "language": "python", 39 | "name": "python3" 40 | }, 41 | "language_info": { 42 | "codemirror_mode": { 43 | "name": "ipython", 44 | "version": 3 45 | }, 46 | "file_extension": ".py", 47 | "mimetype": "text/x-python", 48 | "name": "python", 49 | "nbconvert_exporter": "python", 50 | "pygments_lexer": "ipython3", 51 | "version": "3.8.8" 52 | } 53 | }, 54 | "nbformat": 4, 55 | "nbformat_minor": 5 56 | } 57 | -------------------------------------------------------------------------------- /06.Exponentiating Matrix.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "id": "601db36c", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "# Python3 program to find value of f(n) \n", 11 | "# where f(n) is defined as\n", 12 | "# F(n) = F(n-1) + F(n-2) + F(n-3), n >= 3\n", 13 | "# Base Cases :\n", 14 | "# F(0) = 0, F(1) = 1, F(2) = 1\n", 15 | " \n", 16 | "# A utility function to multiply two \n", 17 | "# matrices a[][] and b[][]. Multiplication \n", 18 | "# result is stored back in b[][]\n", 19 | "def multiply(a, b):\n", 20 | " \n", 21 | " # Creating an auxiliary matrix \n", 22 | " # to store elements of the\n", 23 | " # multiplication matrix\n", 24 | " mul = [[0 for x in range(3)]\n", 25 | " for y in range(3)];\n", 26 | " for i in range(3):\n", 27 | " for j in range(3):\n", 28 | " mul[i][j] = 0;\n", 29 | " for k in range(3):\n", 30 | " mul[i][j] += a[i][k] * b[k][j];\n", 31 | " \n", 32 | " # storing the multiplication\n", 33 | " # result in a[][]\n", 34 | " for i in range(3):\n", 35 | " for j in range(3):\n", 36 | " a[i][j] = mul[i][j]; # Updating our matrix\n", 37 | " return a;\n", 38 | " \n", 39 | "# Function to compute F raise \n", 40 | "# to power n-2.\n", 41 | "def power(F, n):\n", 42 | " \n", 43 | " M = [[1, 1, 1], [1, 0, 0], [0, 1, 0]];\n", 44 | " \n", 45 | " # Multiply it with initial values i.e \n", 46 | " # with F(0) = 0, F(1) = 1, F(2) = 1\n", 47 | " if (n == 1):\n", 48 | " return F[0][0] + F[0][1];\n", 49 | " \n", 50 | " power(F, int(n / 2));\n", 51 | " \n", 52 | " F = multiply(F, F);\n", 53 | " \n", 54 | " if (n % 2 != 0):\n", 55 | " F = multiply(F, M);\n", 56 | " \n", 57 | " # Multiply it with initial values i.e \n", 58 | " # with F(0) = 0, F(1) = 1, F(2) = 1\n", 59 | " return F[0][0] + F[0][1] " 60 | ] 61 | } 62 | ], 63 | "metadata": { 64 | "kernelspec": { 65 | "display_name": "Python 3", 66 | "language": "python", 67 | "name": "python3" 68 | }, 69 | "language_info": { 70 | "codemirror_mode": { 71 | "name": "ipython", 72 | "version": 3 73 | }, 74 | "file_extension": ".py", 75 | "mimetype": "text/x-python", 76 | "name": "python", 77 | "nbconvert_exporter": "python", 78 | "pygments_lexer": "ipython3", 79 | "version": "3.8.8" 80 | } 81 | }, 82 | "nbformat": 4, 83 | "nbformat_minor": 5 84 | } 85 | -------------------------------------------------------------------------------- /07.Code Up Histogram Maker.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "7592eb87", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def histogram(a, bins = 1000, density=False):\n", 11 | " start_time = time.perf_counter()\n", 12 | " min_data = np.min(a)\n", 13 | " max_data = np.max(a)\n", 14 | " dx = (max_data - min_data) / bins\n", 15 | " print(time.perf_counter() - start_time, 'to calc min/max')\n", 16 | " \n", 17 | " x = np.zeros(bins)\n", 18 | " y = np.zeros(bins+1)\n", 19 | " print(time.perf_counter() - start_time, 'to create x, y')\n", 20 | " \n", 21 | " for i in range(bins):\n", 22 | " x[i] = i*dx + min_data\n", 23 | " print(time.perf_counter() - start_time, 'to calc bin borders')\n", 24 | "\n", 25 | " a_to_bins = ((a - min_data) / dx).astype(int)\n", 26 | " print(time.perf_counter() - start_time, 'to calc bins')\n", 27 | " for bin in a_to_bins:\n", 28 | " y[bin] += 1\n", 29 | " print(time.perf_counter() - start_time, 'to fill bins')\n", 30 | " \n", 31 | " y[bins-2] += y[bins-1]\n", 32 | " y = y[:bins]\n", 33 | " \n", 34 | " if density == True:\n", 35 | " inte = sum((x[1]-x[0])*y)\n", 36 | " y /= inte\n", 37 | "\n", 38 | " print(time.perf_counter() - start_time, 'before draw')\n", 39 | " plt.bar(x, y, width=dx)\n", 40 | " print(time.perf_counter() - start_time, 'after draw')\n", 41 | " return np.column_stack((x, y))" 42 | ] 43 | } 44 | ], 45 | "metadata": { 46 | "kernelspec": { 47 | "display_name": "Python 3", 48 | "language": "python", 49 | "name": "python3" 50 | }, 51 | "language_info": { 52 | "codemirror_mode": { 53 | "name": "ipython", 54 | "version": 3 55 | }, 56 | "file_extension": ".py", 57 | "mimetype": "text/x-python", 58 | "name": "python", 59 | "nbconvert_exporter": "python", 60 | "pygments_lexer": "ipython3", 61 | "version": "3.8.8" 62 | } 63 | }, 64 | "nbformat": 4, 65 | "nbformat_minor": 5 66 | } 67 | -------------------------------------------------------------------------------- /08.f(x) = sin(x) divided by x.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "id": "bc136b77", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import math\n", 11 | "\n", 12 | "def f(x):\n", 13 | " return math.sin(x)/x" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 3, 19 | "id": "87347396", 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "data": { 24 | "text/plain": [ 25 | "-0.05440211108893698" 26 | ] 27 | }, 28 | "execution_count": 3, 29 | "metadata": {}, 30 | "output_type": "execute_result" 31 | } 32 | ], 33 | "source": [ 34 | "f(10)" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 6, 40 | "id": "9fa0529f", 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "#Computers use series to approximate sin(x)" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": null, 50 | "id": "c606dc5a", 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [] 54 | } 55 | ], 56 | "metadata": { 57 | "kernelspec": { 58 | "display_name": "Python 3", 59 | "language": "python", 60 | "name": "python3" 61 | }, 62 | "language_info": { 63 | "codemirror_mode": { 64 | "name": "ipython", 65 | "version": 3 66 | }, 67 | "file_extension": ".py", 68 | "mimetype": "text/x-python", 69 | "name": "python", 70 | "nbconvert_exporter": "python", 71 | "pygments_lexer": "ipython3", 72 | "version": "3.8.8" 73 | } 74 | }, 75 | "nbformat": 4, 76 | "nbformat_minor": 5 77 | } 78 | -------------------------------------------------------------------------------- /09.Grandma Plane Problem.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "dd86e5a1", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "#Code to simulate the infamous grandma problem\n", 11 | "#For simplicity people are labelled from 1-100 in the queue (0-99 index wise) with their seat number the same as their label\n", 12 | "#Seats are represented as a boolean array - False if empty and True if taken\n", 13 | "#Grandma is labelled 1 (0 index)\n", 14 | "#If the 100th person (99th indexed) finds out his seat is empty then we return True and False otherwise\n", 15 | "\n", 16 | "import random\n", 17 | "def simulate():\n", 18 | " seats = [False for i in range(100)]\n", 19 | " grandma_seat = random.randint(0,99)\n", 20 | " if grandma_seat == 0:\n", 21 | " return True\n", 22 | " seats[grandma_seat] = True\n", 23 | " for i in range(1,100):\n", 24 | " if i == 99:\n", 25 | " if seats[99] == False:\n", 26 | " return True\n", 27 | " else:\n", 28 | " return False\n", 29 | " if seats[i] == False:\n", 30 | " seats[i] = True\n", 31 | " else:\n", 32 | " newSeat = random.randint(0,99)\n", 33 | " while (seats[newSeat] == True):\n", 34 | " newSeat = random.randint(0,99)\n", 35 | " seats[newSeat] = True\n", 36 | " " 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 29, 42 | "id": "b3128680", 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "0.4977\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "yesCount = 0\n", 55 | "noCount = 0\n", 56 | "for i in range(10000):\n", 57 | " if simulate():\n", 58 | " yesCount += 1\n", 59 | " else:\n", 60 | " noCount +=1 \n", 61 | "\n", 62 | "print(round(yesCount/(noCount + yesCount),4))" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "id": "16e0c3e0", 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "id": "c4d69ae0", 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [] 80 | } 81 | ], 82 | "metadata": { 83 | "kernelspec": { 84 | "display_name": "Python 3", 85 | "language": "python", 86 | "name": "python3" 87 | }, 88 | "language_info": { 89 | "codemirror_mode": { 90 | "name": "ipython", 91 | "version": 3 92 | }, 93 | "file_extension": ".py", 94 | "mimetype": "text/x-python", 95 | "name": "python", 96 | "nbconvert_exporter": "python", 97 | "pygments_lexer": "ipython3", 98 | "version": "3.8.8" 99 | } 100 | }, 101 | "nbformat": 4, 102 | "nbformat_minor": 5 103 | } 104 | -------------------------------------------------------------------------------- /10.Maximum Sum Subarray.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 7, 6 | "id": "32640400", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def maxSubArray(nums):\n", 11 | " #Kadane's Algorithm\n", 12 | " maxSub = nums[0]\n", 13 | " currSum = 0\n", 14 | " \n", 15 | " for n in nums:\n", 16 | " currSum = max(currSum + n, n)\n", 17 | " maxSub = max(currSum,maxSub)\n", 18 | " \n", 19 | " return maxSub" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 8, 25 | "id": "c4f5aec5", 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "23" 32 | ] 33 | }, 34 | "execution_count": 8, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "maxSubArray([5,4,-1,7,8])" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": null, 46 | "id": "fa709f2e", 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [] 50 | } 51 | ], 52 | "metadata": { 53 | "kernelspec": { 54 | "display_name": "Python 3", 55 | "language": "python", 56 | "name": "python3" 57 | }, 58 | "language_info": { 59 | "codemirror_mode": { 60 | "name": "ipython", 61 | "version": 3 62 | }, 63 | "file_extension": ".py", 64 | "mimetype": "text/x-python", 65 | "name": "python", 66 | "nbconvert_exporter": "python", 67 | "pygments_lexer": "ipython3", 68 | "version": "3.8.8" 69 | } 70 | }, 71 | "nbformat": 4, 72 | "nbformat_minor": 5 73 | } 74 | -------------------------------------------------------------------------------- /11.Sorting Algorithms.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 7, 6 | "id": "e378aae9", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def mergeSort(arr,s,e):\n", 11 | " if (e - s + 1 <=1):\n", 12 | " return arr\n", 13 | " \n", 14 | " m = (s + e) // 2\n", 15 | " mergeSort(arr,s,m)\n", 16 | " mergeSort(arr,m+1,e)\n", 17 | " merge(arr,s,m,e)\n", 18 | " \n", 19 | " return arr\n", 20 | "\n", 21 | "def merge(arr,s,m,e):\n", 22 | " L = arr[s:m+1]\n", 23 | " R = arr[m+1:e+1]\n", 24 | " \n", 25 | " i,j,k = 0,0,s\n", 26 | " \n", 27 | " while i < len(L) and j < len(R):\n", 28 | " if L[i] <= R[j]:\n", 29 | " arr[k] = L[i]\n", 30 | " k += 1\n", 31 | " i += 1\n", 32 | " else:\n", 33 | " arr[k] = R[j]\n", 34 | " k += 1\n", 35 | " j += 1\n", 36 | " \n", 37 | " while i < len(L):\n", 38 | " arr[k] = L[i]\n", 39 | " k += 1\n", 40 | " i += 1\n", 41 | " while j < len(R):\n", 42 | " arr[k] = R[j]\n", 43 | " k += 1\n", 44 | " j += 1\n" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 8, 50 | "id": "27b8fd25", 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "nums = [5,1,1,2,0,0]" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 9, 60 | "id": "6b25c5d2", 61 | "metadata": {}, 62 | "outputs": [ 63 | { 64 | "data": { 65 | "text/plain": [ 66 | "[0, 0, 1, 1, 2, 5]" 67 | ] 68 | }, 69 | "execution_count": 9, 70 | "metadata": {}, 71 | "output_type": "execute_result" 72 | } 73 | ], 74 | "source": [ 75 | "mergeSort(nums,0,len(nums))" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "id": "57c1526c", 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [] 85 | } 86 | ], 87 | "metadata": { 88 | "kernelspec": { 89 | "display_name": "Python 3", 90 | "language": "python", 91 | "name": "python3" 92 | }, 93 | "language_info": { 94 | "codemirror_mode": { 95 | "name": "ipython", 96 | "version": 3 97 | }, 98 | "file_extension": ".py", 99 | "mimetype": "text/x-python", 100 | "name": "python", 101 | "nbconvert_exporter": "python", 102 | "pygments_lexer": "ipython3", 103 | "version": "3.8.8" 104 | } 105 | }, 106 | "nbformat": 4, 107 | "nbformat_minor": 5 108 | } 109 | -------------------------------------------------------------------------------- /12.Permute Integers from 1-100.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "id": "7e3b2c3d", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import random\n", 11 | "\n", 12 | "def permute():\n", 13 | " numList = [0 for i in range(100)]\n", 14 | " for i in range(100):\n", 15 | " number = random.randint(1,100)\n", 16 | " while (number in numList):\n", 17 | " number = random.randint(1,100)\n", 18 | " numList[i] = number\n", 19 | " \n", 20 | " return numList" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 4, 26 | "id": "2847099d", 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "data": { 31 | "text/plain": [ 32 | "[22,\n", 33 | " 98,\n", 34 | " 79,\n", 35 | " 28,\n", 36 | " 50,\n", 37 | " 11,\n", 38 | " 12,\n", 39 | " 14,\n", 40 | " 91,\n", 41 | " 31,\n", 42 | " 95,\n", 43 | " 59,\n", 44 | " 51,\n", 45 | " 46,\n", 46 | " 8,\n", 47 | " 55,\n", 48 | " 92,\n", 49 | " 26,\n", 50 | " 20,\n", 51 | " 100,\n", 52 | " 47,\n", 53 | " 71,\n", 54 | " 70,\n", 55 | " 81,\n", 56 | " 84,\n", 57 | " 39,\n", 58 | " 56,\n", 59 | " 94,\n", 60 | " 44,\n", 61 | " 36,\n", 62 | " 53,\n", 63 | " 34,\n", 64 | " 69,\n", 65 | " 4,\n", 66 | " 45,\n", 67 | " 42,\n", 68 | " 41,\n", 69 | " 76,\n", 70 | " 87,\n", 71 | " 86,\n", 72 | " 13,\n", 73 | " 65,\n", 74 | " 32,\n", 75 | " 37,\n", 76 | " 43,\n", 77 | " 57,\n", 78 | " 18,\n", 79 | " 72,\n", 80 | " 23,\n", 81 | " 96,\n", 82 | " 29,\n", 83 | " 68,\n", 84 | " 33,\n", 85 | " 7,\n", 86 | " 67,\n", 87 | " 61,\n", 88 | " 85,\n", 89 | " 15,\n", 90 | " 30,\n", 91 | " 24,\n", 92 | " 52,\n", 93 | " 64,\n", 94 | " 63,\n", 95 | " 89,\n", 96 | " 19,\n", 97 | " 82,\n", 98 | " 80,\n", 99 | " 35,\n", 100 | " 6,\n", 101 | " 66,\n", 102 | " 2,\n", 103 | " 48,\n", 104 | " 62,\n", 105 | " 97,\n", 106 | " 54,\n", 107 | " 88,\n", 108 | " 75,\n", 109 | " 77,\n", 110 | " 40,\n", 111 | " 73,\n", 112 | " 78,\n", 113 | " 74,\n", 114 | " 25,\n", 115 | " 3,\n", 116 | " 16,\n", 117 | " 49,\n", 118 | " 1,\n", 119 | " 99,\n", 120 | " 5,\n", 121 | " 90,\n", 122 | " 58,\n", 123 | " 83,\n", 124 | " 60,\n", 125 | " 10,\n", 126 | " 38,\n", 127 | " 27,\n", 128 | " 21,\n", 129 | " 9,\n", 130 | " 17,\n", 131 | " 93]" 132 | ] 133 | }, 134 | "execution_count": 4, 135 | "metadata": {}, 136 | "output_type": "execute_result" 137 | } 138 | ], 139 | "source": [ 140 | "permute()" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "id": "c6b7c632", 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [] 150 | } 151 | ], 152 | "metadata": { 153 | "kernelspec": { 154 | "display_name": "Python 3", 155 | "language": "python", 156 | "name": "python3" 157 | }, 158 | "language_info": { 159 | "codemirror_mode": { 160 | "name": "ipython", 161 | "version": 3 162 | }, 163 | "file_extension": ".py", 164 | "mimetype": "text/x-python", 165 | "name": "python", 166 | "nbconvert_exporter": "python", 167 | "pygments_lexer": "ipython3", 168 | "version": "3.8.8" 169 | } 170 | }, 171 | "nbformat": 4, 172 | "nbformat_minor": 5 173 | } 174 | -------------------------------------------------------------------------------- /13.Factorial Function.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "a709d538", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def factorial(n):\n", 11 | " res = 1\n", 12 | " for i in range(1,n+1):\n", 13 | " res *= i \n", 14 | " \n", 15 | " return res" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 6, 21 | "id": "6fa6aef3", 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "def factorial_recursive(n):\n", 26 | " if n == 1:\n", 27 | " return 1\n", 28 | " return n * factorial_recursive(n-1)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": null, 34 | "id": "611e99e0", 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [] 38 | } 39 | ], 40 | "metadata": { 41 | "kernelspec": { 42 | "display_name": "Python 3", 43 | "language": "python", 44 | "name": "python3" 45 | }, 46 | "language_info": { 47 | "codemirror_mode": { 48 | "name": "ipython", 49 | "version": 3 50 | }, 51 | "file_extension": ".py", 52 | "mimetype": "text/x-python", 53 | "name": "python", 54 | "nbconvert_exporter": "python", 55 | "pygments_lexer": "ipython3", 56 | "version": "3.8.8" 57 | } 58 | }, 59 | "nbformat": 4, 60 | "nbformat_minor": 5 61 | } 62 | -------------------------------------------------------------------------------- /14.2D Binary Search.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "6e35e089", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], \n", 11 | "target = 3" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 3, 17 | "id": "352f339a", 18 | "metadata": {}, 19 | "outputs": [], 20 | "source": [ 21 | "def grid_binary_search(grid,target):\n", 22 | " # finding row\n", 23 | " T, B = 0, len(grid) - 1\n", 24 | " while ( T<= B):\n", 25 | " row = (T+B) // 2\n", 26 | " if target > grid[row][-1]:\n", 27 | " T = row + 1\n", 28 | " if target < grid[row][0]:\n", 29 | " B = row - 1\n", 30 | " else:\n", 31 | " break\n", 32 | " \n", 33 | " #finding column\n", 34 | " L,R = 0, len(grid[0]) - 1\n", 35 | " while (L<=R):\n", 36 | " col = (L+R) // 2\n", 37 | " if target > grid[row][col]:\n", 38 | " L = col + 1\n", 39 | " elif target < grid[row][col]:\n", 40 | " R = col - 1\n", 41 | " else:\n", 42 | " return True\n", 43 | " return False" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 4, 49 | "id": "a35273c0", 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "ename": "TypeError", 54 | "evalue": "'>' not supported between instances of 'int' and 'list'", 55 | "output_type": "error", 56 | "traceback": [ 57 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 58 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 59 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mgrid_binary_search\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmatrix\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mtarget\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 60 | "\u001b[1;32m\u001b[0m in \u001b[0;36mgrid_binary_search\u001b[1;34m(grid, target)\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[1;33m(\u001b[0m \u001b[0mT\u001b[0m\u001b[1;33m<=\u001b[0m \u001b[0mB\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mrow\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mT\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mB\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m//\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[1;32mif\u001b[0m \u001b[0mtarget\u001b[0m \u001b[1;33m>\u001b[0m \u001b[0mgrid\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mrow\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 7\u001b[0m \u001b[0mT\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrow\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mtarget\u001b[0m \u001b[1;33m<\u001b[0m \u001b[0mgrid\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mrow\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 61 | "\u001b[1;31mTypeError\u001b[0m: '>' not supported between instances of 'int' and 'list'" 62 | ] 63 | } 64 | ], 65 | "source": [ 66 | "grid_binary_search(matrix,target)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "id": "73c3b7b3", 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [] 76 | } 77 | ], 78 | "metadata": { 79 | "kernelspec": { 80 | "display_name": "Python 3", 81 | "language": "python", 82 | "name": "python3" 83 | }, 84 | "language_info": { 85 | "codemirror_mode": { 86 | "name": "ipython", 87 | "version": 3 88 | }, 89 | "file_extension": ".py", 90 | "mimetype": "text/x-python", 91 | "name": "python", 92 | "nbconvert_exporter": "python", 93 | "pygments_lexer": "ipython3", 94 | "version": "3.8.8" 95 | } 96 | }, 97 | "nbformat": 4, 98 | "nbformat_minor": 5 99 | } 100 | -------------------------------------------------------------------------------- /15.Check for Duplicates.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 37, 6 | "id": "ee095d1e", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def no_duplicates(nums):\n", 11 | " hashset = {*()}\n", 12 | " for n in nums:\n", 13 | " if (n in hashset):\n", 14 | " return False\n", 15 | " else:\n", 16 | " hashset.add(n)\n", 17 | " \n", 18 | " return True" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 38, 24 | "id": "4180162d", 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "nums = [1,3,2,4,5]" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 39, 34 | "id": "32feafca", 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "True" 41 | ] 42 | }, 43 | "execution_count": 39, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "no_duplicates(nums)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "id": "f44e5d71", 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [] 59 | } 60 | ], 61 | "metadata": { 62 | "kernelspec": { 63 | "display_name": "Python 3", 64 | "language": "python", 65 | "name": "python3" 66 | }, 67 | "language_info": { 68 | "codemirror_mode": { 69 | "name": "ipython", 70 | "version": 3 71 | }, 72 | "file_extension": ".py", 73 | "mimetype": "text/x-python", 74 | "name": "python", 75 | "nbconvert_exporter": "python", 76 | "pygments_lexer": "ipython3", 77 | "version": "3.8.8" 78 | } 79 | }, 80 | "nbformat": 4, 81 | "nbformat_minor": 5 82 | } 83 | -------------------------------------------------------------------------------- /16.Linear Interpolation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "id": "8dab2fd6", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def interpolate(a,b,x):\n", 11 | " x1, y1 = a\n", 12 | " x2, y2 = b\n", 13 | " gradient = (y2-y1) / (x2-x1)\n", 14 | " y = y1 + gradient*(x - x1)\n", 15 | " \n", 16 | " return y\n", 17 | " " 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 6, 23 | "id": "8653827d", 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "data": { 28 | "text/plain": [ 29 | "1004.2" 30 | ] 31 | }, 32 | "execution_count": 6, 33 | "metadata": {}, 34 | "output_type": "execute_result" 35 | } 36 | ], 37 | "source": [ 38 | "a = (250,1003)\n", 39 | "b = (300,1005)\n", 40 | "interpolate(a,b,280)" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": null, 46 | "id": "1b4d9ed6", 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [] 50 | } 51 | ], 52 | "metadata": { 53 | "kernelspec": { 54 | "display_name": "Python 3", 55 | "language": "python", 56 | "name": "python3" 57 | }, 58 | "language_info": { 59 | "codemirror_mode": { 60 | "name": "ipython", 61 | "version": 3 62 | }, 63 | "file_extension": ".py", 64 | "mimetype": "text/x-python", 65 | "name": "python", 66 | "nbconvert_exporter": "python", 67 | "pygments_lexer": "ipython3", 68 | "version": "3.8.8" 69 | } 70 | }, 71 | "nbformat": 4, 72 | "nbformat_minor": 5 73 | } 74 | -------------------------------------------------------------------------------- /17.Submarine Puzzle.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "fe1cb8f7", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "#Try all possible pairs of (a,b) it will eventually hit the submarine" 11 | ] 12 | } 13 | ], 14 | "metadata": { 15 | "kernelspec": { 16 | "display_name": "Python 3", 17 | "language": "python", 18 | "name": "python3" 19 | }, 20 | "language_info": { 21 | "codemirror_mode": { 22 | "name": "ipython", 23 | "version": 3 24 | }, 25 | "file_extension": ".py", 26 | "mimetype": "text/x-python", 27 | "name": "python", 28 | "nbconvert_exporter": "python", 29 | "pygments_lexer": "ipython3", 30 | "version": "3.8.8" 31 | } 32 | }, 33 | "nbformat": 4, 34 | "nbformat_minor": 5 35 | } 36 | -------------------------------------------------------------------------------- /18.Make Robots Collide with the Same Code.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 29, 6 | "id": "67c73e4d", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def robotsMet(a,b):\n", 11 | " return a == b\n", 12 | "\n", 13 | "def robotMeet():\n", 14 | " flag = False\n", 15 | " reachedParachute = False\n", 16 | " \n", 17 | " while not (robotsMet):\n", 18 | " if (reachedParachute):\n", 19 | " moveLeft()\n", 20 | " moveLeft()\n", 21 | " else:\n", 22 | " moveLeft()\n", 23 | " \n", 24 | " if (location == target_parachute):\n", 25 | " reachedParachute = True" 26 | ] 27 | } 28 | ], 29 | "metadata": { 30 | "kernelspec": { 31 | "display_name": "Python 3", 32 | "language": "python", 33 | "name": "python3" 34 | }, 35 | "language_info": { 36 | "codemirror_mode": { 37 | "name": "ipython", 38 | "version": 3 39 | }, 40 | "file_extension": ".py", 41 | "mimetype": "text/x-python", 42 | "name": "python", 43 | "nbconvert_exporter": "python", 44 | "pygments_lexer": "ipython3", 45 | "version": "3.8.8" 46 | } 47 | }, 48 | "nbformat": 4, 49 | "nbformat_minor": 5 50 | } 51 | -------------------------------------------------------------------------------- /19.Sum of Tree Nodes.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "id": "2d8a1588", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def inorderTraversal(root):\n", 11 | " total = 0\n", 12 | "\n", 13 | " def inorderDFS(root):\n", 14 | " if not root:\n", 15 | " return \n", 16 | " inorderDFS(root.left)\n", 17 | " total += root.val\n", 18 | " inorderDFS(root.right)\n", 19 | "\n", 20 | " inorderDFS(root)\n", 21 | " return total" 22 | ] 23 | } 24 | ], 25 | "metadata": { 26 | "kernelspec": { 27 | "display_name": "Python 3", 28 | "language": "python", 29 | "name": "python3" 30 | }, 31 | "language_info": { 32 | "codemirror_mode": { 33 | "name": "ipython", 34 | "version": 3 35 | }, 36 | "file_extension": ".py", 37 | "mimetype": "text/x-python", 38 | "name": "python", 39 | "nbconvert_exporter": "python", 40 | "pygments_lexer": "ipython3", 41 | "version": "3.8.8" 42 | } 43 | }, 44 | "nbformat": 4, 45 | "nbformat_minor": 5 46 | } 47 | -------------------------------------------------------------------------------- /20.Price Digital Call using Monte Carlo.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 16, 6 | "id": "6610a1c5", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import datetime\n", 11 | "import numpy as np" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 17, 17 | "id": "1116fb4d", 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "name": "stdout", 22 | "output_type": "stream", 23 | "text": [ 24 | "0.1643835616438356\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "# initial derivative parameters\n", 30 | "S = 101.15 #stock price\n", 31 | "K = 98.01 #strike price\n", 32 | "vol = 0.0991 #volatility (%)\n", 33 | "r = 0.01 #risk-free rate (%)\n", 34 | "N = 10 #number of time steps\n", 35 | "M = 1000 #number of simulations\n", 36 | "\n", 37 | "market_value = 3.86 #market price of option\n", 38 | "T = ((datetime.date(2022,3,17)-datetime.date(2022,1,17)).days+1)/365 #time in years\n", 39 | "print(T)" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 18, 45 | "id": "b380ee4a", 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "name": "stdout", 50 | "output_type": "stream", 51 | "text": [ 52 | "Call value is $3.55 with SE +/- 0.1\n" 53 | ] 54 | } 55 | ], 56 | "source": [ 57 | "#precompute constants\n", 58 | "dt = T/N\n", 59 | "nudt = (r - 0.5*vol**2)*dt\n", 60 | "volsdt = vol*np.sqrt(dt)\n", 61 | "lnS = np.log(S)\n", 62 | "\n", 63 | "# Monte Carlo Method\n", 64 | "Z = np.random.normal(size=(N, M))\n", 65 | "delta_lnSt = nudt + volsdt*Z\n", 66 | "lnSt = lnS + np.cumsum(delta_lnSt, axis=0)\n", 67 | "lnSt = np.concatenate( (np.full(shape=(1, M), fill_value=lnS), lnSt ) )\n", 68 | "\n", 69 | "# Compute Expectation and SE\n", 70 | "ST = np.exp(lnSt)\n", 71 | "CT = np.maximum(0, ST - K)\n", 72 | "C0 = np.exp(-r*T)*np.sum(CT[-1])/M\n", 73 | "\n", 74 | "sigma = np.sqrt( np.sum( (CT[-1] - C0)**2) / (M-1) )\n", 75 | "SE = sigma/np.sqrt(M)\n", 76 | "\n", 77 | "print(\"Call value is ${0} with SE +/- {1}\".format(np.round(C0,2),np.round(SE,2)))" 78 | ] 79 | } 80 | ], 81 | "metadata": { 82 | "kernelspec": { 83 | "display_name": "Python 3", 84 | "language": "python", 85 | "name": "python3" 86 | }, 87 | "language_info": { 88 | "codemirror_mode": { 89 | "name": "ipython", 90 | "version": 3 91 | }, 92 | "file_extension": ".py", 93 | "mimetype": "text/x-python", 94 | "name": "python", 95 | "nbconvert_exporter": "python", 96 | "pygments_lexer": "ipython3", 97 | "version": "3.8.8" 98 | } 99 | }, 100 | "nbformat": 4, 101 | "nbformat_minor": 5 102 | } 103 | -------------------------------------------------------------------------------- /21.Number Swap with O(1) Space.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "id": "1d854814", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def numSwap(i,j):\n", 11 | " i = i + j\n", 12 | " j = i - j\n", 13 | " i = i - j\n", 14 | " return (i,j)" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 6, 20 | "id": "415e28c4", 21 | "metadata": {}, 22 | "outputs": [ 23 | { 24 | "data": { 25 | "text/plain": [ 26 | "(4, 11)" 27 | ] 28 | }, 29 | "execution_count": 6, 30 | "metadata": {}, 31 | "output_type": "execute_result" 32 | } 33 | ], 34 | "source": [ 35 | "numSwap(11,4)" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "id": "cc4c147e", 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [] 45 | } 46 | ], 47 | "metadata": { 48 | "kernelspec": { 49 | "display_name": "Python 3", 50 | "language": "python", 51 | "name": "python3" 52 | }, 53 | "language_info": { 54 | "codemirror_mode": { 55 | "name": "ipython", 56 | "version": 3 57 | }, 58 | "file_extension": ".py", 59 | "mimetype": "text/x-python", 60 | "name": "python", 61 | "nbconvert_exporter": "python", 62 | "pygments_lexer": "ipython3", 63 | "version": "3.8.8" 64 | } 65 | }, 66 | "nbformat": 4, 67 | "nbformat_minor": 5 68 | } 69 | -------------------------------------------------------------------------------- /22.Unique Elements.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "id": "4bdb90ef", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def uniqueElements(arr):\n", 11 | " numSet = {*()}\n", 12 | " for n in arr:\n", 13 | " numSet.add(n)\n", 14 | " \n", 15 | " return list(numSet)" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 4, 21 | "id": "096e004e", 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "arr = [1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9]" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 6, 31 | "id": "9c974321", 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "data": { 36 | "text/plain": [ 37 | "[1, 3, 5, 9]" 38 | ] 39 | }, 40 | "execution_count": 6, 41 | "metadata": {}, 42 | "output_type": "execute_result" 43 | } 44 | ], 45 | "source": [ 46 | "uniqueElements(arr)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "id": "22f7f77b", 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [] 56 | } 57 | ], 58 | "metadata": { 59 | "kernelspec": { 60 | "display_name": "Python 3", 61 | "language": "python", 62 | "name": "python3" 63 | }, 64 | "language_info": { 65 | "codemirror_mode": { 66 | "name": "ipython", 67 | "version": 3 68 | }, 69 | "file_extension": ".py", 70 | "mimetype": "text/x-python", 71 | "name": "python", 72 | "nbconvert_exporter": "python", 73 | "pygments_lexer": "ipython3", 74 | "version": "3.8.8" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 5 79 | } 80 | -------------------------------------------------------------------------------- /23.Horner's Algorithm.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 8, 6 | "id": "73c4583e", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "Value of polynomial is 5\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "# Python program for\n", 19 | "# implementation of Horner Method \n", 20 | "# for Polynomial Evaluation\n", 21 | " \n", 22 | "# returns value of poly[0]x(n-1)\n", 23 | "# + poly[1]x(n-2) + .. + poly[n-1]\n", 24 | "def horner(poly, n, x):\n", 25 | " \n", 26 | " # Initialize result\n", 27 | " result = poly[0] \n", 28 | " \n", 29 | " # Evaluate value of polynomial\n", 30 | " # using Horner's method\n", 31 | " for i in range(1, n):\n", 32 | " \n", 33 | " result = result*x + poly[i]\n", 34 | " \n", 35 | " return result\n", 36 | " \n", 37 | "# Driver program to\n", 38 | "# test above function.\n", 39 | " \n", 40 | "# Let us evaluate value of\n", 41 | "# 2x3 - 6x2 + 2x - 1 for x = 3\n", 42 | "poly = [2, -6, 2, -1]\n", 43 | "x = 3\n", 44 | "n = len(poly)\n", 45 | " \n", 46 | "print(\"Value of polynomial is \" , horner(poly, n, x))" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 9, 52 | "id": "61c6227c", 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "#My O(n) algorithm\n", 57 | "def jamaal(poly,x):\n", 58 | " res = 0\n", 59 | " n = len(poly)\n", 60 | " power = n - 1\n", 61 | " for i in range(n-1):\n", 62 | " res += poly[i]*(x**power)\n", 63 | " power -= 1\n", 64 | " \n", 65 | " res += poly[-1]\n", 66 | " \n", 67 | " return res" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 10, 73 | "id": "2f268896", 74 | "metadata": {}, 75 | "outputs": [ 76 | { 77 | "name": "stdout", 78 | "output_type": "stream", 79 | "text": [ 80 | "Value of polynomial is 5\n" 81 | ] 82 | } 83 | ], 84 | "source": [ 85 | "print(\"Value of polynomial is \" , jamaal(poly,x))" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "id": "961cbc8b", 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [] 95 | } 96 | ], 97 | "metadata": { 98 | "kernelspec": { 99 | "display_name": "Python 3", 100 | "language": "python", 101 | "name": "python3" 102 | }, 103 | "language_info": { 104 | "codemirror_mode": { 105 | "name": "ipython", 106 | "version": 3 107 | }, 108 | "file_extension": ".py", 109 | "mimetype": "text/x-python", 110 | "name": "python", 111 | "nbconvert_exporter": "python", 112 | "pygments_lexer": "ipython3", 113 | "version": "3.8.8" 114 | } 115 | }, 116 | "nbformat": 4, 117 | "nbformat_minor": 5 118 | } 119 | -------------------------------------------------------------------------------- /24.Moving Average.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 34, 6 | "id": "a929f238", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def ma(arr,n):\n", 11 | " res = []\n", 12 | " \n", 13 | " i = 0 \n", 14 | " \n", 15 | " while i < len(arr) - n + 1:\n", 16 | " window = arr[i:i+n]\n", 17 | " average = round((sum(window)/n),2)\n", 18 | " res.append(average)\n", 19 | " i += 1\n", 20 | " \n", 21 | " return res" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 35, 27 | "id": "6b2fd11a", 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "arr = [1,2,3,4,6]" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 36, 37 | "id": "38c78bbe", 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "[]" 44 | ] 45 | }, 46 | "execution_count": 36, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "ma(arr,6)" 53 | ] 54 | } 55 | ], 56 | "metadata": { 57 | "kernelspec": { 58 | "display_name": "Python 3", 59 | "language": "python", 60 | "name": "python3" 61 | }, 62 | "language_info": { 63 | "codemirror_mode": { 64 | "name": "ipython", 65 | "version": 3 66 | }, 67 | "file_extension": ".py", 68 | "mimetype": "text/x-python", 69 | "name": "python", 70 | "nbconvert_exporter": "python", 71 | "pygments_lexer": "ipython3", 72 | "version": "3.8.8" 73 | } 74 | }, 75 | "nbformat": 4, 76 | "nbformat_minor": 5 77 | } 78 | -------------------------------------------------------------------------------- /25.Sorting Algorithms.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 8, 6 | "id": "ff85f8a5", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def mergeSort(arr,s,e): #O(nlogn)\n", 11 | " if (e - s + 1) <= 1:\n", 12 | " return arr\n", 13 | " m = (s+e) // 2\n", 14 | " mergeSort(arr,s,m)\n", 15 | " mergeSort(arr,m+1,e)\n", 16 | " merge(arr,s,m,e)\n", 17 | " \n", 18 | " return arr\n", 19 | " \n", 20 | "def merge(arr,s,m,e):\n", 21 | " L = arr[s:m+1]\n", 22 | " R = arr[m+1:e+1]\n", 23 | " \n", 24 | " i,j,k = 0,0,s\n", 25 | " \n", 26 | " while i < len(L) and j < len(R):\n", 27 | " if L[i] <= R[j]:\n", 28 | " arr[k] = L[i]\n", 29 | " k += 1\n", 30 | " i += 1\n", 31 | " else:\n", 32 | " arr[k] = R[j]\n", 33 | " k += 1\n", 34 | " j += 1\n", 35 | " \n", 36 | " while i < len(L):\n", 37 | " arr[k] = L[i]\n", 38 | " k += 1\n", 39 | " i += 1\n", 40 | " while j < len(R):\n", 41 | " arr[k] = R[j]\n", 42 | " k += 1\n", 43 | " j += 1\n" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 10, 49 | "id": "cb153373", 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "data": { 54 | "text/plain": [ 55 | "[0, 0, 2, 4, 6, 7]" 56 | ] 57 | }, 58 | "execution_count": 10, 59 | "metadata": {}, 60 | "output_type": "execute_result" 61 | } 62 | ], 63 | "source": [ 64 | "arr = [0,0,4,2,7,6]\n", 65 | "mergeSort(arr,0,len(arr))" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 14, 71 | "id": "26db07ef", 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "data": { 76 | "text/plain": [ 77 | "[0, 0, 2, 4, 6, 7]" 78 | ] 79 | }, 80 | "execution_count": 14, 81 | "metadata": {}, 82 | "output_type": "execute_result" 83 | } 84 | ], 85 | "source": [ 86 | "def insertionSort(arr): #O(n^2)\n", 87 | " for i in range(1,len(arr)):\n", 88 | " j = i - 1\n", 89 | " while( j >= 0 and arr[j+1] < arr[j]):\n", 90 | " arr[j+1], arr[i] = arr[i], arr[j+1]\n", 91 | " j -= 1\n", 92 | " return arr\n", 93 | "insertionSort(arr)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 16, 99 | "id": "504a3780", 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "[0, 0, 2, 4, 6, 7]" 106 | ] 107 | }, 108 | "execution_count": 16, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "def quickSort(arr,s,e): #O(n^2) but is usually O(nlogn)\n", 115 | " if (e -s + 1) <= 1:\n", 116 | " return arr\n", 117 | " \n", 118 | " pivot = arr[e]\n", 119 | " left = s\n", 120 | " \n", 121 | " for i in range(s,e):\n", 122 | " if (arr[i] < pivot):\n", 123 | " arr[left], arr[i] = arr[i], arr[left]\n", 124 | " left += 1\n", 125 | " \n", 126 | " arr[e] = arr[left]\n", 127 | " arr[left] = pivot\n", 128 | " \n", 129 | " quickSort(arr,s,left-1)\n", 130 | " quickSort(arr,left+1,e)\n", 131 | " \n", 132 | " return arr\n", 133 | "\n", 134 | "quickSort(arr,0,len(arr)-1)" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": null, 140 | "id": "a0957e51", 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [] 144 | } 145 | ], 146 | "metadata": { 147 | "kernelspec": { 148 | "display_name": "Python 3", 149 | "language": "python", 150 | "name": "python3" 151 | }, 152 | "language_info": { 153 | "codemirror_mode": { 154 | "name": "ipython", 155 | "version": 3 156 | }, 157 | "file_extension": ".py", 158 | "mimetype": "text/x-python", 159 | "name": "python", 160 | "nbconvert_exporter": "python", 161 | "pygments_lexer": "ipython3", 162 | "version": "3.8.8" 163 | } 164 | }, 165 | "nbformat": 4, 166 | "nbformat_minor": 5 167 | } 168 | -------------------------------------------------------------------------------- /26.Random Permutation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 8, 6 | "id": "5b24b53f", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "[18, 3, 32, 22, 11, 14, 5, 33, 39, 40, 30, 19, 51, 28, 10, 36, 41, 9, 35, 37, 24, 25, 48, 29, 17, 47, 44, 26, 8, 6, 21, 45, 1, 23, 2, 49, 46, 13, 12, 34, 15, 31, 16, 43, 50, 20, 38, 7, 4, 42, 0, 27]\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "import random\n", 19 | "#Knuth Shuffle\n", 20 | "A = [i for i in range(52)]\n", 21 | "for i in range(52):\n", 22 | " random_num = random.randint(0,51)\n", 23 | " A[i],A[random_num] = A[random_num], A[i]\n", 24 | "\n", 25 | "print(A)\n", 26 | " " 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "id": "4b474046", 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [] 36 | } 37 | ], 38 | "metadata": { 39 | "kernelspec": { 40 | "display_name": "Python 3", 41 | "language": "python", 42 | "name": "python3" 43 | }, 44 | "language_info": { 45 | "codemirror_mode": { 46 | "name": "ipython", 47 | "version": 3 48 | }, 49 | "file_extension": ".py", 50 | "mimetype": "text/x-python", 51 | "name": "python", 52 | "nbconvert_exporter": "python", 53 | "pygments_lexer": "ipython3", 54 | "version": "3.8.8" 55 | } 56 | }, 57 | "nbformat": 4, 58 | "nbformat_minor": 5 59 | } 60 | -------------------------------------------------------------------------------- /27.Maximum Sum Contiguous Subarray.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 8, 6 | "id": "69d65073", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def maxSubArray(nums):\n", 11 | " maxSub = nums[0]\n", 12 | " currSum = 0 \n", 13 | " for n in nums:\n", 14 | " currSum = max(n, currSum +n)\n", 15 | " maxSub = max(maxSub, currSum)\n", 16 | " \n", 17 | " return maxSub" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 9, 23 | "id": "db8f3257", 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "arr = [1,2,-5,4,-3,2,6,-5,-1]" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 10, 33 | "id": "dda86a4c", 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "data": { 38 | "text/plain": [ 39 | "9" 40 | ] 41 | }, 42 | "execution_count": 10, 43 | "metadata": {}, 44 | "output_type": "execute_result" 45 | } 46 | ], 47 | "source": [ 48 | "maxSubArray(arr)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "id": "d295fe6c", 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [] 58 | } 59 | ], 60 | "metadata": { 61 | "kernelspec": { 62 | "display_name": "Python 3", 63 | "language": "python", 64 | "name": "python3" 65 | }, 66 | "language_info": { 67 | "codemirror_mode": { 68 | "name": "ipython", 69 | "version": 3 70 | }, 71 | "file_extension": ".py", 72 | "mimetype": "text/x-python", 73 | "name": "python", 74 | "nbconvert_exporter": "python", 75 | "pygments_lexer": "ipython3", 76 | "version": "3.8.8" 77 | } 78 | }, 79 | "nbformat": 4, 80 | "nbformat_minor": 5 81 | } 82 | -------------------------------------------------------------------------------- /28.Millenium Brainteaser.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 12, 6 | "id": "e96ed159", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "def f(n):\n", 11 | " if n == 1:\n", 12 | " return (1/6)\n", 13 | " return (1/(n*(4*n-1)*(4*n-2)))+f(n-1)" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 20, 19 | "id": "5da5b511", 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "F = f(1024)" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 21, 29 | "id": "d51a68da", 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "def g(n):\n", 34 | " if n == 1:\n", 35 | " return (1/30)\n", 36 | " return (1/(n*(4*n+1)*(4*n+2)))+g(n-1)" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 22, 42 | "id": "0492fcd2", 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "G = g(1024)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 23, 52 | "id": "94df712f", 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "res = F + 3 - G" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 24, 62 | "id": "b04b05cf", 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "data": { 67 | "text/plain": [ 68 | "3.141592653560732" 69 | ] 70 | }, 71 | "execution_count": 24, 72 | "metadata": {}, 73 | "output_type": "execute_result" 74 | } 75 | ], 76 | "source": [ 77 | "res" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 28, 83 | "id": "3e911182", 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "data": { 88 | "text/plain": [ 89 | "3.141592653589793" 90 | ] 91 | }, 92 | "execution_count": 28, 93 | "metadata": {}, 94 | "output_type": "execute_result" 95 | } 96 | ], 97 | "source": [ 98 | "import math\n", 99 | "math.pi" 100 | ] 101 | } 102 | ], 103 | "metadata": { 104 | "kernelspec": { 105 | "display_name": "Python 3", 106 | "language": "python", 107 | "name": "python3" 108 | }, 109 | "language_info": { 110 | "codemirror_mode": { 111 | "name": "ipython", 112 | "version": 3 113 | }, 114 | "file_extension": ".py", 115 | "mimetype": "text/x-python", 116 | "name": "python", 117 | "nbconvert_exporter": "python", 118 | "pygments_lexer": "ipython3", 119 | "version": "3.8.8" 120 | } 121 | }, 122 | "nbformat": 4, 123 | "nbformat_minor": 5 124 | } 125 | -------------------------------------------------------------------------------- /29.Umbrella Q.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 7, 6 | "id": "6809925f", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "Probability of getting wet = 0.28547885369653314\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "import random\n", 19 | "\n", 20 | "umbrellas = {\"home\":1,\"work\":1}\n", 21 | "currState = \"home\"\n", 22 | "\n", 23 | "wetCount = 0 \n", 24 | "isWet = 0 \n", 25 | "iterations = 1000000\n", 26 | "\n", 27 | "for i in range(iterations):\n", 28 | " if currState == \"home\":\n", 29 | " nextState = \"work\"\n", 30 | " else:\n", 31 | " nextState = \"home\"\n", 32 | " chanceOfRain = random.randint(0,4)\n", 33 | " if chanceOfRain == 0:\n", 34 | " isWet += 1\n", 35 | " if chanceOfRain == 0 and umbrellas[currState] == 0 :\n", 36 | " wetCount += 1\n", 37 | " #print(f\"Wet travelling from {currState} to {nextState}\")\n", 38 | " elif chanceOfRain == 0 and umbrellas[currState] > 0:\n", 39 | " umbrellas[currState] -= 1\n", 40 | " umbrellas[nextState] += 1\n", 41 | " \n", 42 | " currState = nextState\n", 43 | " \n", 44 | "\n", 45 | "print(f\"Probability of getting wet = {wetCount/isWet}\")\n", 46 | " " 47 | ] 48 | } 49 | ], 50 | "metadata": { 51 | "kernelspec": { 52 | "display_name": "Python 3", 53 | "language": "python", 54 | "name": "python3" 55 | }, 56 | "language_info": { 57 | "codemirror_mode": { 58 | "name": "ipython", 59 | "version": 3 60 | }, 61 | "file_extension": ".py", 62 | "mimetype": "text/x-python", 63 | "name": "python", 64 | "nbconvert_exporter": "python", 65 | "pygments_lexer": "ipython3", 66 | "version": "3.8.8" 67 | } 68 | }, 69 | "nbformat": 4, 70 | "nbformat_minor": 5 71 | } 72 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Quantitative Coding Questions 2 | 3 |
Most of the questions are from the algorithms section of Quant Job Q&A by Mark Joshi 4 | and Practical Guide to Quantitative Finance Interviews by Xinfeng Zhou. 5 |
List is being updated whenever I implement a coding solution to a question 6 | --------------------------------------------------------------------------------