Skip to content

Commit ca79c7e

Browse files
authored
Add files via upload
1 parent 8c9b62d commit ca79c7e

9 files changed

+9
-0
lines changed

Code/Bubble_sort_practice.ipynb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"cell_type":"markdown","id":"2daaee0e","metadata":{"id":"2daaee0e"},"source":["### Implement a python function for swapping two variables\n","\n","(ex) x = 3, y = 8 --> swap(x, y) --> x = 8, y = 3"]},{"cell_type":"code","execution_count":null,"id":"f2dc11aa","metadata":{"id":"f2dc11aa"},"outputs":[],"source":["# Load python tutor\n","from metakernel import register_ipython_magics\n","register_ipython_magics()"]},{"cell_type":"code","execution_count":null,"id":"04dac262","metadata":{"id":"04dac262","outputId":"5a8e6fb4-338e-477a-8ba4-a5c1c773bcc8"},"outputs":[{"data":{"text/html":["\n"," <iframe\n"," width=\"100%\"\n"," height=\"500\"\n"," src=\"https://pythontutor.com/iframe-embed.html#code=%0Adef%20swap%28x%20%3A%20int%2C%20y%20%3A%20int%29%3A%0A%20%20%20%20tmp%20%3D%20x%0A%20%20%20%20x%20%3D%20y%0A%20%20%20%20y%20%3D%20tmp%0A%20%20%20%20print%28%22x%20%3D%22%2C%20x%2C%20%22%2C%22%2C%20%22y%20%3D%22%2C%20y%29%20%20%20%0A%0Aswap%28x%20%3D%203%2C%20y%20%3D%208%29%0A&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400\"\n"," frameborder=\"0\"\n"," allowfullscreen\n"," \n"," ></iframe>\n"," "],"text/plain":["<IPython.lib.display.IFrame at 0x7f23d45e5160>"]},"metadata":{},"output_type":"display_data"}],"source":["%%tutor\n","\n","def swap(x : int, y : int):\n"," tmp = x\n"," x = y\n"," y = tmp\n"," print(\"x =\", x, \",\", \"y =\", y)\n","\n","swap(x = 3, y = 8)"]},{"cell_type":"markdown","id":"25064f9b","metadata":{"id":"25064f9b"},"source":["### Bubble sort\n","\n","![nn](https://gmlwjd9405.github.io/images/algorithm-bubble-sort/bubble-sort.png)"]},{"cell_type":"code","execution_count":null,"id":"adc66f03","metadata":{"id":"adc66f03","outputId":"9d9adde0-209b-4324-c657-df255228381a"},"outputs":[{"data":{"text/html":["\n"," <iframe\n"," width=\"100%\"\n"," height=\"500\"\n"," src=\"https://pythontutor.com/iframe-embed.html#code=%0Adef%20Bubble_sort%28A%20%3A%20list%29%3A%0A%20%20%20%20n%20%3D%20len%28A%29%0A%20%20%20%20%0A%20%20%20%20for%20i%20in%20range%28n-1%29%3A%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%280%2C%20n-i-1%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20A%5Bj%5D%20%3E%20A%5Bj%2B1%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20A%5Bj%5D%2C%20A%5Bj%2B1%5D%20%3D%20A%5Bj%2B1%5D%2C%20A%5Bj%5D%0A%20%20%20%20print%28A%29%0A%20%20%20%20%0AA%20%3D%20%5B5%2C%202%2C%204%2C%201%2C%203%5D%0ABubble_sort%28A%29%0A&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400\"\n"," frameborder=\"0\"\n"," allowfullscreen\n"," \n"," ></iframe>\n"," "],"text/plain":["<IPython.lib.display.IFrame at 0x7f23d4767820>"]},"metadata":{},"output_type":"display_data"}],"source":["%%tutor\n","\n","def Bubble_sort(A : list):\n"," n = len(A)\n","\n"," for i in range(n-1):\n"," for j in range(0, n-i-1):\n"," if A[j] > A[j+1]:\n"," A[j], A[j+1] = A[j+1], A[j]\n"," print(A)\n","\n","A = [5, 2, 4, 1, 3]\n","Bubble_sort(A)"]}],"metadata":{"kernelspec":{"display_name":"Python 3 (ipykernel)","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.9.12"},"colab":{"provenance":[]}},"nbformat":4,"nbformat_minor":5}

Code/Dynamic_practice.ipynb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"cell_type":"code","execution_count":null,"id":"ef960c8e","metadata":{"id":"ef960c8e"},"outputs":[],"source":["from metakernel import register_ipython_magics\n","register_ipython_magics()"]},{"cell_type":"markdown","id":"7398e3df","metadata":{"id":"7398e3df"},"source":["# Dynamic Programming"]},{"cell_type":"markdown","id":"f5cd77c5","metadata":{"id":"f5cd77c5"},"source":["### Binominal Coefficient (이항계수)"]},{"cell_type":"code","execution_count":null,"id":"90062d7e","metadata":{"id":"90062d7e"},"outputs":[],"source":["def comb(n : int, k : int):\n"," C = [[0 for j in range(k + 1)] for i in range(n + 1)]\n","\n"," for i in range(1, n + 1):\n"," for j in range(min(i, k) + 1):\n"," if j == 0 or j == i:\n"," C[i][j] = 1\n"," else:\n"," C[i][j] = C[i-1][j-1] + C[i-1][j]\n","\n"," return C[n][k]"]},{"cell_type":"code","execution_count":null,"id":"17f17542","metadata":{"id":"17f17542","outputId":"4fa95f4a-0e70-4405-96ca-1e492bb637d1"},"outputs":[{"name":"stdout","output_type":"stream","text":["10\n"]}],"source":["print(comb(5, 3))"]},{"cell_type":"code","execution_count":null,"id":"f2cb38f2","metadata":{"id":"f2cb38f2","outputId":"000345f1-d029-4d66-f258-962bd593f57a"},"outputs":[{"name":"stdout","output_type":"stream","text":["1\n"]}],"source":["print(comb(5, 5))"]},{"cell_type":"code","execution_count":null,"id":"b00bdb22","metadata":{"id":"b00bdb22","outputId":"1cbec678-66e7-4a69-f39b-ab348227d299"},"outputs":[{"name":"stdout","output_type":"stream","text":["1\n"]}],"source":["print(comb(5, 0))"]},{"cell_type":"markdown","id":"ec813b9d","metadata":{"id":"ec813b9d"},"source":["### Matrix chain Multiplication (행렬의 곱셈순서)"]},{"cell_type":"code","execution_count":null,"id":"9e384f67","metadata":{"id":"9e384f67"},"outputs":[],"source":["import sys\n","\n","def MatrixChainOrder(n : int, d : list):\n","\n"," M = [[0 * x for x in range(n)] for x in range(n)]\n","\n"," for diag in range(1, n):\n"," for i in range(1, n - diag):\n"," j = i + diag\n"," M[i][j] = sys.maxsize # Initialize inf.\n"," for k in range(i, j):\n"," M[i][j] = min(M[i][j],\n"," M[i][k] + M[k + 1][j] + d[i-1] * d[k] * d[j])\n","\n"," return M[1][n-1]\n","\n","# A : 10 * 30 matrix, B : 30 * 5 matrix, C : 5 * 60 matrix\n","\n","d = [10, 30, 5, 60] # d0 = 10, d1 = 30, d2 = 5, d3 = 60\n","n = 4"]},{"cell_type":"code","execution_count":null,"id":"f6ffcafd","metadata":{"id":"f6ffcafd","outputId":"e3127fb8-73d1-49f9-8f93-96d5c1eb2964"},"outputs":[{"name":"stdout","output_type":"stream","text":["4500\n"]}],"source":["print(MatrixChainOrder(n, d)) # (AB)C = (10 * 30 * 5) + (10 * 5 * 60) = 1500 + 3000 = 4500"]}],"metadata":{"kernelspec":{"display_name":"Python 3 (ipykernel)","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.9.12"},"colab":{"provenance":[]}},"nbformat":4,"nbformat_minor":5}

Code/Fibonacci_practice.ipynb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"cell_type":"markdown","id":"1c7d1683","metadata":{"id":"1c7d1683"},"source":["### Dynamic programming"]},{"cell_type":"code","execution_count":null,"id":"ba7b5c4a","metadata":{"id":"ba7b5c4a"},"outputs":[],"source":["# Fibonacci sequence by suing recursive algorithms\n","\n","def fib1(n: int):\n"," if n == 0:\n"," return 0\n"," elif n == 1:\n"," return 1\n"," else:\n"," return fib1(n-1) + fib1(n-2)"]},{"cell_type":"code","execution_count":null,"id":"7aeeae6f","metadata":{"id":"7aeeae6f","outputId":"e1bfb243-4256-439b-ff88-c5a5a45e4cba"},"outputs":[{"name":"stdout","output_type":"stream","text":["Input integer : 4\n","3\n"]}],"source":["n = int(input(\"Input integer : \"))\n","\n","print(fib1(n))"]},{"cell_type":"code","execution_count":null,"id":"2dbe4730","metadata":{"id":"2dbe4730"},"outputs":[],"source":["# Fibonacci sequence by using dynamic programming\n","\n","def fib2(n: int):\n"," fib = [0 * i for i in range(n+1)]\n","\n"," fib[0] = 0\n"," fib[1] = 1\n","\n"," for i in range(2, n+1):\n"," fib[i] = fib[i-1] + fib[i-2]\n","\n"," return fib[n]"]},{"cell_type":"code","execution_count":null,"id":"c0c15629","metadata":{"id":"c0c15629","outputId":"981d0065-dd07-4fc2-ac65-fc4cf31cd43c"},"outputs":[{"name":"stdout","output_type":"stream","text":["Input integer : 4\n","3\n"]}],"source":["n = int(input(\"Input integer : \"))\n","\n","print(fib2(n))"]}],"metadata":{"kernelspec":{"display_name":"Algorithms","language":"python","name":"algorithms"},"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.7.13"},"colab":{"provenance":[]}},"nbformat":4,"nbformat_minor":5}

Code/Flyoid_practice.ipynb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"cell_type":"code","execution_count":null,"id":"21fe7525","metadata":{"id":"21fe7525"},"outputs":[],"source":["from metakernel import register_ipython_magics\n","register_ipython_magics()\n","import sys"]},{"cell_type":"markdown","id":"bef15c6a","metadata":{"id":"bef15c6a"},"source":["### Flyoid's algorithms"]},{"cell_type":"code","execution_count":null,"id":"9666ff0e","metadata":{"id":"9666ff0e"},"outputs":[],"source":["INF = sys.maxsize\n","\n","def Floyd(n : int, A : list):\n","\n"," D = [[INF] * (n) for _ in range(n)]\n","\n"," for i in range(n):\n"," for j in range(n):\n"," if i == j:\n"," D[i][j] = 0\n"," else:\n"," D[i][j] = A[i][j]\n","\n","\n"," for k in range(n):\n"," for i in range(n):\n"," for j in range(n):\n"," if D[i][j] > D[i][k] + D[k][j]:\n"," D[i][j] = D[i][k] + D[k][j]\n","\n"," return D"]},{"cell_type":"code","execution_count":null,"id":"75cdbd6a","metadata":{"id":"75cdbd6a","outputId":"3e9ef979-bd15-4592-823a-0c35b786dcea"},"outputs":[{"name":"stdout","output_type":"stream","text":["[[0, 6, 9, 2], [15, 0, 12, 5], [3, 9, 0, 5], [10, 16, 7, 0]]\n"]}],"source":["n = 4\n","G = [[0, 6, 10, 2],\n"," [INF, 0, INF, 5],\n"," [3, 12, 0, INF],\n"," [INF, INF, 7, 0]]\n","\n","print(Floyd(n, G))"]}],"metadata":{"kernelspec":{"display_name":"Python 3 (ipykernel)","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.9.12"},"colab":{"provenance":[]}},"nbformat":4,"nbformat_minor":5}

Code/Merge_sort_practice.ipynb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"cell_type":"markdown","id":"aa70ec6a","metadata":{"id":"aa70ec6a"},"source":["### Merge two sorted sublists to one sorted list\n","\n","Input : [1, 5, 8], [2, 3, 6]\n","\n","Output : [1, 2, 3, 5, 6, 8]"]},{"cell_type":"code","execution_count":null,"id":"5d78119b","metadata":{"id":"5d78119b"},"outputs":[],"source":["def merge(A : list, B : list):\n"," output = []\n"," i = 0; j = 0\n","\n"," while (i < len(A)) and (j < len(B)):\n"," if A[i] < B[j]:\n"," output.append(A[i])\n"," i += 1\n"," elif A[i] > B[j]:\n"," output.append(B[j])\n"," j += 1\n","\n"," if i < j:\n"," output.append(A[i])\n"," else:\n"," output.append(B[j])\n","\n"," print(output)"]},{"cell_type":"code","execution_count":null,"id":"0d06e31c","metadata":{"id":"0d06e31c"},"outputs":[],"source":["A = [1, 5, 8]\n","B = [2, 3, 6]"]},{"cell_type":"code","execution_count":null,"id":"f9406089","metadata":{"id":"f9406089","outputId":"7281987d-1ba6-4640-c271-e1afcd6cfcb0"},"outputs":[{"name":"stdout","output_type":"stream","text":["[1, 2, 3, 5, 6, 8]\n"]}],"source":["merge(A, B)"]},{"cell_type":"markdown","id":"8cd692f3","metadata":{"id":"8cd692f3"},"source":["### Implement merge sort"]},{"cell_type":"code","execution_count":null,"id":"ad3fb706","metadata":{"id":"ad3fb706"},"outputs":[],"source":["A = [54, 23, 79, 13]"]},{"cell_type":"code","execution_count":null,"id":"a4092741","metadata":{"id":"a4092741"},"outputs":[],"source":["def MergeSort(A : list):\n"," if len(A) <= 1:\n"," return A\n","\n"," mid = len(A) // 2\n","\n"," left = A[:mid]\n"," right = A[mid:]\n","\n"," leftlist = MergeSort(left)\n"," rightlist = MergeSort(right)\n","\n"," return Merge(leftlist, rightlist)"]},{"cell_type":"code","execution_count":null,"id":"ed6d2e5c","metadata":{"id":"ed6d2e5c"},"outputs":[],"source":["def Merge(A : list, B : list):\n"," output = []\n"," i = 0; j = 0\n","\n"," while (i < len(A)) and (j < len(B)):\n"," if A[i] < B[j]:\n"," output.append(A[i])\n"," i += 1\n"," elif A[i] > B[j]:\n"," output.append(B[j])\n"," j += 1\n","\n"," if i < j:\n"," output.append(A[i])\n"," else:\n"," output.append(B[j])\n","\n"," return output"]},{"cell_type":"code","execution_count":null,"id":"8959719c","metadata":{"id":"8959719c","outputId":"982f4523-7b55-4666-cf44-911ec4639ce0"},"outputs":[{"name":"stdout","output_type":"stream","text":["[13, 23, 54, 79]\n"]}],"source":["print(MergeSort(A))"]}],"metadata":{"kernelspec":{"display_name":"Python 3 (ipykernel)","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.9.12"},"colab":{"provenance":[]}},"nbformat":4,"nbformat_minor":5}

Code/Quick_sort_practice.ipynb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"cell_type":"code","execution_count":null,"id":"6de0af48","metadata":{"id":"6de0af48"},"outputs":[],"source":["## Tutor Magic in IPython\n","from metakernel import register_ipython_magics\n","register_ipython_magics()"]},{"cell_type":"markdown","id":"bb05eedd","metadata":{"id":"bb05eedd"},"source":["### Implement Quick Sort"]},{"cell_type":"code","execution_count":null,"id":"6cfb2a5a","metadata":{"id":"6cfb2a5a"},"outputs":[],"source":["def QuickSort(A : list, low : int, high : int):\n"," if (high > low):\n"," mid = Partition(A, low, high)\n"," QuickSort(A, low, mid - 1)\n"," QuickSort(A, mid + 1, high)\n"," return A\n","\n","def Partition(A : list, low : int, high : int):\n"," p = low\n"," pivot = A[low]\n"," for i in range(low + 1, high + 1):\n"," if A[i] < pivot:\n"," p += 1\n"," A[p], A[i] = A[i], A[p]\n"," A[low], A[p] = A[p], A[low]\n"," return p"]},{"cell_type":"code","execution_count":null,"id":"15af7b81","metadata":{"id":"15af7b81","outputId":"5cd0709a-bbac-4f0f-c8c2-ebf41f4edca9"},"outputs":[{"data":{"text/plain":["[15, 17, 23, 35, 47, 61, 84, 92]"]},"execution_count":12,"metadata":{},"output_type":"execute_result"}],"source":["A = [47, 84, 15, 23, 35, 92, 61, 17]\n","QuickSort(A, 0, len(A) - 1)"]}],"metadata":{"kernelspec":{"display_name":"Python 3 (ipykernel)","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.9.12"},"colab":{"provenance":[]}},"nbformat":4,"nbformat_minor":5}

Code/Search_practice.ipynb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"cell_type":"markdown","id":"18048cb8-c2e6-4e24-bab0-53f47321f36d","metadata":{"id":"18048cb8-c2e6-4e24-bab0-53f47321f36d"},"source":["### Implement linear search"]},{"cell_type":"code","execution_count":null,"id":"1c56f614-dafd-4e48-84bb-ef54483ab274","metadata":{"id":"1c56f614-dafd-4e48-84bb-ef54483ab274"},"outputs":[],"source":["def linear_search(A : list, x : int):\n"," n = len(A)\n","\n"," for i in range(n):\n"," if A[i] == x:\n"," return i\n","\n"," return None"]},{"cell_type":"code","execution_count":null,"id":"718a1eb0","metadata":{"id":"718a1eb0"},"outputs":[],"source":["A = [15, 45, 31, 16, 27, 12, 8]"]},{"cell_type":"code","execution_count":null,"id":"2a71f64b","metadata":{"id":"2a71f64b","outputId":"fa11dc71-31b7-4ba3-c4c5-fff276cb0620"},"outputs":[{"name":"stdout","output_type":"stream","text":["4\n"]}],"source":["print(linear_search(A, 27))"]},{"cell_type":"code","execution_count":null,"id":"3c7d969c","metadata":{"id":"3c7d969c","outputId":"57363930-4d31-4249-ddc9-f0d6344b382b"},"outputs":[{"name":"stdout","output_type":"stream","text":["None\n"]}],"source":["print(linear_search(A, 3))"]},{"cell_type":"markdown","id":"d023ccd5","metadata":{"id":"d023ccd5"},"source":["### Implement binary search"]},{"cell_type":"code","execution_count":null,"id":"b907240b","metadata":{"id":"b907240b"},"outputs":[],"source":["def binary_search(A : list, low : int, high : int, x : int):\n"," mid = (low + high) // 2\n","\n"," if high > low:\n"," if A[mid] == x:\n"," return mid\n"," elif A[mid] < x:\n"," binary_search(A, low, mid, x)\n"," else:\n"," binary_search(A, mid + 1, high, x)\n"," return None"]},{"cell_type":"code","execution_count":null,"id":"82520e95","metadata":{"id":"82520e95","outputId":"1176a83e-9bfb-42c1-f82e-184d629cfc72"},"outputs":[{"name":"stdout","output_type":"stream","text":["4\n"]}],"source":["print(binary_search(A, 1, len(A), 27))"]},{"cell_type":"code","execution_count":null,"id":"1ffc7781","metadata":{"id":"1ffc7781","outputId":"1a30fff1-0611-4201-8a96-565f9f12d748"},"outputs":[{"name":"stdout","output_type":"stream","text":["None\n"]}],"source":["print(binary_search(A, 1, len(A), 3))"]}],"metadata":{"kernelspec":{"display_name":"Python 3 (ipykernel)","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.9.12"},"colab":{"provenance":[]}},"nbformat":4,"nbformat_minor":5}

Code/dijkstra_practice.ipynb

+1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)