diff --git a/Python/BT - 1.ipynb b/Python/BT - 1.ipynb new file mode 100644 index 0000000..60f794e --- /dev/null +++ b/Python/BT - 1.ipynb @@ -0,0 +1,1023 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Print Binary Tree" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "class BT:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.right = None\n", + " self.left = None" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "def PrintBT(root):\n", + " if root is None:\n", + " return\n", + " print(root.data,end=\" : \")\n", + " if root.left is not None:\n", + " print('L',root.left.data,end=\" \")\n", + " if root.right is not None:\n", + " print('R',root.right.data,end =\"\")\n", + " print()\n", + " PrintBT(root.left)\n", + " PrintBT(root.right)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Take Binary Tree Input" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "def inputBT():\n", + " data = int(input())\n", + " root = BT(data)\n", + " if data == -1:\n", + " return\n", + " root.left = inputBT()\n", + " root.right = inputBT()\n", + " return root" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "4\n", + "-1\n", + "-1\n", + "5\n", + "-1\n", + "-1\n", + "3\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "a = inputBT()" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 : L 2 R 3\n", + "2 : L 4 R 5\n", + "4 : \n", + "5 : \n", + "3 : \n" + ] + } + ], + "source": [ + "PrintBT(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Counting Length of BT" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "def countBT(root):\n", + " if root is None:\n", + " return 0\n", + " left = countBT(root.left)\n", + " right = countBT(root.right)\n", + " return left + right +1" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "countBT(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Sum of Nodes" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "def sumBT(root):\n", + " if root is None:\n", + " return 0\n", + " left = sumBT(root.left)\n", + " right = sumBT(root.right)\n", + " return root.data + left + right" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sumBT(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Pre order" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "class BT:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.left = None\n", + " self.right = None\n", + "def inputBT():\n", + " n= int(input())\n", + " if n == -1:\n", + " return \n", + " root = BT(n)\n", + " root.left = inputBT()\n", + " root.right = inputBT()\n", + " return root\n", + "def preorder(root):\n", + " if root is None:\n", + " return\n", + " print(root.data)\n", + " preorder(root.left)\n", + " preorder(root.right)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "a = inputBT()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n" + ] + } + ], + "source": [ + "preorder(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### postorder - who will be completed first" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "def postorder(a):\n", + " if a is None:\n", + " return\n", + " postorder(a.left)\n", + " postorder(a.right)\n", + " print(a.data)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n", + "6\n", + "5\n", + "4\n", + "3\n", + "2\n", + "1\n" + ] + } + ], + "source": [ + "postorder(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "-1\n", + "-1\n", + "4\n", + "-1\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "a = inputBT()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### inorder - left then root then right" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "def inorder(root):\n", + " if root is None:\n", + " return\n", + " inorder(root.left)\n", + " print(root.data)\n", + " inorder(root.right)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "2\n", + "4\n", + "1\n" + ] + } + ], + "source": [ + "inorder(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Finding largest node" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "def findLargest(root):\n", + " if root is None:\n", + " return -1\n", + " a = findLargest(root.left)\n", + " b = findLargest(root.right)\n", + " return max(a,b,root.data)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "findLargest(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Find greater than nodes" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [], + "source": [ + "def greater(root,data):\n", + " if root is None:\n", + " return 0\n", + " left_count = greater(root.left,data)\n", + " right_count = greater(root.right,data)\n", + " if root.data>data:\n", + " return 1+ left_count + right_count \n", + " else:\n", + " return left_count + right_count" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "greater(a,2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Height of the tree" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [], + "source": [ + "def height(root):\n", + " if root is None:\n", + " return 0\n", + " a = height(root.left)\n", + " b = height(root.right)\n", + " if a>b:\n", + " return a +1\n", + " else:\n", + " return b+1" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "height(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "8\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "a = inputBT()" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 : L 2 R 8 \n", + "2 : L 3 \n", + "3 : L 4 \n", + "4 : L 5 \n", + "5 : L 6 \n", + "6 : \n", + "8 : \n" + ] + } + ], + "source": [ + "outputBT(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "height(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Number of leaf Node" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [], + "source": [ + "def leaf_node(root):\n", + " if root is None:\n", + " return 0\n", + " a = leaf_node(root.left)\n", + " b = leaf_node(root.right)\n", + " if root.left is None and root.right is None:\n", + " return a +b +1\n", + " return a+b" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "leaf_node(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "b = inputBT()" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "leaf_node(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Better way" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [], + "source": [ + "def leafNode(root):\n", + " if root is None:\n", + " return 0\n", + " if root.left is None and root.right is None:\n", + " return 1\n", + " leftLeaf = leafNode(root.left)\n", + " rightLeaf = leafNode(root.right)\n", + " return leftLeaf + rightLeaf" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "leafNode(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Print node at dept k" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [], + "source": [ + "def depthK(root,k):\n", + " if root is None:\n", + " return \n", + " if k == 0:\n", + " print(root.data)\n", + " return \n", + " depthK(root.left,k-1)\n", + " depthK(root.right,k-1)" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "8\n" + ] + } + ], + "source": [ + "depthK(a,1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Replace node with depth" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [], + "source": [ + "def replace_node(root,d =0):\n", + " if root is None:\n", + " return\n", + " replace_node(root.left, d+1)\n", + " replace_node(root.right,d+1)\n", + " root.data = d\n", + " return root" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [], + "source": [ + "c=replace_node(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 : L 1 R 1 \n", + "1 : L 2 \n", + "2 : L 3 \n", + "3 : L 4 \n", + "4 : L 5 \n", + "5 : \n", + "1 : \n" + ] + } + ], + "source": [ + "outputBT(c)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Find a Node" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "class BT:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.right = None\n", + " self.left = None\n", + "def inputBT():\n", + " n = int(input())\n", + " if n == -1:\n", + " return \n", + " root = BT(n)\n", + " root.left = inputBT()\n", + " root.right = inputBT()\n", + " return root\n", + "def outputBT(root):\n", + " if root is None:\n", + " return \n", + " print(root.data)\n", + " outputBT(root.left)\n", + " outputBT(root.right)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "def find_node(root,x):\n", + " if root is None:\n", + " return False\n", + " Left = find_node(root.left,x)\n", + " Right = find_node(root.right,x)\n", + " if root.data == x:\n", + " return True\n", + " elif Left or Right:\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "-1\n", + "-1\n", + "4\n", + "-1\n", + "-1\n", + "5\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "a = inputBT()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n" + ] + } + ], + "source": [ + "outputBT(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "find_node(a,3)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "find_node(a,6)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Node without Sibling" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "def no_sibling(a):\n", + " if a is None:\n", + " return 0\n", + " Left = no_sibling(a.left)\n", + " Right= no_sibling(a.right)\n", + " if a.left is None and a.right is None:\n", + " return 1+Left+Right\n", + " else:\n", + " return Left+Right" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "no_sibling(a)" + ] + } + ], + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/Python/BT-2.ipynb b/Python/BT-2.ipynb new file mode 100644 index 0000000..ee33af6 --- /dev/null +++ b/Python/BT-2.ipynb @@ -0,0 +1,1680 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "class BT:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.left = None\n", + " self.right = None\n", + "def inputBT():\n", + " n = int(input())\n", + " if n == -1:\n", + " return\n", + " root = BT(n)\n", + " root.left = inputBT()\n", + " root.right = inputBT()\n", + " return root\n", + "def outputBT(root):\n", + " if root is None:\n", + " return\n", + " print(root.data,end = \" : \")\n", + " if root.left:\n", + " print(\"L\",root.left.data,end =\" \")\n", + " if root.right:\n", + " print(\"R\",root.right.data,end = \" \")\n", + " print()\n", + " outputBT(root.left)\n", + " outputBT(root.right)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "-1\n", + "-1\n", + "4\n", + "-1\n", + "-1\n", + "5\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "a = inputBT()" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 : L 2 R 5 \n", + "2 : L 3 R 4 \n", + "3 : \n", + "4 : \n", + "5 : \n" + ] + } + ], + "source": [ + "outputBT(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "def remove_leaf(root):\n", + " if root is None:\n", + " return None\n", + " if root.left is None and root.right is None:\n", + " return None\n", + " root.left = remove_leaf(root.left)\n", + " root.right = remove_leaf(root.right)\n", + " return root" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "b =remove_leaf(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 : L 2 \n", + "2 : \n" + ] + } + ], + "source": [ + "outputBT(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Mirror BT" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "-1\n", + "-1\n", + "4\n", + "-1\n", + "-1\n", + "5\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "a = inputBT()" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [], + "source": [ + "def mirror(root):\n", + " if root is None:\n", + " return\n", + " a = mirror(root.left)\n", + " b = mirror(root.right)\n", + " root.left,root.right = root.right,root.left" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [], + "source": [ + "mirror(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 : L 5 R 2 \n", + "5 : \n", + "2 : L 4 R 3 \n", + "4 : \n", + "3 : \n" + ] + } + ], + "source": [ + "outputBT(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Balanced Tree or Not" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "T.C = O(n)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + "def height(root):\n", + " if root is None:\n", + " return 0\n", + " return 1+max(height(root.left),height(root.right))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "[best case] T.C = 2T(n/2) + 2n => 2T(n/2) + n = O(nlogn)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "[worst case] T.C = n+ T(n-1) = O(n^2)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [], + "source": [ + "def balanced(root):\n", + " if root is None:\n", + " return True\n", + " # to check for root\n", + " lh = height(root.left)\n", + " rh = height(root.right)\n", + " if abs(lh-rh)<=1:\n", + " return True\n", + " else:\n", + " return False\n", + " # to check for other elements\n", + " isLeftBalanced = balanced(root.left)\n", + " isRightBalanced = balanced(root.right)\n", + " if isLeftBalanced and isRightBalanced:\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "-1\n", + "-1\n", + "4\n", + "-1\n", + "-1\n", + "5\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "a =inputBT()" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "balanced(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Diameter of tree" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "number of edges between 2 farthest nodes" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [], + "source": [ + "def diameter(root):\n", + " if root is None:\n", + " return 0\n", + " left_depth = height(root.left)\n", + " right_depth = height(root.right)\n", + " left_dia = diameter(root.left)\n", + " right_dia = diameter(root.right)\n", + " return max(1+left_depth+right_depth,max(left_dia,right_dia))" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "-1\n", + "-1\n", + "6\n", + "-1\n", + "-1\n", + "7\n", + "-1\n", + "-1\n", + "8\n", + "-1\n", + "-1\n", + "9\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "a = inputBT()" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 : L 2 R 9 \n", + "2 : L 3 R 8 \n", + "3 : L 4 R 7 \n", + "4 : L 5 R 6 \n", + "5 : \n", + "6 : \n", + "7 : \n", + "8 : \n", + "9 : \n" + ] + } + ], + "source": [ + "outputBT(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "diameter(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "6\n", + "-1\n", + "7\n", + "-1\n", + "8\n", + "-1\n", + "-1\n", + "4\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "b = inputBT()" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 : L 2 R 4 \n", + "2 : L 3 R 6 \n", + "3 : L 4 \n", + "4 : L 5 \n", + "5 : \n", + "6 : R 7 \n", + "7 : R 8 \n", + "8 : \n", + "4 : \n" + ] + } + ], + "source": [ + "outputBT(b)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "diameter(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Better way for diameter" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [], + "source": [ + "def height(root, result): \n", + " if (root == None): \n", + " return 0 \n", + " left_height = height(root.left, result) \n", + " right_height = height(root.right, result) \n", + " result[0] = max(result[0], 1 + left_height + right_height) \n", + " return 1 + max(left_height, right_height) " + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [], + "source": [ + "def diameter(root): \n", + " if (root == None): \n", + " return 0\n", + " result = [-999999999999]\n", + " height_of_tree = height(root, result) \n", + " return result[0] " + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "diameter(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Levelwise input" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [], + "source": [ + "from queue import Queue\n", + "def levelwise():\n", + " root = BT(int(input()))\n", + " a = Queue()\n", + " a.put(root)\n", + " while not a.empty():\n", + " l = BT(int(input()))\n", + " r = BT(int(input()))\n", + " temp = a.get()\n", + " if l.data>=0:\n", + " temp.left = l\n", + " a.put(l)\n", + " if r.data>=0:\n", + " temp.right = r\n", + " a.put(r)\n", + " return root" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "a = levelwise()" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 : L 2 R 3 \n", + "2 : L 4 \n", + "4 : \n", + "3 : \n" + ] + } + ], + "source": [ + "outputBT(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Print Levelwise" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [], + "source": [ + "def printLevel(root):\n", + " if root is None:\n", + " return None\n", + " a = Queue()\n", + " a.put(root)\n", + " while not a.empty():\n", + " temp = a.get()\n", + " print(temp.data,end=\" : \")\n", + " if temp.left:\n", + " print(\"L\",temp.left.data,end=\" \")\n", + " a.put(temp.left)\n", + " if temp.right:\n", + " print(\"R\",temp.right.data,end=\" \")\n", + " a.put(temp.right)\n", + " print()" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 : L 2 R 3 \n", + "2 : L 4 \n", + "3 : \n", + "4 : \n" + ] + } + ], + "source": [ + "printLevel(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Preorder and inorder" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": {}, + "outputs": [], + "source": [ + "from queue import Queue\n", + "class BT:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.left = None\n", + " self.right = None\n", + "def outputBT(root):\n", + " a = Queue()\n", + " a.put(root)\n", + " while not a.empty():\n", + " temp = a.get()\n", + " print(temp.data,end= \" : \")\n", + " if temp.left:\n", + " print(\"L\",temp.left.data,end=\" \")\n", + " a.put(temp.left)\n", + " if temp.right:\n", + " print(\"R\",temp.right.data,end =\" \")\n", + " a.put(temp.right)\n", + " print()" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": {}, + "outputs": [], + "source": [ + "def pre_in(preorder,inorder):\n", + " if len(preorder) == 0:\n", + " return None\n", + " rootData = preorder[0]\n", + " root = BT(rootData)\n", + " ind = inorder.index(rootData)\n", + " left_in = inorder[0:ind]\n", + " right_in = inorder[ind+1:]\n", + " size_l = len(left_in)\n", + " left_pre = preorder[1:size_l+1]\n", + " right_pre = preorder[size_l+1:]\n", + " left_tree= pre_in(left_pre,left_in)\n", + " right_tree = pre_in(right_pre,right_in)\n", + " root.left = left_tree\n", + " root.right = right_tree\n", + " return root" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": {}, + "outputs": [], + "source": [ + "a = pre_in([1 ,2, 4 ,5 ,3 ,6, 7 ],[4 ,2 ,5 ,1 ,6 ,3 ,7 ])" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 : L 2 R 3 \n", + "2 : L 4 R 5 \n", + "3 : L 6 R 7 \n", + "4 : \n", + "5 : \n", + "6 : \n", + "7 : \n" + ] + } + ], + "source": [ + "outputBT(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Postorder and inorder" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": {}, + "outputs": [], + "source": [ + "def post_in(postorder,inorder):\n", + " if len(postorder) == 0:\n", + " return None\n", + " rootData = postorder[-1]\n", + " root = BT(rootData)\n", + " ind = inorder.index(rootData)\n", + " left_inorder = inorder[0:ind]\n", + " right_inorder = inorder[ind+1:]\n", + " l_size = len(left_inorder)\n", + " \n", + " left_postorder = postorder[:l_size]\n", + " right_postorder = postorder[l_size:-1]\n", + " \n", + " left_tree = post_in(left_postorder,left_inorder)\n", + " right_tree = post_in(right_postorder,right_inorder)\n", + " root.left = left_tree\n", + " root.right =right_tree\n", + " return root" + ] + }, + { + "cell_type": "code", + "execution_count": 121, + "metadata": {}, + "outputs": [], + "source": [ + "a = post_in([4 ,5 ,2,6, 7 ,3 ,1 ],[4 ,2, 5 ,1 ,6 ,3 ,7 ])" + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 : L 2 R 3 \n", + "2 : L 4 R 5 \n", + "3 : L 6 R 7 \n", + "4 : \n", + "5 : \n", + "6 : \n", + "7 : \n" + ] + } + ], + "source": [ + "outputBT(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Duplicate node addition at left" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "-1\n", + "-1\n", + "4\n", + "-1\n", + "-1\n", + "5\n", + "6\n", + "-1\n", + "-1\n", + "7\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "a = inputBT()" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "metadata": {}, + "outputs": [], + "source": [ + "def duplicate(root):\n", + " if root is None:\n", + " return\n", + " duplicate(root.left)\n", + " duplicate(root.right)\n", + " duplicateData= root.data\n", + " duplicateNode = BT(duplicateData)\n", + " duplicateNode.left = root.left\n", + " root.left = duplicateNode\n", + " return root" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "metadata": {}, + "outputs": [], + "source": [ + "b = duplicate(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 125, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 : L 1 R 3 \n", + "1 : L 2 \n", + "3 : L 3 R 7 \n", + "2 : L 2 R 5 \n", + "3 : L 6 \n", + "7 : L 7 \n", + "2 : L 4 \n", + "5 : L 5 \n", + "6 : L 6 \n", + "7 : \n", + "4 : L 4 \n", + "5 : \n", + "6 : \n", + "4 : \n" + ] + } + ], + "source": [ + "outputBT(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Revision Duplicate" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "from queue import Queue" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "class BT:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.left = None\n", + " self.right = None\n", + "def inputBT():\n", + " rootData = int(input())\n", + " if rootData == -1:\n", + " return\n", + " root = BT(rootData)\n", + " a = Queue()\n", + " a.put(root)\n", + " while not a.empty():\n", + " temp = a.get()\n", + " l = BT(int(input()))\n", + " r = BT(int(input()))\n", + " if l.data !=-1:\n", + " temp.left = l\n", + " a.put(l)\n", + " if r.data != -1:\n", + " temp.right = r\n", + " a.put(r)\n", + " return root\n", + "def outputBT(root):\n", + " if root is None:\n", + " return None\n", + " a = Queue()\n", + " a.put(root)\n", + " while not a.empty():\n", + " temp = a.get()\n", + " print(temp.data,end=\" : \")\n", + " if temp.left:\n", + " print(\"L\",temp.left.data,end=\" \")\n", + " a.put(temp.left)\n", + " if temp.right:\n", + " print(\"R\",temp.right.data,end=\" \")\n", + " a.put(temp.right)\n", + " print()" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "def duplicate(root):\n", + " if root is None:\n", + " return\n", + " duplicate(root.left)\n", + " duplicate(root.right)\n", + " duplicateData = root.data\n", + " duplicateNode = BT(duplicateData)\n", + " duplicateNode.left = root.left\n", + " root.left = duplicateNode" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "a = inputBT()" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 : L 2 R 3 \n", + "2 : L 4 R 5 \n", + "3 : L 6 R 7 \n", + "4 : L 8 \n", + "5 : \n", + "6 : \n", + "7 : \n", + "8 : \n" + ] + } + ], + "source": [ + "outputBT(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "duplicate(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 : L 1 R 3 \n", + "1 : L 2 \n", + "3 : L 3 R 7 \n", + "2 : L 2 R 5 \n", + "3 : L 6 \n", + "7 : L 7 \n", + "2 : L 4 \n", + "5 : L 5 \n", + "6 : L 6 \n", + "7 : \n", + "4 : L 4 \n", + "5 : \n", + "6 : \n", + "4 : \n" + ] + } + ], + "source": [ + "outputBT(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "def min_max(root):\n", + " if root is None:\n", + " return 1000000,0\n", + " l_min,l_max = min_max(root.left)\n", + " r_min,r_max = min_max(root.right)\n", + " minimum = min(root.data,l_min,r_min)\n", + " maximum = max(root.data,l_max,r_max)\n", + " return minimum,maximum" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 8)" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "min_max(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n", + "3\n", + "10\n", + "1\n", + "6\n", + "-1\n", + "14\n", + "-1\n", + "-1\n", + "4\n", + "7\n", + "13\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "a = inputBT()" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8 : L 3 R 10 \n", + "3 : L 1 R 6 \n", + "10 : R 14 \n", + "1 : \n", + "6 : L 4 R 7 \n", + "14 : L 13 \n", + "4 : \n", + "7 : \n", + "13 : \n" + ] + } + ], + "source": [ + "outputBT(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 14)" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "min_max(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Level wise Traversal" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from queue import Queue" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "class BT:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.left = None\n", + " self.right = None\n", + "def inputBT():\n", + " n = int(input())\n", + " if n == -1 :\n", + " return\n", + " root = BT(n)\n", + " a = Queue()\n", + " a.put(root)\n", + " while not a.empty():\n", + " temp = a.get()\n", + " l = BT(int(input()))\n", + " r = BT(int(input()))\n", + " if l.data !=-1:\n", + " temp.left = l\n", + " a.put(l)\n", + " if r.data != -1:\n", + " temp.right = r\n", + " a.put(r)\n", + " return root" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [], + "source": [ + "def output_traversal(root):\n", + " arr = []\n", + " arr.append(root)\n", + " while arr:\n", + " count = len(arr)\n", + " while count > 0:\n", + " temp = arr.pop(0)\n", + " print(temp.data, end = ' ')\n", + " if temp.left:\n", + " arr.append(temp.left)\n", + " if temp.right:\n", + " arr.append(temp.right)\n", + " count -= 1\n", + " print(' ')" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 \n", + "2 3 \n", + "4 5 6 7 \n" + ] + } + ], + "source": [ + "output_traversal(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "a = inputBT()" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "def outputT(root):\n", + " if root is None:\n", + " return None\n", + " arr = []\n", + " arr.append(root)\n", + " while arr:\n", + " count = len(arr)\n", + " while count>0:\n", + " temp = arr.pop(0)\n", + " print(temp.data,end = \" \")\n", + " if temp.left:\n", + " arr.append(temp.left)\n", + " if temp.right:\n", + " arr.append(temp.right)\n", + " count-=1\n", + " print()\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 \n", + "2 3 \n", + "4 5 6 7 \n" + ] + } + ], + "source": [ + "outputT(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Path sum root" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [], + "source": [ + "def path_sum(root,k,string):\n", + " if root is None:\n", + " return \n", + " string += str(root.data)\n", + " k-= root.data\n", + " if k == 0:\n", + " for i in string:\n", + " print(i,end=\" \")\n", + " return\n", + " path_sum(root.left,k,string)\n", + " path_sum(root.right,k,string)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 3 6 " + ] + } + ], + "source": [ + "path_sum(a,10,\"\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Print all nodes at distance k from target node" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from queue import Queue" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "class BT:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.right = None\n", + " self.left = None\n", + " \n", + "def inputBT():\n", + " rootData = int(input())\n", + " if rootData == -1:\n", + " return\n", + " root = BT(rootData)\n", + " a = Queue()\n", + " a.put(root)\n", + " while not a.empty():\n", + " temp = a.get()\n", + " l = BT(int(input()))\n", + " r = BT(int(input()))\n", + " if l.data !=-1:\n", + " temp.left = l\n", + " a.put(l)\n", + " if r.data != -1:\n", + " temp.right = r\n", + " a.put(r)\n", + " return root\n", + "def outputBT(root):\n", + " if root is None:\n", + " return\n", + " a = Queue()\n", + " a.put(root)\n", + " while not a.empty():\n", + " temp = a.get()\n", + " print(temp.data,end=\" : \")\n", + " if temp.left:\n", + " print(\"L\",temp.left.data,end=\" \")\n", + " a.put(temp.left)\n", + " if temp.right:\n", + " print(\"R\",temp.right.data,end =\" \")\n", + " a.put(temp.right)\n", + " print()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "def printDownNodes(root,k):\n", + " if root is None:\n", + " return \n", + " if k == 0:\n", + " print(root.data)\n", + " return\n", + " printDownNodes(root.left,k-1)\n", + " printDownNodes(root.right,k-1)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [], + "source": [ + "def printNodes(root,target,k):\n", + " if root is None:\n", + " return -1\n", + " if root.data == target:\n", + " printDownNodes(root,k) \n", + " return 0 \n", + " \n", + " #for left subtree\n", + " left_subtree = printNodes(root.left,target,k)\n", + " if left_subtree != -1:\n", + " if left_subtree +1 == k:\n", + " print(root.data)\n", + " else:\n", + " printDownNodes(root.right,k-left_subtree-2)\n", + " return left_subtree +1\n", + " \n", + " #for right subtree\n", + " right_subtree = printNodes(root.right.target,k)\n", + " if right_subtree != -1:\n", + " if right_subtree+1 == k:\n", + " print(root.data)\n", + " else:\n", + " printDownNodes(root.left,k-right_subtree-2)\n", + " return right_subtree+1\n", + " print(left_subtree,right_subtree)\n", + " return -1" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "5\n", + "1\n", + "6\n", + "2\n", + "0\n", + "8\n", + "-1\n", + "-1\n", + "7\n", + "4\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "a = inputBT()" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 : L 5 R 1 \n", + "5 : L 6 R 2 \n", + "1 : L 0 R 8 \n", + "6 : \n", + "2 : L 7 R 4 \n", + "0 : \n", + "8 : \n", + "7 : \n", + "4 : \n" + ] + } + ], + "source": [ + "outputBT(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n", + "2\n", + "0\n", + "8\n" + ] + } + ], + "source": [ + "printDownNodes(a,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n", + "4\n", + "1\n" + ] + }, + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "printNodes(a,5,2)" + ] + } + ], + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/Python/Complete OOPS.ipynb b/Python/Complete OOPS.ipynb new file mode 100644 index 0000000..e96e25f --- /dev/null +++ b/Python/Complete OOPS.ipynb @@ -0,0 +1,1829 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Class\n", + "\n", + "Usefull Functions - \n", + "1. getattr()\n", + "2. delattr()\n", + "3. hasattr()" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "class student:\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "s1 = student()\n", + "s2 = student()\n", + "s1.name = \"Sudhanshu\"\n", + "s2.number = 1" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'name': 'Sudhanshu'}" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s1.__dict__" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'number': 1}" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s2.__dict__" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hasattr(s1,\"name\")" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hasattr(s2,\"name\")" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hasattr(s2,\"number\")" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Sudhanshu'" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "getattr(s1,\"name\")" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "getattr(s1,\"number\",0)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{}" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "delattr(s2,\"number\")\n", + "s2.__dict__" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## init" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "class Student:\n", + " def __init__(self,name,batch):\n", + " self.name = name\n", + " self.batch = batch" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "s1 = Student(\"Sudhanshu\",1)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'name': 'Sudhanshu', 'batch': 1}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s1.__dict__" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Static method" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "class Student():\n", + " def info(self):\n", + " self.name = \"Sudhanshu\"\n", + " self.batch = 1\n", + " def print_info(self):\n", + " print(self.name, self.batch)\n", + " \n", + " @staticmethod\n", + " def welcome_to_school():\n", + " print(\"welcome to school\")" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "s1 = Student()" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{}" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s1.__dict__" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sudhanshu 1\n" + ] + } + ], + "source": [ + "s1.info()\n", + "Student.print_info(s1)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'name': 'Sudhanshu', 'batch': 1}" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s1.__dict__" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sudhanshu 1\n" + ] + } + ], + "source": [ + "s1.print_info()\n", + "#Student.print_info(s1)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "welcome to school\n" + ] + } + ], + "source": [ + "s1.welcome_to_school()" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "welcome to school\n" + ] + } + ], + "source": [ + "Student.welcome_to_school()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Predict the output" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "class Student:\n", + " def __init__(self):\n", + " self.name = \"Sudhanshu\"\n", + " self.age = 21\n", + " def print_info(self):\n", + " print(self.name, self.age)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sudhanshu 21\n" + ] + } + ], + "source": [ + "s = Student()\n", + "s.print_info()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Fraction Class" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [], + "source": [ + "class Fraction:\n", + " def __init__(self,numerator=0,denominator=1):\n", + " self.numerator = numerator\n", + " self.denominator = denominator\n", + " \n", + " def print_fraction(self):\n", + " if self.numerator == 0:\n", + " print(0)\n", + " elif self.denominator == 1:\n", + " print(self.numerator)\n", + " else:\n", + " print(f'{self.numerator}/{self.denominator}')\n", + " \n", + " def simplify(self):\n", + " gcd = min(self.numerator,self.denominator)\n", + " while gcd>1:\n", + " if self.numerator%gcd == 0 and self.denominator%gcd == 0:\n", + " break\n", + " gcd -=1\n", + " if gcd > 1: \n", + " self.numerator = self.numerator//gcd\n", + " self.denominator = self.denominator//gcd\n", + " def add(self,obj):\n", + " self.numerator = self.numerator*obj.denominator + obj.numerator*self.denominator\n", + " self.denominator = self.denominator*obj.denominator\n", + " self.simplify()\n", + " \n", + " def multiply(self,obj):\n", + " self.numerator = self.numerator*obj.numerator\n", + " self.denominator = self.denominator*obj.denominator\n", + " self.simplify()" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [], + "source": [ + "f1 = Fraction(1,2)\n", + "f2 = Fraction(2,3)" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [], + "source": [ + "f1.multiply(f2)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1/3\n" + ] + } + ], + "source": [ + "f1.print_fraction()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Complex Number" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": {}, + "outputs": [], + "source": [ + "class ComplexNumber:\n", + " def __init__(self,real=0,imaginary=0):\n", + " self.real = real\n", + " self.imaginary = imaginary\n", + " \n", + " def print_complex(self):\n", + " if self.real == 0 and self.imaginary == 0:\n", + " print(0)\n", + " elif self.real == 0:\n", + " print(f'{self.imaginary}i')\n", + " elif self.imaginary == 0:\n", + " print(f'{self.real}')\n", + " else:\n", + " print(f'{self.real} + {self.imaginary}i')\n", + " \n", + " def add(self,obj):\n", + " self.real += obj.real\n", + " self.imaginary += obj.imaginary\n", + " \n", + " def multiply(self,obj):\n", + " first = (self.real*obj.real) - (self.imaginary*obj.imaginary)\n", + " second = (self.real*obj.imaginary) + (self.imaginary*obj.real)\n", + " self.real = first\n", + " self.imaginary = second" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 0\n", + "0 0\n", + "1\n", + "0\n" + ] + } + ], + "source": [ + "input1 = [int(x) for x in input().split()]\n", + "obj1 = ComplexNumber(input1[0],input1[1])\n", + "input2 = [int(x) for x in input().split()]\n", + "obj2 = ComplexNumber(input2[0],input2[1])\n", + "option = int(input())\n", + "if option == 1:\n", + " obj1.add(obj2)\n", + "else:\n", + " obj1.multiply(obj2)\n", + "obj1.print_complex()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Class Methods" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "from datetime import date\n", + "class Student:\n", + " def __init__(self,name,age):\n", + " self.name = name\n", + " self.age = age\n", + " \n", + " @classmethod\n", + " def CalculateAge(cls,name,birthyear):\n", + " return cls(name,date.today().year - birthyear)\n", + " \n", + " def print_info(self):\n", + " print(self.name , self.age)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "s = Student.CalculateAge('Sudhanshu',2000)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sudhanshu 21\n" + ] + } + ], + "source": [ + "s.print_info()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Private variable" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "class Student:\n", + " def __init__(self,name,age):\n", + " self.__name = name\n", + " self.age = age\n", + " def print_info(self):\n", + " print(self.__name)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "s = Student('Sudhanshu',20)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sudhanshu\n" + ] + }, + { + "data": { + "text/plain": [ + "'Sudhanshu'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.print_info()\n", + "s._Student__name" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Complex number revision" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "class Complex_Number:\n", + " def __init__(self,real =0,imaginary =0 ):\n", + " self.__real = real\n", + " self.__imaginary = imaginary\n", + " \n", + " def add(self,obj):\n", + " self.__real +=obj.__real\n", + " self.__imaginary += obj.__imaginary\n", + " \n", + " def multiply(self,obj):\n", + " first = (self.__real*obj.__real) - (self.__imaginary*obj.__imaginary)\n", + " second = (self.__real*obj.__imaginary) + (obj.__real*self.__imaginary)\n", + " self.__real = first\n", + " self.__imaginary = second\n", + " \n", + " def output(self):\n", + " if self.__real != 0 and self.__imaginary !=0:\n", + " print(f'{self.__real} + {self.__imaginary}i')\n", + " elif self.__real == 0:\n", + " print(f'{self.__real}')\n", + " elif self.__imaginary == 0:\n", + " print(f'{self.__imaginary}i')\n", + " else:\n", + " print(0)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "a = Complex_Number(4,5)\n", + "b = Complex_Number(6,7)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-11 + 58i\n" + ] + } + ], + "source": [ + "a.multiply(b)\n", + "a.output()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Static method revision" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [], + "source": [ + "from datetime import date\n", + "class Student:\n", + " def __init__(self,name,age):\n", + " self.name = name\n", + " self.age = age\n", + " \n", + " @classmethod\n", + " def birthYear(cls,name,year):\n", + " return cls(name,date.today().year - year)\n", + " \n", + " def output(self):\n", + " print(self.name,self.age)\n", + " \n", + " @staticmethod\n", + " def isTeen(age):\n", + " return age>18" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [], + "source": [ + "s = Student.birthYear(\"Sudhanshu\",2000)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sudhanshu 21\n" + ] + } + ], + "source": [ + "s.output()" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.isTeen(20)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Inheritence" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [], + "source": [ + "class Vehicle:\n", + " def __init__(self,color,maxSpeed):\n", + " self.color = color\n", + " self.__maxSpeed = maxSpeed\n", + " \n", + "class Car(Vehicle):\n", + " def __init__(self,color,maxSpeed,wheels):\n", + " super().__init__(color,maxSpeed)\n", + " self.wheels = wheels\n", + " def Output(self):\n", + " print(self.color,self._Vehicle__maxSpeed,self.wheels)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "a = Car(\"red\",120,4)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "red 120 4\n" + ] + } + ], + "source": [ + "a.Output()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Getter and Setter" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "class Vehicle:\n", + " def __init__(self,color,maxSpeed):\n", + " self.color = color\n", + " self.__maxSpeed = maxSpeed\n", + " \n", + " def getMaxSpeed(self):\n", + " return self.__maxSpeed\n", + " def setMaxSpeed(self,maxSpeed):\n", + " self.__maxSpeed = maxSpeed\n", + "\n", + "class Bike(Vehicle):\n", + " def __init__(self,color,maxSpeed,wheels):\n", + " super().__init__(color,maxSpeed)\n", + " self.wheels = wheels\n", + " \n", + " def output(self):\n", + " print(self.color,self.getMaxSpeed(),self.wheels)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [], + "source": [ + "a = Bike(\"black\",30,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "black 30 2\n" + ] + } + ], + "source": [ + "a.output()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Printing from parent class" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [], + "source": [ + "class Vehicle:\n", + " def __init__(self,color,maxSpeed):\n", + " self.color = color\n", + " self.__maxSpeed = maxSpeed\n", + " \n", + " def print_from_parent(self):\n", + " print(self.color,self.__maxSpeed,end = \" \")\n", + " \n", + "class Cycle(Vehicle):\n", + " def __init__(self,color,maxSpeed,wheel):\n", + " super().__init__(color,maxSpeed)\n", + " self.wheel = wheel\n", + " \n", + " def output(self):\n", + "# self.print_from_parent()\n", + " super().print_from_parent()\n", + " print(self.wheel)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [], + "source": [ + "c = Cycle(\"white\",5,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "white 5 2\n" + ] + } + ], + "source": [ + "c.output()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Polymorphism" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### Taking many forms (method overiding)\n", + "> the class will search for the method into its own class first and if unable then into parent class" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "class Vehicle:\n", + " def __init__(self,color):\n", + " self.__color = color\n", + " \n", + " def output(self):\n", + " print(self.__color)\n", + " \n", + "class Truck(Vehicle):\n", + " def __init__(self,color,wheels):\n", + " super().__init__(color)\n", + " self.wheels = wheels\n", + " \n", + " def output(self):\n", + " Vehicle.output(self)\n", + " print(self.wheels)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "T = Truck(\"blue\",6)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "blue\n", + "6\n" + ] + } + ], + "source": [ + "T.output()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Protected Members" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "class Vehicle:\n", + " def __init__(self,color,maxSpeed):\n", + " self.color = color\n", + " self._maxSpeed = maxSpeed\n", + " \n", + " def output(self):\n", + " print(self.color, self._maxSpeed,end = \" \")\n", + " \n", + "class Cycle(Vehicle):\n", + " def __init__(self,color,maxSpeed,wheel):\n", + " super().__init__(color,maxSpeed)\n", + " self.wheel = wheel\n", + " \n", + " def print_output(self):\n", + "# self.output()\n", + " print(self.color,self._maxSpeed,end = \" \")\n", + " print(self.wheel)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "b1 = Cycle(\"red\",10,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b1._maxSpeed" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "#__str__" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "class Circle:\n", + " def __init__(self,radius):\n", + " self.radius = radius\n", + " \n", + " def __str__(self):\n", + " return \"This is a circle class\"" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "c1 = Circle(14)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is a circle class\n" + ] + } + ], + "source": [ + "print(c1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## def str revision" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "class Circle():\n", + " def __init__(self,radius):\n", + " self._radius = radius\n", + " \n", + " def __str__(self):\n", + " return \"It is a circle class\"" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "c = Circle(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "It is a circle class\n" + ] + } + ], + "source": [ + "print(c)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c._radius" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Multiple base classes" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "class Father:\n", + " def __init__(self,name):\n", + " self.name = name\n", + " def output(self):\n", + " print(self.name)\n", + "class Mother:\n", + " @staticmethod\n", + " def output():\n", + " print(\"Mother called\")\n", + "class Child(Father,Mother):\n", + " def __init__(self):\n", + " self.name = 'Rohan'\n", + " def callDad(self):\n", + " super().__init__('Ajay')" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "c = Child()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Rohan\n" + ] + } + ], + "source": [ + "c.output()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# MRO - Method Resolution Order" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "class Father:\n", + " def __init__(self):\n", + " self.name = \"Named by father\"\n", + "\n", + "class Mother:\n", + " def __init__(self):\n", + " self.name = \"Named by mother\"\n", + " \n", + "class Child(Father,Mother):\n", + " def __init__(self):\n", + " super().__init__()\n", + " \n", + " def output(self):\n", + " print(self.name)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "c = Child()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Named by father\n" + ] + } + ], + "source": [ + "c.output()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[__main__.Child, __main__.Father, __main__.Mother, object]" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Child.mro()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Operator Overloading" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "class Point:\n", + " def __init__(self,x,y):\n", + " self.x =x \n", + " self.y =y\n", + " def __str__(self):\n", + " return f'The x point is {self.x} and y is {self.y}'\n", + " def __add__(self,point):\n", + " return Point(self.x + point.x, self.y + point.y)\n", + " \n", + " def __lt__(self,point):\n", + " return math.sqrt(self.x**2 + self)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "p1 = Point(10,20)\n", + "p2 = Point(5,10)\n", + "p3 = p1+ p2" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The x point is 15 and y is 30\n" + ] + } + ], + "source": [ + "print(p3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Revision Operator overloading" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "class Point:\n", + " def __init__(self,x,y):\n", + " self.x = x\n", + " self.y = y\n", + " \n", + " def __add__(self,obj):\n", + " return Point(self.x + obj.x, self.y + obj.y)\n", + " \n", + " def __str__(self):\n", + " return f'x point is {self.x} and y point is {self.y}'" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "p = Point(10,20)\n", + "p1 = Point(20,30)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "p2 = p +p1" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.Point at 0x1d7c318c490>" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "p2" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x point is 30 and y point is 50\n" + ] + } + ], + "source": [ + "print(p2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Abstract Class" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Car created\n", + "Automobile start\n" + ] + } + ], + "source": [ + "from abc import ABC,abstractmethod\n", + "\n", + "class Automobile(ABC):\n", + " def __init__(self):\n", + " print(\"Automobile created\")\n", + " @abstractmethod \n", + " def start(self):\n", + " print(\"Automobile start\")\n", + " \n", + "class Car(Automobile):\n", + " def __init__(self):\n", + " print(\"Car created\")\n", + " def start(self):\n", + " super().start()\n", + " \n", + "c = Car()\n", + "c.start()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## More in abstract class" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "from abc import ABC,abstractmethod\n", + "\n", + "class Automobile(ABC):\n", + " def __init__(self,color,wheels):\n", + " self.color = color\n", + " self.wheels = wheels\n", + " \n", + " @abstractmethod\n", + " def start(self):\n", + " print(\"Automobile start\")\n", + " \n", + " @abstractmethod\n", + " def stop(self):\n", + " print(\"Automobile stop\")\n", + " \n", + "class Car(Automobile):\n", + " def __init__(self,color,wheel,speed):\n", + " Automobile.__init__(self,color,wheel)\n", + " self.speed = speed\n", + " \n", + " def start(self):\n", + " super().start()\n", + " def stop(self):\n", + " print(\"car stops\")" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "c = Car(\"red\",4,40)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Automobile start\n" + ] + } + ], + "source": [ + "c.start()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "car stops\n" + ] + } + ], + "source": [ + "c.stop()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Error Handling" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the numberator: 1\n", + "Enter the denominator: 0\n", + "value should be integer and denominator should not be zero\n", + "Enter the numberator: 1\n", + "Enter the denominator: abc\n", + "value should be integer and denominator should not be zero\n", + "Enter the numberator: 1\n", + "Enter the denominator: -2\n", + "-0.5\n" + ] + } + ], + "source": [ + "while True:\n", + " try:\n", + " num = int(input('Enter the numberator: '))\n", + " deno = int(input('Enter the denominator: '))\n", + " output = num/deno\n", + " print(output)\n", + " break\n", + "# except ValueError:\n", + "# print('Numerator and denominator must be integer')\n", + "# except ZeroDivisionError:\n", + "# print(\"Denominator cant be zero\")\n", + "# except:\n", + "# print(\"Kindly type correct values\")\n", + " except (ValueError,ZeroDivisionError):\n", + " print(\"value should be integer and denominator should not be zero\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the numerator: 5\n", + "Enter the denominator: 0\n", + "deno cant be zero\n" + ] + } + ], + "source": [ + "while True:\n", + " try:\n", + " num = int(input('Enter the numerator: '))\n", + " deno = int(input('Enter the denominator: '))\n", + " if deno ==0:\n", + " raise ZeroDivisionError\n", + " output = num/deno\n", + " print(output)\n", + " except ValueError:\n", + " print(\"ValueError\")\n", + " except ZeroDivisionError:\n", + " print(\"deno cant be zero\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# error handling and inheritance" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "0\n", + "Zero division not allowed\n", + "5\n", + "5\n", + "1.0\n" + ] + } + ], + "source": [ + "class DividingByZero(ZeroDivisionError):\n", + " pass\n", + "\n", + "while True:\n", + " try:\n", + " a= int(input())\n", + " b= int(input())\n", + " if b == 0:\n", + " raise DividingByZero\n", + " print(a/b)\n", + " break\n", + " except ZeroDivisionError:\n", + " print(\"Zero division not allowed\")\n", + " except DividingByZero:\n", + " print(\"Dividing\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# else and finally " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "0\n", + "Denominator cant be zero\n", + "\"finally\" executed\n", + "5\n", + "5\n", + "1.0\n", + "\"finally\" executed\n" + ] + } + ], + "source": [ + "while True:\n", + " try:\n", + " a= int(input())\n", + " b= int(input())\n", + " if b == 0:\n", + " raise ZeroDivisionError()\n", + " except ZeroDivisionError:\n", + " print(\"Denominator cant be zero\")\n", + " else:\n", + " print(a/b)\n", + " break\n", + " finally:\n", + " print(\"\\\"finally\\\" executed\")" + ] + }, + { + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/Python/Linked List.ipynb b/Python/Linked List.ipynb new file mode 100644 index 0000000..e943d43 --- /dev/null +++ b/Python/Linked List.ipynb @@ -0,0 +1,2193 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Create a node" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.next = None" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "13\n", + "15\n", + "None\n" + ] + } + ], + "source": [ + "a = Node(13)\n", + "b= Node(15)\n", + "a.next = b\n", + "print(a.data)\n", + "print(a.next.data)\n", + "print(b.next)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Take input for node" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.next = None" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "TC for input_node() = \n", + "\n", + "TC = k1*n + k2*n = O(n)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "def input_node():\n", + " node = [int(ele) for ele in input().split()]\n", + " head = None\n", + " for ele in node:\n", + " if ele == -1:\n", + " break\n", + " NewNode = Node(ele)\n", + " if head is None:\n", + " head = NewNode\n", + " Original_Head = NewNode\n", + " else:\n", + " head.next = NewNode\n", + " head = NewNode\n", + " \n", + " return Original_Head" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 -1\n" + ] + } + ], + "source": [ + "a = input_node()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.Node at 0x1c6b35ebc70>" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.Node at 0x1c6b35f3f70>" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.next" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.data" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.next.next.data" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.next = None" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "def input_ll():\n", + " arr = [int(a) for a in input().split()]\n", + " head = None\n", + " for i in arr:\n", + " NewNode = Node(i)\n", + " if head is None:\n", + " head = NewNode\n", + " original_head = NewNode\n", + " else:\n", + " head.next = NewNode\n", + " head = NewNode\n", + " return original_head" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 4 3 2\n" + ] + } + ], + "source": [ + "a = input_ll()" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.Node at 0x296e1471520>" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.next" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.data" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.next.data" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.next.next.data" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [], + "source": [ + "def output_ll(a):\n", + " while a is not None:\n", + " print(a.data,end = '->')\n", + " a = a.next\n", + " print(None)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1->4->3->2->None\n" + ] + } + ], + "source": [ + "output_ll(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Finding Length of LL" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "def find_len(a):\n", + " counter = 0\n", + " while a is not None:\n", + " counter+=1\n", + " a = a.next\n", + " return counter" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "find_len(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Print i th element of LL" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "def print_i(a,index):\n", + " counter = 0\n", + " while counter \")\n", + " head = head.next\n", + " print(None)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4 5 6 7 8 9\n" + ] + } + ], + "source": [ + "a = input_LL()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4 -> 5 -> 6 -> 7 -> 8 -> 9 -> None\n" + ] + } + ], + "source": [ + "output_LL(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "#inserting a node at ith position\n", + "def insert_LL(head,index,data):\n", + " counter = 0\n", + " if index == 0:\n", + " ele = Node(data)\n", + " ele.next = head\n", + " head = ele\n", + " return head\n", + " while counter!=index-1:\n", + " if head.next is None:\n", + " ele = Node(data)\n", + " head.next = ele\n", + " return \n", + " head= head.next\n", + " counter+=1\n", + " temp = head.next\n", + " ele = Node(data)\n", + " ele.next = temp\n", + " head.next = ele\n", + " return head" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "a = insert_LL(a,0,12)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "12 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> None\n" + ] + } + ], + "source": [ + "output_LL(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Delete a node at ith position" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "def delete_LL(head,index):\n", + " count = 0\n", + " while count" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "delete_LL(a,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "12 -> 4 -> 6 -> 7 -> 8 -> 9 -> None\n" + ] + } + ], + "source": [ + "output_LL(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Inserting in Linked List recursively" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.next = None\n", + "def insert_LL_recusion(head,i,data):\n", + " if i == 1:\n", + " temp = head.next\n", + " head.next = Node(data)\n", + " head.next.next = temp\n", + " return\n", + " return insert_LL_recusion(head.next,i-1,data)\n", + "\n", + "def input_LL():\n", + " arr = [int(a) for a in input().split()]\n", + " head =None\n", + " tail = None\n", + " for ele in arr:\n", + " newNode = Node(ele)\n", + " if head is None:\n", + " head = newNode\n", + " tail = newNode\n", + " else:\n", + " tail.next = newNode\n", + " tail = newNode\n", + " return head\n", + "def output_LL(head):\n", + " while head is not None:\n", + " print(head.data,end = \" -> \")\n", + " head = head.next\n", + " print(None)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 4 4 4 5\n" + ] + } + ], + "source": [ + "a = input_LL()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 -> 2 -> 3 -> 4 -> 5 -> None\n" + ] + } + ], + "source": [ + "output_LL(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [], + "source": [ + "insert_LL_recusion(a,2,55)" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 -> 4 -> 3 -> 2 -> 1 -> None\n" + ] + } + ], + "source": [ + "output_LL(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Another approach of insertion using recursion" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 4 5\n" + ] + } + ], + "source": [ + "a = input_LL()" + ] + }, + { + "cell_type": "code", + "execution_count": 131, + "metadata": {}, + "outputs": [], + "source": [ + "def insert_recursion(head,i,data):\n", + " if i == 0:\n", + " newNode = Node(data)\n", + " newNode.next = head\n", + " return newNode\n", + " smallHead = insert_recursion(head.next,i-1,data)\n", + " head.next = smallHead\n", + " return head" + ] + }, + { + "cell_type": "code", + "execution_count": 132, + "metadata": {}, + "outputs": [], + "source": [ + "j = insert_recursion(a,2,900)" + ] + }, + { + "cell_type": "code", + "execution_count": 133, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 -> 2 -> 900 -> 3 -> 4 -> 5 -> None\n" + ] + } + ], + "source": [ + "output_LL(j)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Deletion using Recursion" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "def delete_node(head,i):\n", + " if i == 0:\n", + " head = head.next\n", + " return head\n", + " smallHead= delete_node(head.next,i-1)\n", + " head.next = smallHead\n", + " return head" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.Node at 0x1cd9480caf0>" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "delete_node(a,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 -> 2 -> 3 -> 4 -> 5 -> None\n" + ] + } + ], + "source": [ + "output_LL(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Find a node in LL" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "def findNode(head,node):\n", + " counter =0\n", + " while head is not None:\n", + " if head.data == node:\n", + " return counter\n", + " counter +=1\n", + " head = head.next\n", + " else:\n", + " return -1" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "findNode(a,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "findNode(a,1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Append last N to first" + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 4 5\n" + ] + } + ], + "source": [ + "a = input_LL()" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "metadata": {}, + "outputs": [], + "source": [ + "def append_node(head,i):\n", + " counter = 0\n", + " tail = head\n", + " while counter 4 -> 5 -> 1 -> 2 -> None\n" + ] + } + ], + "source": [ + "output_LL(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Clear way to do it" + ] + }, + { + "cell_type": "code", + "execution_count": 174, + "metadata": {}, + "outputs": [], + "source": [ + "def append_better(head,i):\n", + " counter = 0\n", + " tail = head\n", + " while counter 5 -> 1 -> 2 -> 3 -> None\n" + ] + } + ], + "source": [ + "output_LL(c)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Eliminates duplicates from LL" + ] + }, + { + "cell_type": "code", + "execution_count": 178, + "metadata": {}, + "outputs": [], + "source": [ + "def input_arr(arr):\n", + " head = None\n", + " tail = None\n", + " for i in arr:\n", + " newNode = Node(i)\n", + " if head is None:\n", + " head = newNode\n", + " tail = newNode\n", + " else:\n", + " tail.next = newNode\n", + " tail = tail.next\n", + " return head" + ] + }, + { + "cell_type": "code", + "execution_count": 179, + "metadata": {}, + "outputs": [], + "source": [ + "def duplicate_checker():\n", + " arr = [int(n) for n in input().split()]\n", + " uni = []\n", + " for i in arr:\n", + " if i not in uni:\n", + " uni.append(i)\n", + " return input_arr(uni)" + ] + }, + { + "cell_type": "code", + "execution_count": 180, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 4 5 5 5 5 5\n" + ] + } + ], + "source": [ + "b = duplicate_checker()" + ] + }, + { + "cell_type": "code", + "execution_count": 181, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 -> 2 -> 3 -> 4 -> 5 -> None\n" + ] + } + ], + "source": [ + "output_LL(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Print LL in reverse" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 4 5 6\n" + ] + } + ], + "source": [ + "a = input_LL()" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [], + "source": [ + "def print_reverse(head):\n", + " if head is None:\n", + " print(None,end =\" \")\n", + " return\n", + " print_reverse(head.next)\n", + " print(\" <- \" ,head.data, end= \" \")" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None <- 6 <- 5 <- 4 <- 3 <- 2 <- 1 " + ] + } + ], + "source": [ + "print_reverse(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Palindrome LL" + ] + }, + { + "cell_type": "code", + "execution_count": 158, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3\n" + ] + } + ], + "source": [ + "a = input_LL()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### TC for reversing LL = n + n = O(n) but space complexity is also n + n = O(n)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "arr = []\n", + "def reverse_arr(head):\n", + " global arr\n", + " if head is None:\n", + " return arr\n", + " reverse(head.next)\n", + " arr.append(head.data)\n", + "def reverse_ll(arr):\n", + " head = None\n", + " tail = None\n", + " for i in arr:\n", + " newNode = Node(i)\n", + " if head is None:\n", + " head = newNode\n", + " tail = newNode\n", + " else:\n", + " tail.next = newNode\n", + " tail = tail.next\n", + " return head\n", + "\n", + "def reverse(head):\n", + " reverse_arr(head)\n", + " b = reverse_ll(arr)\n", + " return b\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 176, + "metadata": {}, + "outputs": [], + "source": [ + "def check_palindrome(head):\n", + " head2 = reverse(head)\n", + " while head is not None:\n", + " if head.data != head2.data:\n", + " return \"Not a pallindrome\"\n", + " head = head.next\n", + " head2 = head2.next\n", + " else:\n", + " return \"Yes\"" + ] + }, + { + "cell_type": "code", + "execution_count": 155, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Not a pallindrome'" + ] + }, + "execution_count": 155, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "check_palindrome(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Reverse LL" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### T(n) = T(n-1) + n - 1 = O(n^2)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.next = None\n", + "def input_LL():\n", + " arr = [int(a) for a in input().split()]\n", + " head = None\n", + " tail = None\n", + " for i in arr:\n", + " newNode = Node(i)\n", + " if head is None:\n", + " head = newNode\n", + " tail = newNode\n", + " else:\n", + " tail.next = newNode\n", + " tail = newNode\n", + " return head\n", + "\n", + "def output_LL(head):\n", + " while head is not None:\n", + " print(head.data,end =\" -> \")\n", + " head = head.next\n", + " print(None)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 4 5\n" + ] + } + ], + "source": [ + "a = input_LL()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 -> 2 -> 3 -> 4 -> 5 -> None\n" + ] + } + ], + "source": [ + "output_LL(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [], + "source": [ + "def reverse_LL(head):\n", + " if head.next is None:\n", + " return head\n", + " smallHead = reverse_LL(head.next)\n", + " tail = smallHead\n", + " while tail.next is not None:\n", + " tail = tail.next\n", + " tail.next = head\n", + " head.next = None\n", + " return smallHead" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "b = reverse_LL(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 -> 4 -> 3 -> 2 -> 1 -> None\n" + ] + } + ], + "source": [ + "output_LL(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Better Reverse LL" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 4 5\n" + ] + } + ], + "source": [ + "a = input_LL()" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [], + "source": [ + "answer = 0\n", + "def reverse_ll(head):\n", + " if head is None or head.next is None:\n", + " global answer\n", + " answer = head\n", + " return head\n", + " smallHead = reverse_ll(head.next)\n", + " smallHead.next = head\n", + " head.next = None\n", + " return head" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [], + "source": [ + "c= reverse_ll(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 -> 4 -> 3 -> 2 -> 1 -> None\n" + ] + } + ], + "source": [ + "output_LL(answer)" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 4 5\n" + ] + } + ], + "source": [ + "a = input_LL()" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "metadata": {}, + "outputs": [], + "source": [ + "def reverseLL(head):\n", + " if head is None or head.next is None:\n", + " return head,head\n", + " smallHead,smallTail = reverseLL(head.next)\n", + " smallTail.next = head\n", + " head.next = None\n", + " return smallHead,head\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": {}, + "outputs": [], + "source": [ + "head,tail= reverseLL(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 -> 4 -> 3 -> 2 -> 1 -> None\n" + ] + } + ], + "source": [ + "output_LL(head)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Best way for reversing LL" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.next = None\n", + "def input_LL():\n", + " arr = [int(a) for a in input().split()]\n", + " head = None\n", + " tail = None\n", + " for i in arr:\n", + " newNode = Node(i)\n", + " if head is None:\n", + " head = newNode\n", + " tail = newNode\n", + " else:\n", + " tail.next = newNode\n", + " tail = newNode\n", + " return head\n", + "\n", + "def output_LL(head):\n", + " while head is not None:\n", + " print(head.data,end =\" -> \")\n", + " head = head.next\n", + " print(None)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "def reverse_ll(head):\n", + " if head is None or head.next is None:\n", + " return head\n", + " smallHead = reverse_ll(head.next)\n", + " tail = head.next \n", + " tail.next = head\n", + " head.next = None\n", + " return smallHead" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "c = reverse_ll(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 -> 4 -> 3 -> 2 -> 1 -> None\n" + ] + } + ], + "source": [ + "output_LL(c)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Best way to reverse recursively" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.next = None\n", + "\n", + "def inputll():\n", + " arr = [int(a) for a in input().split()]\n", + " head = None\n", + " tail = None\n", + " for i in arr:\n", + " newNode = Node(i)\n", + " if head is None:\n", + " head = newNode\n", + " tail = newNode\n", + " else:\n", + " tail.next = newNode\n", + " tail = newNode\n", + " return head\n", + "\n", + "def outputll(head):\n", + " while head is not None:\n", + " print(head.data,end =\" -> \")\n", + " head = head.next\n", + " print(None)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 4 5\n" + ] + } + ], + "source": [ + "a = inputll()" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 -> 2 -> 3 -> 4 -> 5 -> None\n" + ] + } + ], + "source": [ + "outputll(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [], + "source": [ + "def reversell(head):\n", + " if head is None or head.next is None:\n", + " return head\n", + " smallHead = reversell(head.next)\n", + " temp = head.next\n", + " temp.next = head\n", + " head.next = None\n", + " return smallHead" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "c= reversell(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 -> 4 -> 3 -> 2 -> 1 -> None\n" + ] + } + ], + "source": [ + "outputll(c)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Reverse LL iteratively" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 4 5\n" + ] + } + ], + "source": [ + "a = inputll()" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [], + "source": [ + "def reverse_iter1(head):\n", + " current = head\n", + " previous = None\n", + " while current is not None:\n", + " upcoming = current.next\n", + " current.next = previous\n", + " previous = current\n", + " current = upcoming\n", + " return previous" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [], + "source": [ + "c = reverse_iter1(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 -> 4 -> 3 -> 2 -> 1 -> None\n" + ] + } + ], + "source": [ + "outputll(c)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Mid point of LL" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 4 5\n" + ] + } + ], + "source": [ + "a = inputll()" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [], + "source": [ + "def lengthLL(head):\n", + " counter =0 \n", + " while head is not None:\n", + " counter+=1\n", + " head = head.next\n", + " return counter" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lengthLL(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [], + "source": [ + "def midpoint(head):\n", + " head_copy = head\n", + " len = lengthLL(head_copy)\n", + " if len%2==0:\n", + " mid_point = len//2\n", + " else:\n", + " mid_point = len//2 + 1\n", + " i = 1\n", + " while i\n" + ] + }, + { + "data": { + "text/plain": [ + "<__main__.Node at 0x180f46ba760>" + ] + }, + "execution_count": 135, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "midPoint(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 129, + "metadata": {}, + "outputs": [], + "source": [ + "def Merge(a1,a2,head):\n", + " while a1 is not None and a2 is not None:\n", + " if a1.data n:\n", + " end = mid -1\n", + " search(start,end)\n", + " elif arr[mid] < n:\n", + " start = mid+1\n", + " search(start,end)\n", + " return -1" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": {}, + "outputs": [], + "source": [ + "index = search(0,len(arr)-1)" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "print(index)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Selection Sort " + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": {}, + "outputs": [], + "source": [ + "arr = [5,4,2,6,1]\n", + "for i in range(len(arr)-1):\n", + " min_index = i\n", + " for j in range(i+1,len(arr)):\n", + " if arr[j]arr[i+1]:\n", + " arr[i],arr[i+1] = arr[i+1],arr[i]" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "print(arr)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Insertion Sort " + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [], + "source": [ + "arr = [ 5,4,2,1,3]\n", + "for i in range(1,len(arr)):\n", + " temp = arr[i]\n", + " j = i-1\n", + " while(j>=0 and arr[j] > temp):\n", + " arr[j+1] = arr[j]\n", + " j -= 1\n", + " arr[j+1] = temp" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "print(arr)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Merge 2 arrays " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "arr1 = [1,3,5,7,11]\n", + "arr2 = [2,4,8,10]\n", + "arr3 = []\n", + "i =0\n", + "j=0\n", + "while iarr[j+1]:\n", + " arr[j],arr[j+1] = arr[j+1],arr[j]\n", + "[arr2.append(x) for x in arr if x not in arr2]\n", + "print(arr2[-2])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Check Array Rotation " + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "arr = [10, 20, 30 ,1]\n", + "flag = 0\n", + "for i in range(len(arr)-1):\n", + " if arr[i] > arr[i+1]:\n", + " flag = i+1\n", + "print(flag)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Sort 0 1 2 " + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 0, 0, 1, 1, 2, 2]" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr = [0 ,1 ,2 ,0 ,2 ,0 ,1] \n", + "arr2 = []\n", + "one = 0\n", + "two = 0\n", + "for i in arr:\n", + " if i == 0:\n", + " arr2.append(0)\n", + " elif i==1:\n", + " one +=1\n", + " else:\n", + " two +=1\n", + "for i in range(one):\n", + " arr2.append(1)\n", + "for i in range(two):\n", + " arr2.append(2) \n", + "arr2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Add two arrays " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "arr1 = input()\n", + "arr2 = input()\n", + "add = int(arr1.replace(\" \",\"\")) + int(arr2.replace(\" \",\"\"))\n", + "str(add)" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[5, 7, 9]" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr3 = []\n", + "[arr3.append(int(i)) for i in str(add)]\n", + "arr3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Insertion Sort " + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "arr = [9,8,7,6,5,3]\n", + "for i in range(1,len(arr)):\n", + " temp = arr[i]\n", + " j= i-1\n", + " while j>=0 and arr[j]>temp:\n", + " arr[j+1] = arr[j]\n", + " j -=1\n", + " arr[j+1] = temp" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[3, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Strings" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "name = '''My\n", + "name is \n", + "Sudhanshu'''" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "' '" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "name[7]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'\\n'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "name[2]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'My\\nname is \\nSudhanshu'" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "name" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "My\n", + "name is \n", + "Sudhanshu\n" + ] + } + ], + "source": [ + "print(name)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "str = \"abcde\"" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'ebcde'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "str.replace(\"a\",\"e\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "# Manual way of using replace functionality\n", + "def rep(s):\n", + " str = []\n", + " for i in range(len(s)):\n", + " if s[i] == \"a\":\n", + " str.append(\"d\")\n", + " else:\n", + " str.append(s[i])\n", + " return \"\".join(str)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'dbcddd'" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rep(\"abcdaa\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "#Another method\n", + "def rep2(s):\n", + " str = \"\"\n", + " for i in s:\n", + " if i == \"a\":\n", + " str += \"e\"\n", + " else:\n", + " str += i\n", + " return str" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'ebcsdeee'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rep2(\"abcsdaaa\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Count Vowels consonants and digits" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "def count(s):\n", + " vowels = 0\n", + " consonants = 0\n", + " digits = 0\n", + " spe = 0\n", + " vowl = 'aeiou'\n", + " s = s.lower()\n", + " for i in s:\n", + " if (i >= 'a' and i<= 'z'):\n", + " if i in vowl:\n", + " vowels +=1\n", + " else:\n", + " consonants +=1\n", + " elif (i>='0' and i<='9'):\n", + " digits +=1\n", + " else:\n", + " spe +=1\n", + " return vowels,consonants,digits,spe" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 5, 6, 3)" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count('aAbcd5445@! sp23')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Checking permutation of each other" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Possible\n" + ] + } + ], + "source": [ + "str1= \"sinrtg\" \n", + "str2 = \"string\"\n", + "str3 = []\n", + "[str3.append(i) for i in str2]\n", + "for i in str1:\n", + " if i in str3:\n", + " str3.remove(i)\n", + " else:\n", + " break\n", + "else:\n", + " print(\"Possible\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Remove Consecutive Duplicates" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [], + "source": [ + "str = \"aaabbccaa\"\n", + "str_final =\"\"\n", + "\n", + "for i in range(len(str)-1):\n", + " if str[i] == str[i+1]:\n", + " continue\n", + " else:\n", + " str_final += str[i]\n", + "str_final += str[-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'abca'" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "str_final" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Reverse all Words" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [], + "source": [ + "str = \"Welcome to Coding Ninjas\"\n", + "str = str.split()" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [], + "source": [ + "str_final = []\n", + "for i in str:\n", + " rev_str = \"\"\n", + " for j in range(len(i)-1,-1,-1):\n", + " rev_str += i[j]\n", + " str_final.append(rev_str)" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'emocleW ot gnidoC sajniN'" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\" \".join(str_final)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Remove characters" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "aaabbcbaabcbabcbaa\n", + "a\n" + ] + } + ], + "source": [ + "str = input()\n", + "rep = input()\n", + "new_str = \"\"\n", + "for i in str:\n", + " if i != rep:\n", + " new_str += i" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'bbcbbcbbcb'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_str" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Highest Occuring Character" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a\n" + ] + } + ], + "source": [ + "str = \"abcdeapapqarr\"\n", + "frequency = [0]*26\n", + "for i in str:\n", + " order = ord(i)\n", + " frequency[order - 97]+= 1\n", + "print(chr((frequency.index(max(frequency)))+97))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Compressing Duplicates" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'str' object is not callable", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mcount\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[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0mnew_str\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcount\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'str' object is not callable" + ] + } + ], + "source": [ + "stri = \"aaabbccdsa\"\n", + "new_str = \"\"\n", + "count = 1\n", + "for i in range(len(stri)-1):\n", + " if count ==1:\n", + " new_str += stri[i]\n", + " if stri[i] == stri[i+1]:\n", + " count +=1\n", + " else:\n", + " if count>1:\n", + " new_str += str(count)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'a\\x03\\x04\\x05\\x05\\x05'" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_str" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Pattern" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + " *\n", + " **\n", + " ***\n", + " ****\n", + "*****\n" + ] + } + ], + "source": [ + "n = int(input())\n", + "for i in range(1,n+1):\n", + " for spaces in range(0,n-i):\n", + " print(\" \",end=\"\")\n", + " for stars in range(i):\n", + " print(\"*\",end=\"\")\n", + " print(\"\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Recursion" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Fibonacci Series" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "import sys\n", + "sys.setrecursionlimit(3000)\n", + "def fib(n):\n", + " if n == 1 or n ==2:\n", + " return 1\n", + " fib_n_1 = fib(n-1)\n", + " fib_n_2 = fib(n-2)\n", + " output = fib_n_1 + fib_n_2\n", + " return output" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "fib(2000)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Checking first occurence of 'x'" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "def check(li,x,start=0):\n", + " if start == len(li)-1:\n", + " return -1\n", + " if li[start] == x:\n", + " return start\n", + " return check(li,x,start+1)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-1" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "check([1,2,3,4],8)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "def firstIndex(li,x):\n", + " if len(li) == 0:\n", + " return -1\n", + " if li[0] == x:\n", + " return 0\n", + " smallerList = firstIndex(li[1:],x)\n", + " if smallerList !=-1:\n", + " return smallerList +1" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "firstIndex([1,2,3,3,4],4)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Checking last occurence of 'x'" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [], + "source": [ + "def checkL(li,x,start):\n", + " if start == -1: \n", + " return -1\n", + " if li[start-1] == x:\n", + " return start-1\n", + " return checkL(li,x,start-1)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-1" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "checkLast([1,2,3,4,5,5,5],5)" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "checkL([1,2,3,4,5,5,5,5,5],5,9)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Checking last occurence of 'x' -2" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def lastIndex(li,x):\n", + " if len(li) == 0:\n", + " return -1\n", + " output = lastIndex(li[1:],x)\n", + " if output != -1:\n", + " return output+1\n", + " else:\n", + " if li[0] == x:\n", + " return 0\n", + " else:\n", + " return -1" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lastIndex([1,2,2,3,3,5],2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Replace all the occurence in string" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "def replace(str,a,b,c=\"\"):\n", + " if len(str) == 0:\n", + " print(c)\n", + " return\n", + " if str[0] == a:\n", + " c += b\n", + " else:\n", + " c += str[0]\n", + " replace(str[1:],a,b,c) " + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "heooooooo\n" + ] + } + ], + "source": [ + "replace(\"hellllllo\",'l','o')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Remove X" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [], + "source": [ + "def remove(stri,a):\n", + " if len(stri) == 0:\n", + " return stri\n", + " smallOutput = remove(stri[1:],a)\n", + "# print(smallOutput)\n", + " if stri[0] == a:\n", + " return smallOutput\n", + " else:\n", + " return stri[0] + smallOutput" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'ada'" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "remove(\"axxdax\",\"x\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Replace pi" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [], + "source": [ + "def replace(str):\n", + " if len(str) == 0 or len(str) == 1:\n", + " return str\n", + " if str[0] == \"p\" and str[1] == \"i\":\n", + " smallOutput = replace(str[2:])\n", + " return \"3.14\" + smallOutput\n", + " else:\n", + " smallOutput = replace(str[1:])\n", + " return str[0] + smallOutput" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'ab3.14i3.14'" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "replace(\"abpiipi\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Remove Duplicates Recursively" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def duplicate(str):\n", + " if len(str) == 0 or len(str) == 1:\n", + " return str\n", + " smallOutput = duplicate(str[1:])\n", + " if str[0] == smallOutput:\n", + " return smallOutput\n", + " else:\n", + " str[0]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Binary Search" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [], + "source": [ + "def binarySearch(li,x):\n", + " if len(li) == 0:\n", + " return -1\n", + " q = (len(li)//2)\n", + " print(li)\n", + " if li[q] == x:\n", + " return \"Found\"\n", + " elif len(li) == 1:\n", + " return -1\n", + " elif li[q] > x:\n", + " return binarySearch(li[:q],x)\n", + " elif li[q] < x:\n", + " return binarySearch(li[q:],x)" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 8, 9]\n", + "[4, 5, 8, 9]\n" + ] + }, + { + "data": { + "text/plain": [ + "'Found'" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "binarySearch([1,2,3,4,5,8,9],8)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "def binaryS(li,x,start,end):\n", + " if start > end:\n", + " return -1\n", + " mid = (start+end)//2\n", + "# start = 0\n", + "# end = len(li)-1\n", + " if li[mid] ==x :\n", + " return mid\n", + " elif li[mid] > x:\n", + " return binaryS(li,x,start,mid-1)\n", + " elif li[mid] x:\n", + " return binary(li[:mid],x)\n", + " elif li[mid] < x:\n", + " return binary(li[mid+1:],x)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Not Found'" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "binary([1,2,3],100)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [], + "source": [ + "def binaryB(li,x,start,end):\n", + " if start > end:\n", + " return \"Not Found\"\n", + " mid = (start+end)//2\n", + " if li[mid] == x:\n", + " return \"Found\"\n", + " elif li[mid] >x:\n", + " return binaryB(li,x,start,mid-1)\n", + " elif li[mid] (start+count):\n", + " if li[i] < li[start+count]:\n", + " i+=1\n", + " elif li[j] > li[start+count]:\n", + " j-=1\n", + " else:\n", + " temp = li[i]\n", + " li[i] = li[j]\n", + " li[j] = temp\n", + " i +=1\n", + " j -=1\n", + " return start+count" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [], + "source": [ + "def quickSort(li,start,end):\n", + " if start >= end:\n", + " return\n", + " a = partition(li,start,end)\n", + " quickSort(li,start,a-1)\n", + " quickSort(li,a+1,end)\n", + " return li" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 4, 5, 5, 9]" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "quickSort([2,4,1,5,5,9,0],0,6)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Quick Sort using first element as pivot" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def partition(li,start,end):\n", + " count =0\n", + " for i in range(start+1,end):\n", + " if li[start] > li[i]:\n", + " count +=1\n", + " li[start+count],li[start] = li[start],li[start+count]\n", + " i = start\n", + " j =end\n", + " while i < j:\n", + " if li[i] li[start+count]:\n", + " j-=1\n", + " else:\n", + " li[i],li[j] = li[j],li[i]\n", + " i+=1\n", + " j-=1\n", + " return start+count" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "def quick_sort(li,start,end):\n", + " if start >= end:\n", + " return\n", + " pivot = partition(li,start,end)\n", + " quick_sort(li,start,pivot-1)\n", + " quick_sort(li,pivot+1,end)\n", + " return li" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 3, 4, 5, 7, 9]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "quick_sort([4,5,3,7,9,2],0,5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tower of hanoi" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "def tower(n,source,helper,destination):\n", + " if n == 1:\n", + " print(f\"moving 1st disk from {source} to {destination}\")\n", + " return\n", + " tower(n-1,source,destination,helper)\n", + " print(f\"moving {n}th disk from {source} to {destination}\")\n", + " tower(n-1,helper,source,destination)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "moving 1st disk from A to C\n", + "moving 2th disk from A to B\n", + "moving 1st disk from C to B\n", + "moving 3th disk from A to C\n", + "moving 1st disk from B to A\n", + "moving 2th disk from B to C\n", + "moving 1st disk from A to C\n" + ] + } + ], + "source": [ + "tower(3,'A','B','C')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Geometric Sum" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "def geometricSum(n):\n", + " if n ==0:\n", + " return 1\n", + " s = geometricSum(n-1)\n", + " return (s + 1/(2**n))" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1.9375" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "geometricSum(4)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Palindrome" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [], + "source": [ + "def palindrome(str):\n", + " if len(str) == 1:\n", + " return True\n", + " if str[0] == str[-1]:\n", + " return palindrome(str[1:-1])\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "palindrome(\"ninja\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Sum of digits" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [], + "source": [ + "def sum_of_digits(n):\n", + " if n < 10:\n", + " return n\n", + " s = sum_of_digits(n//10)\n", + " return s+n%10" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum_of_digits(9)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Multiplication Recursiviely" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [], + "source": [ + "def multi(n,m):\n", + " if m == 1:\n", + " return n\n", + " m = multi(n,m-1)\n", + " return m + n" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "48" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "multi(12,4)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Count number of zeros" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": {}, + "outputs": [], + "source": [ + "def count_zeros(n):\n", + " global count\n", + " if n < 10:\n", + " if n == 0:\n", + " count +=1\n", + " else:\n", + " return\n", + " if n%10 == 0:\n", + " count +=1\n", + " count_zeros(n//10)\n", + " return count" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 96, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count = 0\n", + "count_zeros(10204)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Pair star" + ] + }, + { + "cell_type": "code", + "execution_count": 145, + "metadata": {}, + "outputs": [], + "source": [ + "def pair_star(str):\n", + " global output\n", + " if len(str) == 1:\n", + " return output + str\n", + " output += str[0]\n", + " if str[0] == str[1]:\n", + " output += \"*\"\n", + " return pair_star(str[1:])\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 147, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'a*a*a*a'" + ] + }, + "execution_count": 147, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "output = \"\"\n", + "pair_star(\"aaaa\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Geometric Sum" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "def geometric(n):\n", + " if n == 0:\n", + " return 1\n", + " s = geometric(n-1)\n", + " return s + 1/(2**(n))" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1.875" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "geometric(3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Check Palindrome " + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [], + "source": [ + "def palindorme(str):\n", + " if len(str) == 1:\n", + " return \"YES\"\n", + " if str[0] == str[-1]:\n", + " return palindorme(str[1:-1])\n", + " else:\n", + " return \"NO\"" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'YES'" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "palindorme(\"racecar\")" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + "#checking palindrome using helper function\n", + "def helper(str,start,end):\n", + " if start == end:\n", + " return True\n", + " if str[start] == str[end]:\n", + " return helper(str,start+1,end-1)\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [], + "source": [ + "def palindrome(s):\n", + " end = len(s)\n", + " return helper(s,0,end-1)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "palindrome(\"ninja\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Sum of digits (recursive)" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [], + "source": [ + "def sum_of_digits(n):\n", + " if n<10:\n", + " return n\n", + " s = sum_of_digits(n//10)\n", + " return s + n%10" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum_of_digits(1234)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Multiplication (Recursive)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [], + "source": [ + "def multiplication(N,M):\n", + " if M == 1:\n", + " return N\n", + " s = multiplication(N,M-1)\n", + " return s + N" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "multiplication(2,3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Count Zeros" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [], + "source": [ + "def count_zeros(n):\n", + " if n < 10:\n", + " if n == 0:\n", + " return 1\n", + " else:\n", + " return 0\n", + " s = count_zeros(n//10)\n", + " if n%10 == 0:\n", + " return s+1\n", + " return s\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_zeros(0)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## String to Integer" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [], + "source": [ + "def string_to_integer(str):\n", + " if len(str) == 1:\n", + " return int(str)\n", + " s = string_to_integer(str[:-1])\n", + " return 10*s + int(str[-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1234" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "string_to_integer('1234')" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [], + "source": [ + "#without using copies of string\n", + "def helper(str,start,end):\n", + " if start == end:\n", + " return int(str[start])\n", + " s = helper(str,start,end-1)\n", + " return s*10 + int(str[end])\n", + "\n", + "def str2int(s):\n", + " end = len(s)-1\n", + " return helper(s,0,end)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1234" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "str2int('1234')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Pair star" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [], + "source": [ + "def pair_star(str):\n", + " global ans\n", + " if len(str)== 1:\n", + " ans+= str\n", + " return\n", + " if str[0] == str[1]:\n", + " ans+= str[0]+ \"*\"\n", + " else:\n", + " ans+= str[0]\n", + " pair_star(str[1:])\n", + " return ans" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'hel*lo'" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ans = \"\"\n", + "pair_star(\"hello\")" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [], + "source": [ + "#pair star without copying the string\n", + "def helper(str,start,end):\n", + " global output\n", + " if start == end:\n", + " output+=str[start]\n", + " return\n", + " if str[start] == str[start+1]:\n", + " output+= str[start]+ '*'\n", + " else:\n", + " output+=str[start]\n", + " helper(str,start+1,end)\n", + " return output\n", + "\n", + "def PairStar(s):\n", + " end = len(s)-1\n", + " return helper(s,0,end)\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'hel*l*lo'" + ] + }, + "execution_count": 94, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "output = \"\" \n", + "PairStar(\"helllo\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Stair case" + ] + }, + { + "cell_type": "code", + "execution_count": 125, + "metadata": {}, + "outputs": [], + "source": [ + "def stairCase(n):\n", + " if n ==1 or n ==0:\n", + " return 1\n", + " elif n ==2:\n", + " return 2\n", + " x = stairCase(n-3)\n", + " y = stairCase(n-2)\n", + " z = stairCase(n-1)\n", + " return x+y+z" + ] + }, + { + "cell_type": "code", + "execution_count": 126, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 126, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stairCase(4)" + ] + }, + { + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/Python/Stack.ipynb b/Python/Stack.ipynb new file mode 100644 index 0000000..614b13f --- /dev/null +++ b/Python/Stack.ipynb @@ -0,0 +1,3270 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Stack using Array" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Stack:\n", + " def __init__(self):\n", + " self.__arr = []\n", + " def push(self,x):\n", + " self.__arr.append(x)\n", + " def pop(self):\n", + " if self.isempty():\n", + " return self.__arr.pop()\n", + " else:\n", + " return \"Underflow\"\n", + " def top(self):\n", + " if self.isempty():\n", + " print(self.__arr[-1])\n", + " else:\n", + " return \"underflow\"\n", + " def isempty(self):\n", + " if len(self.__arr) > 0:\n", + " return 1\n", + " else:\n", + " return 0\n", + " def length(self):\n", + " print(len(self.__arr))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sample = Stack()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sample.push(10)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sample.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sample.top()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sample.push(2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sample.top()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sample.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sample.top()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sample.length()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Stack using LL" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.next = None\n", + " \n", + "class StackLL(Node):\n", + " def __init__(self):\n", + " self.head = None\n", + " self.tail = None\n", + " self.count = 0\n", + " self.prev = None\n", + " def push(self,data):\n", + " if self.head == None:\n", + " newNode = Node(data)\n", + " self.head = newNode\n", + " self.tail = newNode\n", + " else:\n", + " if self.prev is None:\n", + " self.prev = self.tail\n", + " else:\n", + " self.prev.next = self.tail\n", + " self.prev = self.tail\n", + " newNode = Node(data)\n", + " self.tail.next = newNode\n", + " self.tail = newNode\n", + " self.count +=1\n", + " def pop(self):\n", + " if self.isempty():\n", + " print(self.tail.data)\n", + " self.count-=1\n", + " if self.prev is not None:\n", + " self.prev.next = None\n", + " self.tail = self.prev\n", + " else:\n", + " return \"underflow\"\n", + " def isempty(self):\n", + " if self.count >0:\n", + " return 1\n", + " else:\n", + " return 0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = StackLL()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.push(10)\n", + "a.push(20)\n", + "a.push(30)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Stack Revision" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.next = None\n", + "class Stack:\n", + " def __init__(self):\n", + " self.head = None\n", + " self.tail = None\n", + " self.prev = None\n", + " self.count = 0\n", + " def push(self,data):\n", + " newNode = Node(data)\n", + " if self.head is None:\n", + " self.head = newNode\n", + " self.tail = newNode\n", + " else:\n", + " if self.prev is None:\n", + " self.prev = self.tail\n", + " else:\n", + " self.prev.next = self.tail\n", + " self.prev = self.tail\n", + " self.tail.next= newNode\n", + " self.tail = newNode\n", + " self.count +=1\n", + " def pop(self):\n", + " if self.isempty():\n", + " print(self.tail.data)\n", + " self.count -=1\n", + " if self.prev is not None:\n", + " self.prev.next = None\n", + " self.tail = self.prev\n", + " else:\n", + " return \"underflow\"\n", + " def isempty(self):\n", + " if self.count >0:\n", + " return 1\n", + " else:\n", + " return 0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = Stack()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.push(10)\n", + "a.push(20)\n", + "a.push(30)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Stack" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.next = None\n", + "class Stack:\n", + " def __init__(self):\n", + " self.head = None\n", + " self.count = 0\n", + " def push(self,data):\n", + " newNode = Node(data)\n", + " newNode.next = self.head\n", + " self.head = newNode\n", + " self.count +=1\n", + " def pop(self):\n", + " if self.isempty():\n", + " return \"Underflow\"\n", + " else:\n", + " ele = self.head\n", + " self.head = self.head.next\n", + " print(ele.data)\n", + " self.count -=1\n", + " def isempty(self):\n", + " if self.count == 0:\n", + " return 1\n", + " else:\n", + " return 0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = Stack()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.push(10)\n", + "a.push(20)\n", + "a.push(30)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Inbuilt Queue and Stack" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from queue import Queue\n", + "q = Queue()\n", + "q.put(1)\n", + "q.put(2)\n", + "q.put(3)\n", + "while not q.empty():\n", + " print(q.get())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from queue import LifoQueue\n", + "q = LifoQueue()\n", + "q.put(1)\n", + "q.put(2)\n", + "q.put(3)\n", + "while not q.empty():\n", + " print(q.get())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Balanced Paranthesis" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def balanced(string):\n", + " arr = []\n", + " for char in string:\n", + " if char in \"([{\":\n", + " arr.append(char)\n", + " elif char == ')':\n", + " if (arr and arr[-1] == '('):\n", + " arr.pop()\n", + " else:\n", + " return False\n", + " elif char == ']':\n", + " if (arr and arr[-1] == \"[\"):\n", + " arr.pop()\n", + " else:\n", + " return False\n", + " elif char == '}':\n", + " if (arr and arr[-1] == \"{\"):\n", + " arr.pop()\n", + " else:\n", + " return False\n", + " if len(arr) != 0:\n", + " return False\n", + " else:\n", + " return True\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "balanced(\"([(])\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Linked List revision" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.next = None\n", + "def inputll():\n", + " arr = [int(a) for a in input().split()]\n", + " head = None\n", + " tail = None\n", + " for a in arr:\n", + " newNode = Node(a)\n", + " if head is None:\n", + " head = newNode\n", + " tail = newNode\n", + " else:\n", + " tail.next= newNode\n", + " tail = newNode\n", + " return head\n", + "def outputll(head):\n", + " while head is not None:\n", + " print(head.data,end = \" -> \")\n", + " head = head.next\n", + " print(None)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = inputll()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "outputll(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Reverse Linked List" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def reverse(head):\n", + " if head.next is None:\n", + " return head\n", + " smallHead = reverse(head.next)\n", + " temp = head.next\n", + " temp.next = head\n", + " head.next = None\n", + " return smallHead" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "b = reverse(a)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "outputll(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Stack using Linked List" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Stack:\n", + " def __init__(self):\n", + " self.head = None\n", + " self.count =0\n", + " def push(self,data):\n", + " newNode = Node(data)\n", + " newNode.next = self.head\n", + " self.head= newNode\n", + " self.count+=1\n", + " def pop(self):\n", + " if self.isempty():\n", + " return \"Underflow\"\n", + " else:\n", + " temp = self.head.data\n", + " self.head = self.head.next\n", + " self.count -=1\n", + " return temp\n", + " def isempty(self):\n", + " if self.count == 0:\n", + " return 1\n", + " else:\n", + " return 0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = Stack()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.push(10)\n", + "a.push(20)\n", + "a.push(30)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Queue using inbuilt Function" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import queue\n", + "a = queue.Queue()\n", + "a.put(10)\n", + "a.put(20)\n", + "while not a.empty():\n", + " print(a.get())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Stack using inbuilt Function" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "b = queue.LifoQueue()\n", + "b.put(10)\n", + "b.put(20)\n", + "while not b.empty():\n", + " print(b.get())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Balanced Paranthesis" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def balanced(string):\n", + " arr = []\n", + " for ele in string:\n", + " if ele in \"([{\":\n", + " arr.append(ele)\n", + " elif ele == ')':\n", + " if arr and arr[-1] == '(':\n", + " arr.pop()\n", + " else:\n", + " return False\n", + " elif ele == ']':\n", + " if arr and arr[-1] == '[':\n", + " arr.pop()\n", + " else:\n", + " return False\n", + " elif ele == '}':\n", + " if arr and arr[-1] == '{':\n", + " arr.pop()\n", + " else:\n", + " return False \n", + " if len(arr) == 0:\n", + " return True\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "balanced(\"[(a+b)+({a+b}]\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Stack revision" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.next = None\n", + " \n", + "class Stack:\n", + " def __init__(self):\n", + " self.head = None\n", + " self.count = 0\n", + " def isempty(self):\n", + " if self.count == 0:\n", + " return True\n", + " else:\n", + " return False\n", + " def push(self,data):\n", + " newNode = Node(data)\n", + " newNode.next = self.head\n", + " self.head = newNode\n", + " self.count +=1\n", + " def pop(self):\n", + " if self.isempty():\n", + " return \"underflow\"\n", + " else:\n", + " temp = self.head.data\n", + " self.head = self.head.next\n", + " self.count -=1\n", + " return temp\n", + " def empty(self):\n", + " if self.count > 0:\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = Stack()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.push(10)\n", + "a.push(20)\n", + "a.push(30)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Balanced Paranthesis" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def balanced(string):\n", + " arr= []\n", + " for char in string:\n", + " if char in \"([{\":\n", + " arr.append(char)\n", + " elif char == \")\":\n", + " if (arr and arr[-1] == \"(\"):\n", + " arr.pop()\n", + " else:\n", + " return False\n", + " elif char == \"]\":\n", + " if (arr and arr[-1] == \"[\"):\n", + " arr.pop()\n", + " else:\n", + " return False\n", + " elif char == \"}\":\n", + " if (arr and arr[-1] == \"{\"):\n", + " arr.pop()\n", + " else:\n", + " return False\n", + " if arr:\n", + " return False\n", + " return True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "balanced(\"{[(a+b)+c]+a}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "balanced(\"(()\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Reversing a stack" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = Stack()\n", + "a.push(10)\n", + "a.push(20)\n", + "a.push(30)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.push(10)\n", + "a.push(20)\n", + "a.push(30)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def reverse(a1):\n", + " a2 = Stack()\n", + " while a1.empty():\n", + " a2.push(a1.pop())\n", + " return a2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "d = reverse(a)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "d.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "d.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "d.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "d.pop()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Reversing Stack using recursion" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def reverse(s1,s2):\n", + " if s1.count == 1:\n", + " s2.push(s1.pop())\n", + " return\n", + " s2.push(s1.pop())\n", + " reverse(s1,s2)\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s1 = Stack()\n", + "s1.push(10)\n", + "s1.push(20)\n", + "s1.push(30)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s2= Stack()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "reverse(s1,s2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s2.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s2.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s2.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s2.pop()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Reverse Stack better way" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def reverseStack(s1,s2):\n", + " if s1.count == 0 or s1.count ==1:\n", + " return\n", + " while s1.count!= 1:\n", + " s2.push(s1.pop())\n", + " temp = s1.pop()\n", + " \n", + " while s2.count!=0:\n", + " s1.push(s2.pop())\n", + " reverseStack(s1,s2)\n", + " s1.push(temp)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s1 = Stack()\n", + "s1.push(10)\n", + "s1.push(20)\n", + "s1.push(30)\n", + "s2 = Stack()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "reverseStack(s1,s2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s2.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s1.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s1.pop()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Redundant Bracket" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def redundant(string):\n", + " arr = []\n", + " for char in string:\n", + " if char in \"(\":\n", + " arr.append(char)\n", + " elif char == \")\":\n", + " if arr:\n", + " arr.pop()\n", + " else:\n", + " return False\n", + " if arr:\n", + " return False\n", + " return True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "redundant(\"(a+b)\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "redundant(\"a+(b)+c \")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "redundant(\" ((a+b)))\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Complete Revision" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Linked List" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.next = None\n", + "def inputll():\n", + " arr = [int(a) for a in input().split()]\n", + " head = None\n", + " tail = None\n", + " for a in arr:\n", + " newNode = Node(a)\n", + " if head is None:\n", + " head = newNode\n", + " tail = newNode\n", + " else:\n", + " tail.next = newNode\n", + " tail = newNode\n", + " return head\n", + "def outputll(head):\n", + " while head is not None:\n", + " print(head.data,end = \" -> \")\n", + " head = head.next\n", + " print(None)\n", + "def reversell(head):\n", + " if head.next is None:\n", + " return head\n", + " smallHead = reversell(head.next)\n", + " temp = head.next\n", + " temp.next = head\n", + " head.next = None\n", + " return smallHead" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = inputll()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "outputll(a)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "b = reversell(a)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "outputll(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Stack" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Stack:\n", + " def __init__(self):\n", + " self.head = None\n", + " self.count = 0\n", + " def push(self,data):\n", + " newNode = Node(data)\n", + " newNode.next = self.head\n", + " self.head = newNode\n", + " self.count +=1\n", + " def pop(self):\n", + " if self.isempty():\n", + " temp = self.head.data\n", + " self.head = self.head.next\n", + " self.count -= 1\n", + " return temp\n", + " else:\n", + " return \"Underflow\"\n", + " def isempty(self):\n", + " if self.count == 0:\n", + " return False\n", + " else:\n", + " return True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a =Stack()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.push(10)\n", + "a.push(20)\n", + "a.push(30)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Reversing Stack" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def reverse_stack(s1,s2):\n", + " if s1.count <= 1:\n", + " return \n", + " \n", + " while s1.count != 1:\n", + " s2.push(s1.pop())\n", + " \n", + " temp = s1.pop()\n", + " \n", + " while s2.count != 0:\n", + " s1.push(s2.pop())\n", + " reverse_stack(s1,s2)\n", + " s1.push(temp)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s1 = Stack()\n", + "s1.push(10)\n", + "s1.push(20)\n", + "s1.push(30)\n", + "s2 = Stack()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "reverse_stack(s1,s2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s1.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s1.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s1.pop()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Complete Revision" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Linked List" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.next = None\n", + "def inputLL():\n", + " arr = [int(a) for a in input().split()]\n", + " head = None\n", + " tail = None\n", + " for a in arr:\n", + " newNode = Node(a)\n", + " if head is None:\n", + " head = newNode\n", + " tail = newNode\n", + " else:\n", + " tail.next = newNode\n", + " tail = newNode\n", + " return head\n", + "def outputLL(head):\n", + " while head is not None:\n", + " print(head.data ,end = \" -> \")\n", + " head = head.next\n", + " print(None)\n", + "def reverseLL(head):\n", + " if head.next is None:\n", + " return head\n", + " smallHead = reverseLL(head.next)\n", + " temp = head.next\n", + " temp.next = head\n", + " head.next = None\n", + " return smallHead" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = inputLL()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "outputLL(a)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "b = reverseLL(a)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "outputLL(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Stack using array" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Stack:\n", + " def __init__(self):\n", + " self.arr = []\n", + " def push(self,data):\n", + " self.arr.append(data)\n", + " def pop(self):\n", + " if self.isempty():\n", + " temp = self.arr[-1]\n", + " self.arr.pop()\n", + " return temp\n", + " else:\n", + " return \"Underflow\"\n", + " def isempty(self):\n", + " if len(self.arr) == 0:\n", + " return False\n", + " else:\n", + " return True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = Stack()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.push(10)\n", + "a.push(20)\n", + "a.push(30)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Stack using Linked List" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class StackLL:\n", + " def __init__(self):\n", + " self.head = None\n", + " self.count =0\n", + " def push(self,data):\n", + " newNode = Node(data)\n", + " newNode.next = self.head\n", + " self.head = newNode\n", + " self.count +=1\n", + " def pop(self):\n", + " if self.isempty():\n", + " temp = self.head.data\n", + " self.head = self.head.next\n", + " self.count -=1\n", + " return temp\n", + " else:\n", + " return \"Underflow\"\n", + " def isempty(self):\n", + " if self.head == None:\n", + " return False\n", + " else:\n", + " return True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = StackLL()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.push(10)\n", + "a.push(20)\n", + "a.push(30)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Reversing Stack" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def reverseStack(s1,s2):\n", + " if s1.count == 0 or s1.count == 1:\n", + " return \n", + " while s1.count >1:\n", + " s2.push(s1.pop())\n", + " temp = s1.pop()\n", + " while s2.count !=0:\n", + " s1.push(s2.pop())\n", + " reverseStack(s1,s2)\n", + " s1.push(temp)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s1 = StackLL()\n", + "s1.push(10)\n", + "s1.push(20)\n", + "s1.push(30)\n", + "s2 = StackLL()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "reverseStack(s1,s2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s1.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s1.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s1.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "s1.pop()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Redudant Bracket" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def reduce(string):\n", + " arr= []\n", + " for char in string:\n", + " if char in '([{':\n", + " arr.append(char)\n", + " elif char == ')':\n", + " if arr and arr[-1] == '(':\n", + " arr.pop()\n", + " else:\n", + " return False\n", + " elif char == '}':\n", + " if arr and arr[-1] == '{':\n", + " arr.pop()\n", + " else:\n", + " return False\n", + " elif char == ']':\n", + " if arr and arr[-1] == '[':\n", + " arr.pop()\n", + " else:\n", + " return False\n", + " if arr:\n", + " return False\n", + " return True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "reduce(\"a+b{}[()]\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def redudant(string):\n", + " arr = []\n", + " for char in string:\n", + " if char == '(':\n", + " arr.append(char)\n", + " else:\n", + " arr.pop()\n", + " if arr:\n", + " return False\n", + " return True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "redudant(\"((((())))\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "redudant(\"(((((())))))\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Stock Span" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def stock(arr):\n", + " length = len(arr)\n", + " stock = length*[1]\n", + " for i in range(1,len(arr)):\n", + " for j in range(i-1,-1,-1):\n", + " if arr[j]price[i]:\n", + " stack.append(i)\n", + " s[i] = 1\n", + " else:\n", + " count =1\n", + " while stack and price[i] > price[stack[-1]]:\n", + " count += s[stack[-1]]\n", + " stack.pop()\n", + " stack.append(i)\n", + "# s[i] = s[i-1]+count\n", + " s[i] = count\n", + " \n", + " return s" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "stock_span([100, 80, 60, 70, 60, 75, 85])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Revision Stock Span" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [], + "source": [ + "def stock_span(price):\n", + " length = len(price)\n", + " ans = length*[0]\n", + " first = price[0]\n", + " ans[0] = 1\n", + " stack = [0]\n", + " for i in range(1,length):\n", + " if price[i-1]>price[i]:\n", + " stack.append(i)\n", + " ans[i] = 1\n", + " else:\n", + " count = 1\n", + " while stack and price[i] >price[stack[-1]]:\n", + " count += ans[stack[-1]]\n", + " stack.pop()\n", + " stack.append(i)\n", + " ans[i] = count\n", + " return ans" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 1, 1, 2, 8]" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stock_span([60 ,70 ,80 ,100 ,90 ,75 ,80 ,120])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Minimum Bracket Transversal" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "T.C = O(n)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def min_bracket(string):\n", + " arr = []\n", + " for char in string:\n", + " if char == \"{\":\n", + " arr.append(char)\n", + " elif char == \"}\":\n", + " if arr and arr[-1] == \"{\":\n", + " arr.pop()\n", + " else:\n", + " arr.append(char)\n", + " if len(arr)%2 == 0:\n", + " return len(arr)//2\n", + " else:\n", + " return -1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "min_bracket(\"{{{{}}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "min_bracket(\"{{{\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "min_bracket(\"}}{{}}}}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Inbuilt Stack and Queue" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "import queue\n", + "a = queue.Queue()\n", + "a.put(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "a.put(20)\n", + "a.put(30)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "20\n", + "30\n" + ] + } + ], + "source": [ + "while not a.empty():\n", + " print(a.get())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Revision" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data = data\n", + " self.next = None\n", + "def inputLL():\n", + " arr = [int(a) for a in input().split()]\n", + " head = None\n", + " tail = None\n", + " for i in arr:\n", + " newNode = Node(i)\n", + " if head is None:\n", + " head = newNode\n", + " tail = newNode\n", + " else:\n", + " tail.next = newNode\n", + " tail = newNode\n", + " return head\n", + "def outputLL(head):\n", + " while head is not None:\n", + " print(head.data , end = \" -> \")\n", + " head = head.next\n", + " print(None)\n", + "def reverseLL(head):\n", + " if head.next is None:\n", + " return head\n", + " smallHead = reverseLL(head.next)\n", + " temp = head.next\n", + " temp.next = head\n", + " head.next = None\n", + " return smallHead" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 4 5 6 7 8 9\n" + ] + } + ], + "source": [ + "a = inputLL()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> None\n" + ] + } + ], + "source": [ + "outputLL(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "b = reverseLL(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> None\n" + ] + } + ], + "source": [ + "outputLL(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Stack" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [], + "source": [ + "class Stack:\n", + " def __init__(self):\n", + " self.head = None\n", + " self.count =0\n", + " def push(self,data):\n", + " newNode = Node(data)\n", + " newNode.next = self.head\n", + " self.head = newNode\n", + " self.count +=1\n", + " def pop(self):\n", + " if self.count <1:\n", + " return \"Underflow\"\n", + " else:\n", + " temp = self.head.data\n", + " self.head = self.head.next\n", + " self.count -=1\n", + " return temp" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [], + "source": [ + "a = Stack()" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [], + "source": [ + "a.push(10)\n", + "a.push(30)\n", + "a.push(50)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "50" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Underflow'" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "class Stack_arr:\n", + " def __init__(self):\n", + " self.arr = []\n", + " self.count = 0\n", + " def push(self,data):\n", + " self.arr.append(data)\n", + " self.count +=1\n", + " def pop(self):\n", + " if self.count <1:\n", + " return \"Underflow\"\n", + " else:\n", + " self.count-=1\n", + " return self.arr.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "a = Stack_arr()" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [], + "source": [ + "a.push(10)\n", + "a.push(20)\n", + "a.push(30)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Underflow'" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [], + "source": [ + "def reverseStack(s1,s2):\n", + " if s1.count == 1 or s1.count == 0:\n", + " return\n", + " while s1.count > 1:\n", + " s2.push(s1.pop())\n", + " temp = s1.pop()\n", + " while s2.count != 0:\n", + " s1.push(s2.pop())\n", + " reverseStack(s1,s2)\n", + " s1.push(temp)\n", + " return s1\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [], + "source": [ + "b = Stack()" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.Stack at 0x242f5616ac0>" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "reverseStack(a,b)" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "50" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Underflow'" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.pop()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Queue" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [], + "source": [ + "class Queue:\n", + " def __init__(self):\n", + " self.front = None\n", + " self.rear = None\n", + " self.count = 0\n", + " def enqueue(self,data):\n", + " newNode = Node(data)\n", + " if self.front is None:\n", + " self.front = newNode\n", + " self.rear = newNode\n", + " else:\n", + " self.rear.next = newNode\n", + " self.rear = newNode\n", + " self.count +=1\n", + " def dequeue(self):\n", + " if self.count <1:\n", + " return \"Underflow\"\n", + " else:\n", + " temp = self.front.data\n", + " self.front = self.front.next\n", + " self.count -=1\n", + " return temp" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "a = Queue()" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [], + "source": [ + "a.enqueue(10)\n", + "a.enqueue(20)\n", + "a.enqueue(30)" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.dequeue()" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.dequeue()" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.dequeue()" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Underflow'" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.dequeue()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Stock Span" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [], + "source": [ + "def stock_span(arr):\n", + " ans = []\n", + " ans.append(1)\n", + " stack= []\n", + " stack.append(0)\n", + " count = 1\n", + " for i in range(1,len(arr)):\n", + " if arr[i] arr[stack[-1]]:\n", + " count += ans[stack[-1]]\n", + " stack.pop()\n", + " ans.append(count)\n", + " stack.append(i)\n", + " count =1\n", + " return ans" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 1, 1, 2, 8]" + ] + }, + "execution_count": 94, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stock_span([60 ,70 ,80 ,100 ,90 ,75 ,80 ,120])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Implement Queue using 2 stacks" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Q. Enqueue time complexity should be 1 and dequeue be n or vice verse" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "TC = C + n-1 +n = O(n)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "class Queue:\n", + " def __init__(self):\n", + " self.s1 = Stack()\n", + " self.s2 = Stack()\n", + " self.count = 0\n", + " def enqueue(self,data):\n", + " self.s1.push(data)\n", + " self.count +=1\n", + " def dequeue(self):\n", + " if self.s1.count <1:\n", + " return \"Underflow\"\n", + " else:\n", + " while self.s1.count >1:\n", + " self.s2.push(self.s1.pop())\n", + " temp =self.s1.pop()\n", + " while self.s2.count !=0:\n", + " self.s1.push(self.s2.pop())\n", + " return temp" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [], + "source": [ + "a = Queue()" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [], + "source": [ + "a.enqueue(10)\n", + "a.enqueue(20)\n", + "a.enqueue(30)\n", + "a.enqueue(40)\n", + "a.enqueue(50)\n", + "a.enqueue(60)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.dequeue()" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.dequeue()" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.dequeue()" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Underflow'" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.dequeue()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# reverse Queue" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [], + "source": [ + "def reverseQueue(s1,s2,k): \n", + " if s1.count == 0:\n", + " return \n", + " while s1.count >k:\n", + " s2.push(s1.dequeue())\n", + " while s2.count:\n", + " s1.enqueue(s2.pop())\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [], + "source": [ + "b = Stack()\n", + "reverseQueue(a,b,3)" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "40" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.dequeue()" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "50" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.dequeue()" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "60" + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.dequeue()" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Underflow'" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.dequeue()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "4\n", + "1\n", + "2\n", + "3\n", + "4\n", + "2\n", + "3\n", + "4\n" + ] + } + ], + "source": [ + "from queue import Queue\n", + "a = Queue()\n", + "n = int(input())\n", + "k = int(input())\n", + "\n", + "for i in range(k):\n", + " a.put(int(input()))\n", + "p = int(input())\n", + "while p:\n", + " a.get()\n", + " p-=1\n", + " \n", + "while not a.empty():\n", + " print(a.get())" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.get()" + ] + }, + { + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}